viewpager嵌套fragment是一个比较帅气的使用,但是问题多多,毕竟没怎么用过。
之前viewpager的使用顶多是在应用开始的介绍页,viewpager内嵌几张介绍图片,其实这个使用是比较常规的;假如嵌套布局搞的复杂点,如自定义view/fragment,那么就要考虑下布局资源的分发情况了,而不是简单的对Adapter或者pager进行clear、removeAllViews。
layout.removeAllViews();//涉及到实时刷新,所以要将之前的布局清空掉。 homePager.removeAllViewsInLayout();//removeAllViews();//赋值之前先将Adapter中的 homepageFragments.clear(); if(hotIssuesList != null && hotIssuesList.size()>0) { describeArr = new String[hotIssuesList.size()]; solutionArr = new String[hotIssuesList.size()]; for(int i = 0; i < hotIssuesList.size(); i++) {//hotIssuesList.size() describeArr[i] = hotIssuesList.get(i).getHotDescirbe();//提取对应pager数据源。 solutionArr[i] = hotIssuesList.get(i).getHotSolution(); img = new ImageView(this);//准备5个小图标。 img.setScaleType(ScaleType.FIT_XY); img.setImageResource(img_ID); imgsList.add(img); img.setPadding(6, 3, 6, 3); layout.addView(img, params); // colourFragment = new ColourFragment(hotIssuesList.get(i)); // homepageFragments.add(colourFragment); // homePage2Fragment = new HomePage2Fragment(this, i, hotIssuesList.get(i)); homePage2Fragment = HomePage2Fragment.getInstance(hotIssuesList.get(i)); // homePage2Fragment = new HomePage2Fragment(this, i, new String[]{describeArr[i], solutionArr[i]}); homepageFragments.add(homePage2Fragment); // viewpagerItemView = new ViewPagerItemView(this, i, new String[]{describeArr[i], solutionArr[i]}); // homepageViews.add(viewpagerItemView); } //custom-viewpager // MyHomePagerAdapter myAdapter = new MyHomePagerAdapter(this, homepageViews, describeArr, solutionArr); HomePageAdapter adapter = new HomePageAdapter(this, getSupportFragmentManager(), homepageFragments); homePager.setAdapter(adapter); //wrapper-viewpager- // HomePageAdapter adapter = new HomePageAdapter(this, getSupportFragmentManager(), hotIssuesList); // PagerAdapter wrappedAdapter = new InfinitePagerAdapter(adapter); // homePager.setAdapter(wrappedAdapter); homePager.setCurrentItem(0); imgsList.get(0).setImageResource(img_ID_ov); homePager.setOnPageChangeListener(new PageListener()); }从代码中可以看出我已经做过了各种尝试,哎,解决一个问题真的比首次开发难度大多了。
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 callmAdapter.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。
@Override public int getItemPosition(Object object) { // TODO Auto-generated method stub return PagerAdapter.POSITION_NONE; }到这一步我们就可以真正的实现随意、彻底删除viewpager中的fragment;[随意添加完全OK]