FragmentPagerAdapter is not removing items (Fragments) correctly

前端 未结 9 1290
别跟我提以往
别跟我提以往 2021-01-30 01:30

I have implemented the FragmentPagerAdapter and and using a List to hold all fragments for my ViewPager to display. On

9条回答
  •  [愿得一人]
    2021-01-30 02:09

    After a lot of trying, i got it to work so that it removes or attaches a third fragment at the end position correctly.

    Object fragments[] = new Object[3];
    int mItems = 2;
    MyAdapter mAdapter;
    ViewPager mPager;
    
    
    public void addFragment(boolean bool) {
    
        mAdapter.startUpdate(mPager);
    
        if (!bool) {
            mAdapter.destroyItem(mPager, 2, fragments[2]);
            mItems = 2;
            fNach = false;
        }
        else if (bool && !fNach){
            mItems = 3;
            mAdapter.instantiateItem(mPager,2);
            fNach = true;
        }
        mAdapter.finishUpdate(mPager);
        mAdapter.notifyDataSetChanged();
    
    }
    
    public class MyAdapter extends FragmentPagerAdapter {
        MyAdapter(FragmentManager fm) {
            super(fm);
        }
    
        @Override
        public int getCount() {
            return mItems;
        }
    
        @Override
        public CharSequence getPageTitle(int position) {
        ... (code for the PagerTitleStrip)
        }
    
        @Override
        public Fragment getItem(int position) {
            Fragment f = null;
            switch (position) {
                case 0:
                    f = new Fragment1();
                    break;
                case 1:
                    f = new Fragment2();
                    break;
                case 2:
                    f = new Fragment3();
                    break;
            }
            return f;
        }
    
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            Object o = super.instantiateItem(container,position);
            fragments[position] = o;
            return o;
        }
    
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            super.destroyItem(container, position, object);
            System.out.println("Destroy item " + position);
            if (position >= getCount()) {
                    FragmentManager manager = ((Fragment) object).getFragmentManager();
                    FragmentTransaction ft = manager.beginTransaction();
                    ft.remove((Fragment) object);
                    ft.commit();
            }
    
        }
    }
    

    Some clarification: to get the object reference to call destroyItem, i stored the objects returned from instantiateItem in an Array. When you are adding or removing fragments, you have to announce it with startUpdate, finishUpdate and notifyDataSetChanged. The number of items have to be changed manually, for adding you increase it and instantiate it, getItem creates it then. For deletion, you call destroyItem, and in this code, it is essential the position >= mItems, because destroyItem is also called if a fragment goes out of the cache. You don't want to delete it then. The only thing which doesn't work is the swiping animation. After removing the last page, the "cannot swipe left" animation is not restored correctly on the new last page. If you swipe it, a blank page is shown and it bounces back.

提交回复
热议问题