Android's ViewPager determines the number of pages when viewPager.setAdapter(pagerAdapter)is called, and then it doesn't update that count even when pagerAdapter.notifyDataSetChanged() is called.
So if the number count of the pages in the adapter is dynamic, you need to run this code every time the number of pages changes: