IllegalStateException: The application's PagerAdapter changed the adapter's content without calling PagerAdapter#notifyDataSetChanged

前端 未结 10 1995
感情败类
感情败类 2020-11-29 06:52

I\'m using the ViewPager example with ActionBar tabs taken from the Android documentation here.

Unfortunately, as soon as I call the

相关标签:
10条回答
  • 2020-11-29 07:54

    Call Viewpager.setAdapter(adapter); after item add in your array list.

    0 讨论(0)
  • 2020-11-29 07:55

    One more thing to be check here is the adapter for which it gives this error check source list is getting reference from other list or not.

    It is possible that your adapter list is referenced from other list and in that list got update and added some items.

    0 讨论(0)
  • 2020-11-29 07:57

    I fixed it by callinig notifyDataSetChanged() once and just before next call of getCount():

    private boolean doNotifyDataSetChangedOnce = false;
    
    @Override
    public int getCount() {
    
      if (doNotifyDataSetChangedOnce) {
        doNotifyDataSetChangedOnce = false;
        notifyDataSetChanged();
      }
    
      return actionBar.getTabCount();
    
    }
    
    private void addTab(String text) {
    
      doNotifyDataSetChangedOnce = true;
    
      Tab tab = actionBar.newTab();
      tab.setText(text);
      tab.setTabListener(this);
      actionBar.addTab(tab);
    
    }
    
    private void removeTab(int position) {
    
      doNotifyDataSetChangedOnce = true;
    
      actionBar.removeTabAt(position);
    
    }
    
    0 讨论(0)
  • 2020-11-29 07:58

    Make sure the values in setOffscreenPageLimit and getCount match, otherwise you will get this error:

        mViewPager.setOffscreenPageLimit(MAX_TABS);
    
        public class SectionsPagerAdapter extends FragmentPagerAdapter {
        ...
        @Override
        public int getCount() {
            return MAX_TABS;
        }
    
    0 讨论(0)
提交回复
热议问题