Remove Fragment Page from ViewPager in Android

前端 未结 17 1164
庸人自扰
庸人自扰 2020-11-22 17:20

I\'m trying to dynamically add and remove Fragments from a ViewPager, adding works without any problems, but removing doesn\'t work as expected.

Everytime I want to

相关标签:
17条回答
  • 2020-11-22 17:47

    I had some problems with FragmentStatePagerAdapter. After removing an item:

    • there was another item used for a position (an item which did not belong to the position but to a position next to it)
    • or some fragment was not loaded (there was only blank background visible on that page)

    After lots of experiments, I came up with the following solution.

    public class SomeAdapter extends FragmentStatePagerAdapter {
    
        private List<Item> items = new ArrayList<Item>();
    
        private boolean removing;
    
        @Override
        public Fragment getItem(int position) {
            ItemFragment fragment = new ItemFragment();
            Bundle args = new Bundle();
            // use items.get(position) to configure fragment
            fragment.setArguments(args);
            return fragment;
        }
    
        @Override
        public int getCount() {
            return items.size();
        }
    
        @Override
        public int getItemPosition(Object object) {
            if (removing) {
                return PagerAdapter.POSITION_NONE;
            }
    
            Item item = getItemOfFragment(object);
    
            int index = items.indexOf(item);
            if (index == -1) {
                return POSITION_NONE;
            } else {
                return index;
            }
        }
    
       public void addItem(Item item) {
            items.add(item);
            notifyDataSetChanged();
        }
    
        public void removeItem(int position) {
            items.remove(position);
    
            removing = true;
            notifyDataSetChanged();
            removing = false;
        }
    }
    

    This solution only uses a hack in case of removing an item. Otherwise (e.g. when adding an item) it retains the cleanliness and performance of an original code.

    Of course, from the outside of the adapter, you call only addItem/removeItem, no need to call notifyDataSetChanged().

    0 讨论(0)
  • 2020-11-22 17:47

    2020 now.

    Simple add this to PageAdapter:

    override fun getItemPosition(`object`: Any): Int {
        return PagerAdapter.POSITION_NONE
    }
    
    0 讨论(0)
  • 2020-11-22 17:47

    I hope this can help what you want.

    private class MyPagerAdapter extends FragmentStatePagerAdapter {
    
        //... your existing code
    
        @Override
        public int getItemPosition(Object object){
            return PagerAdapter.POSITION_UNCHANGED;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 17:48

    You can combine both for better :

    private class MyPagerAdapter extends FragmentStatePagerAdapter {
    
        //... your existing code
    
        @Override
        public int getItemPosition(Object object){
    
          if(Any_Reason_You_WantTo_Update_Positions) //this includes deleting or adding pages
     return PagerAdapter.POSITION_NONE;
        }
    else
    return PagerAdapter.POSITION_UNCHANGED; //this ensures high performance in other operations such as editing list items.
    
    }
    
    0 讨论(0)
  • 2020-11-22 17:50

    i solved this problem by these steps

    1- use FragmentPagerAdapter

    2- in each fragment create a random id

    fragment.id = new Random().nextInt();
    

    3- override getItemPosition in adapter

    @Override
    public int getItemPosition(@NonNull Object object) {
        return PagerAdapter.POSITION_NONE;
    }
    

    4-override getItemId in adapter

     @Override
    public long getItemId(int position) {
        return mDatasetFragments.get(position).id;
    }
    

    5- now delete code is

     adapter.mDatasetFragments.remove(< item to delete position >);
     adapter.notifyDataSetChanged();
    

    this worked for me i hope help

    0 讨论(0)
提交回复
热议问题