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

前端 未结 10 2108
感情败类
感情败类 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: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);
    
    }
    

提交回复
热议问题