Android笔记--在切换fragment时,让fragment里的listview实时刷新数据的一种方法

来源:互联网 发布:物竞化学品数据库 编辑:程序博客网 时间:2024/05/22 03:24

使用FragmentPagerAdapter 可以处理多个 Fragment 页面的横向滑动。

但在使用过程中,会发现当从fragmentA切换到fragmentB页面时,需要的功能是fragmentB的页面能够重新加载数据,而不是用FragmentManager里面缓存的fragmentB。网上的方法很多,但是很多没有用,所以我使用了定义一个静态变量的方法来实现,做下笔记。

1、在fragmentB里定义一个静态的listview。

2、在activity里的实现OnPageChangeListener接口,并实现onPageSelected(int index)方法,使用switch流程控制语句监听当切换到第二页(index=1)时,实现重新加载数据的方法。

3、new出一个fragmentB,然后使用fragmentB里的静态变量listview适配数据,完成重新加载数据的功能。 

0 0
原创粉丝点击