Update ViewPager dynamically?

后端 未结 20 3388
时光说笑
时光说笑 2020-11-22 02:00

I can\'t update the content in ViewPager.

What is the correct usage of methods instantiateItem() and getItem() in FragmentPagerAdapter class?

I was using onl

20条回答
  •  耶瑟儿~
    2020-11-22 02:37

    This might be of help to someone - in my case when inserting a new page the view pager was asking for the position of an existing fragment twice, but not asking for the position of the new item, causing incorrect behaviour and data not displaying.

    1. Copy the source for for FragmentStatePagerAdapter (seems to have not been updated in ages).

    2. Override notifyDataSetChanged()

      @Override
      public void notifyDataSetChanged() {
          mFragments.clear();
          super.notifyDataSetChanged();
      }
      
    3. Add a sanity check to destroyItem() to prevent crashes:

      if (position < mFragments.size()) {
          mFragments.set(position, null);
      }
      

提交回复
热议问题