I have created an infinite extension of FragmentPagerAdapter
(there are examples on how to achieve this on this site). This allows me to iterate through 50 (arbitrary number) sets of 52 fragments (one per week) thereby giving an infinite feel of fragments to the user.
When scrolling/jumping between fragments by calling ViewPager.setCurrentItem
, there are two scenarios that I see:
- Jumping only one fragment either way - all is ok. This is presumably due to the code that specializes this use-case in
ViewPager.setCurrentItemInternal
(look for the comment beginning with the wordsWe are doing a jump by more than one page
) - Jumping by more than one fragment, the new fragment is shown properly on the screen only if
setCurrentItem
is called whensmoothScroll
is set totrue
(i.e.setCurrentItem(i, true)
); otherwise there is a blank screen
From what I can see, this is probably because ViewPager.scrollToItem
has the following code in it:
if (smoothScroll) {
smoothScrollTo(destX, 0, velocity);
if (dispatchSelected) {
dispatchOnPageSelected(item);
}
} else {
if (dispatchSelected) {
dispatchOnPageSelected(item);
}
completeScroll(false);
scrollTo(destX, 0);
pageScrolled(destX);
}
This is the point where I am out of my depth. Why would this if/else
cause the phenomena that I am experiencing?
FragmentPagerAdapter#getItem()
is properly being called with the appropriate positions. It sounds like it's not loading the fragments appropriately for some reason. You should see it called two maybe three times. Once at the selected position, one before and one after. – CultivargetItem
is called 3 times whensetCurrentItem(i, false)
(i.e. the unexplained situation) is triggered. Notice that the log hasgetItem(1327)
yetnewInstance(27)
. This is because of the "infinite" viewer - 1327 % 52 = 27 – Cicily