FragmentPagerAdapter notifyDataSetChanged not working

前端 未结 6 578
天涯浪人
天涯浪人 2021-01-31 15:30

I got a FragmentPagerAdapter. It\'s getItem method can return a fragment according to data it has from the outside. After I update the data its suppose to display I

6条回答
  •  甜味超标
    2021-01-31 15:57

    Every answer here did not work perfectly for me but combining multiple answers into one is right choice.

    public final class SomeFragment extends FragmentStatePagerAdapter{
        private final List fragments;
        private Fragment removeFragment; //Fragment to be removed in the next notifydatasetchanged
    
    
        @Override
        public int getItemPosition(final Object object) {
           return object.equals(this.removeFragment) ? FragmentStatePagerAdapter.POSITION_NONE : this.fragments.indexOf(object);
        }
    
    }
    

    Return POSITION_NONE only for the fragment you would like to remove from the viewpager. Every other fragment is still in your fragment list. Do not forget to remove the "removeFragment" from the list before calling notifyDataSetChanged();

提交回复
热议问题