I have a ViewPager (using a FragmentStatePagerAdapter to page fragments). The user can use left/right swiping to page from one fragment to the next; this works fine. I've also added < and > buttons for paging. The onClick listeners for these are implemented using:
mViewPager.arrowScroll(View.FOCUS_LEFT);
and
mViewPager.arrowScroll(View.FOCUS_RIGHT);
This works most of the time: when the user taps < or >, the view scrolls left or right as expected.
But on the first tap of the > button, nothing happens. I do hear a "click" confirming that the tap occurred. And I have log statements showing that the onClick listener was called, and mViewPager.arrowScroll(View.FOCUS_RIGHT)
was called. I even check the result return by arrowScroll()
; it's true
! Yet no paging happens.
After that, all tapping on the < and > buttons works fine.
Why would the first call to arrowScroll(View.FOCUS_RIGHT)
have no effect, and how can I fix it?
I guess I can try calling it twice the first time, but since I don't know why the documented behavior isn't happening, I don't know whether that approach will cause a double paging on some phones or Android versions.
Update: some logging
// Before any button taps.
QuestionAdapter: getItem(0)
QuestionAdapter: instantiateItem: position 0
QuestionAdapter: getItem(1)
QuestionAdapter: instantiateItem: position 1
QuestionAdapter: setPrimaryItem: position 0
QuestionAdapter: setPrimaryItem: position 0
// The screen is displaying page 0.
// Now I tap the > button:
QuestionAdapter: setPrimaryItem: position 0
// The screen is still displaying page 0.
// Now I tap the > button again:
QuestionAdapter: getItem(2)
QuestionAdapter: instantiateItem: position 2
QuestionAdapter: setPrimaryItem: position 1
QuestionAdapter: setPrimaryItem: position 1
QuestionAdapter: setPrimaryItem: position 1
// Now the screen displays page 1.
adapter.getItem(position)
is called for positions 0 and 1 immediately, before any buttons are tapped. This fetch-ahead behavior is consistent with what I've seen before. When I tap>
the first time,adapter.getItem(position)
is not called. When I tap>
again,adapter.getItem(2)
is called as the view scrolls from page 0 to 1, again consistent with the usual fetch-ahead behavior. What can I conclude from this? To me it just says thatarrowScroll(View.FOCUS_RIGHT)
isn't doing anything the first time it's called. – BoughinstantiateItem()
andsetPrimaryItem()
.instantiateItem
followsgetItem
: when and only whengetItem(i)
is called,instantiateItem(i)
is called with the same position.setPrimaryItem
on the other hand tracks the visible page. When I tap>
the first time, neithergetItem
norinstantiateItem
is called, butsetPrimaryItem(0)
is called (again... it was also called before any buttons were tapped). When I tap>
again, we seegetItem
andinstantiateItem
doing their fetch-ahead, andsetPrimaryItem(1)
is called as page 1 is displayed. – Bough