2

I'm sorry if the title is ambiguous as I don't know how to describe my problem in a short way.

I'm using ViewPager with FragmentStatePagerAdapter to display a list of posts of a forum's thread. Each page of the ViewPager is corresponds to a page of the thread. There are some types of link that lead to the thread without specifying the page, for example: showthread.php?t=12345&goto=newpost, which means that I can only get current page and total pages after loading that link. Assume that the link will lead to page 5 and there are 10 pages, my current flow is:

  1. ViewPager has 1 page initially
  2. Load the link and notice that current page is 5 and total pages is 10
  3. Update total pages and notify ViewPager using viewPager.getAdapter().notifyDataSetChanged();. This causes ViewPager to load page 2 automatically
  4. Jump to page 5 using viewPager.setCurrentItem(page);. This causes ViewPager to load page 4, 5 and 6 automatically

So I end up with 2 redundant requests (for loading page 2, and page 5 again (the first time is through the link). Is there any ways to avoid this?

user7768828
  • 96
  • 2
  • 5

1 Answers1

0

It's default behaviour of viewpager to load previous and next page

ViewPager pager= (ViewPager)findView.... pager.setOffscreenPageLimit(1);

Check link for more detailed answer

ViewPager offscreen page limit

Community
  • 1
  • 1
Justcurious
  • 2,201
  • 1
  • 21
  • 36