fragment 动态加载,删除
2015-10-22 19:11
225 查看
2、最关键的地方出来了:--pagerAdapter, 一定要替换成下文的样式才能正常删除fragment
注:引用stackoverflow牛人的解决说明:
The ViewPager doesn't remove your fragments with the code above because it loads several views (or fragments in your case) into memory. In addition to the visible view, it also loads the view to either side of the visible one. This provides the smooth scrolling
from view to view that makes the ViewPager so cool.
To achieve the effect you want, you need to do a couple of things.
Change the FragmentPagerAdapter to a FragmentStatePagerAdapter. The reason for this is that the FragmentPagerAdapter will keep all the views that it loads into memory forever. Where the FragmentStatePagerAdapter disposes of views that fall outside the current
and traversable views.
Override the adapter method getItemPosition (shown below). When we call mAdapter.notifyDataSetChanged(); the ViewPager interrogates the adapter to determine what has changed in terms of positioning. We use this method to say that everything has changed so
reprocess all your view positioning。
大致意思是说:viewpager在加载当前页的时候已经将pager页左右页的内容加载进内存里了,这样才保证了viewpager左右滑动的时候的流畅性;
为了解决彻底删除fragment,我们要做的是:
1.将FragmentPagerAdapter 替换成FragmentStatePagerAdapter,因为前者只要加载过,fragment中的视图就一直在内存中,在这个过程中无论你怎么刷新,清除都是无用的,直至程序退出; 后者 可以满足我们的需求。
2.我们可以重写Adapter的方法--getItemPosition(),让其返回PagerAdapter.POSITION_NONE即可。
到这一步我们就可以真正的实现随意、彻底删除viewpager中的fragment,随意添加。
以下为引用内容: public class HomePageAdapter extends FragmentStatePagerAdapter {//FragmentPagerAdapter // private FragmentManager fm; private ArrayList<Fragment> fragments = null; private List<HotIssues> hotIssuesList; private Context context; public HomePageAdapter(Context context, FragmentManager fm, ArrayList<Fragment> fragments) { super(fm); this.context = context; this.fragments = fragments; notifyDataSetChanged(); } public HomePageAdapter(Context context, FragmentManager fm, List<HotIssues> hotIssuesList) { super(fm); this.context = context; this.hotIssuesList = hotIssuesList; notifyDataSetChanged(); //this.fragments = fragments; } @Override public Fragment getItem(int arg0) { //Fragment fragment = new ColourFragment(); //Bundle args = new Bundle(); //args.putInt("title", arg0); //args.putSerializable("content",hotIssuesList.get(arg0)); //fragment.setArguments(args); //return fragment; return fragments.get(arg0); } @Override public int getItemPosition(Object object) { // TODO Auto-generated method stub return PagerAdapter.POSITION_NONE; } @Override public int getCount() { return fragments.size();//hotIssuesList.size(); } }
注:引用stackoverflow牛人的解决说明:
The ViewPager doesn't remove your fragments with the code above because it loads several views (or fragments in your case) into memory. In addition to the visible view, it also loads the view to either side of the visible one. This provides the smooth scrolling
from view to view that makes the ViewPager so cool.
To achieve the effect you want, you need to do a couple of things.
Change the FragmentPagerAdapter to a FragmentStatePagerAdapter. The reason for this is that the FragmentPagerAdapter will keep all the views that it loads into memory forever. Where the FragmentStatePagerAdapter disposes of views that fall outside the current
and traversable views.
Override the adapter method getItemPosition (shown below). When we call mAdapter.notifyDataSetChanged(); the ViewPager interrogates the adapter to determine what has changed in terms of positioning. We use this method to say that everything has changed so
reprocess all your view positioning。
大致意思是说:viewpager在加载当前页的时候已经将pager页左右页的内容加载进内存里了,这样才保证了viewpager左右滑动的时候的流畅性;
为了解决彻底删除fragment,我们要做的是:
1.将FragmentPagerAdapter 替换成FragmentStatePagerAdapter,因为前者只要加载过,fragment中的视图就一直在内存中,在这个过程中无论你怎么刷新,清除都是无用的,直至程序退出; 后者 可以满足我们的需求。
2.我们可以重写Adapter的方法--getItemPosition(),让其返回PagerAdapter.POSITION_NONE即可。
以下为引用内容: @Override public int getItemPosition(Object object) { // TODO Auto-generated method stub return PagerAdapter.POSITION_NONE; }
到这一步我们就可以真正的实现随意、彻底删除viewpager中的fragment,随意添加。
相关文章推荐
- 数据结构:加权图的API
- less的使用方法
- Java 正则表达式
- 2015长春区域赛感想
- jquery date 添加年月日<select><option>
- opencv2读取视频提取ROI区域并把ROI区域保存为图片
- 条款1:仔细选择你的容器
- Javascript的websocket的使用方法
- 如何使用CC2540 USB-Dongle进行抓取蓝牙通讯数据包
- zoj 3913 Bob wants to pour water(二分)
- mac nodejs安装配置
- YARN内存使用优化配置
- Android的handler详解
- Python prettytable的使用方法
- Linux file 命令 【转载】
- VS2015 property manager
- leetcode Combinations
- 大道至简第四章读后感
- ANDROID仿淘宝商品浏览滑(拖)动查看详情界面
- 小小君的C语言第十课