您的位置:首页 > 其它

动态更新ViewPager中的Fragment

2016-07-22 15:32 295 查看
http://stackoverflow.com/questions/7263291/viewpager-pageradapter-not-updating-the-view/8024557#8024557

There are several ways to achieve this.

The first option is easier, but bit more inefficient.

Override 
getItemPosition
 in
your 
PagerAdapter
 like
this:
public int getItemPosition(Object object) {
return POSITION_NONE;
}


This way, when you call 
notifyDataSetChanged()
,
the view pager will remove all views and reload them all. As so the reload effect is obtained.

The second option, suggested by Alvaro Luis Bustamante (previously alvarolb), is to 
setTag()
method
in 
instantiateItem()
 when
instantiating a new view. Then instead of using 
notifyDataSetChanged()
,
you can use 
findViewWithTag()
 to
find the view you want to update.

The second approach is very flexible and high performant. Kudos to alvarolb for the original research.

注意使用POSITION_NONE方法的时候,会将所有的Fragment移除,然后重新加载进来。

http://stackoverflow.com/questions/10849552/update-viewpager-dynamically/10852046#10852046
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: