I\'m using the ViewPager
example with ActionBar
tabs taken from the Android documentation here.
Unfortunately, as soon as I call the
Call Viewpager.setAdapter(adapter); after item add in your array list.
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.
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);
}
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;
}