How to change ViewPager's page?

后端 未结 5 624
暗喜
暗喜 2020-11-30 22:23

I\'m using ViewPager in my app and define it in the main Activity. Inside onCreate method I load some number of pages from SharedPreferences and then pass it to

5条回答
  •  有刺的猬
    2020-11-30 22:50

    Without checking your code, I think what you are describing is that your pages are out of sync and you have stale data.

    You say you are changing the number of pages, then crashing because you are accessing the old set of pages. This sounds to me like you are not calling pageAdapter.notifyDataSetChanged() after changing your data.

    When your viewPager is showing page 3 of a set of 10 pages, and you change to a set with only 5, then call notifyDataSetChanged(), what you'll find is you are now viewing page 3 of the new set. If you were previously viewing page 8 of the old set, after putting in the new set and calling notifyDataSetChanged() you will find you are now viewing the last page of the new set without crashing.

    If you simply change your current page, you may just be masking the problem.

提交回复
热议问题