onPageSelected isn't triggered when calling setCurrentItem(0)

I have an Activity with a ViewPager which displays a bunch of pictures. When it starts the ViewPager's position is set based on what the user selected in a previous Activity. Similar to a gallery.

I want the onPageSelected to be called every time a new page is selected, i.e. when the Activity is first opened or when the user slides to a new page.

I set the starting point like so:

 mPager.setCurrentItem(index);

Everything works, except when setCurrentItem called with index set to 0 as this will not trigger onPageSelected.

mPager.setOnPageChangeListener(new OnPageChangeListener() {
  @Override
  public void onPageSelected(int index) {
    Log.d(TAG, "onPageSelected " + index);
  }
  ...
}

So my question is; is this a bug, and if so what can I do about it?


Solution 1:

The cleanest solution I've found to this so far is to take a reference to the onPageChangeListener you set on the ViewPager (since I don't think there's a ViewPager.getOnPageChangeListener() method), then after you've set the ViewPager's adapter, call:

onPageChangeListener.onPageSelected(viewPager.getCurrentItem());

However, the fragment for the page at the current index won't have been instantiated yet (at least if you're using FragmentStatePagerAdapter), so you may need to wrap it in a runnable, ala:

viewPager.post(new Runnable(){
@Override
    public void run() {
        onPageChangeListener.onPageSelected(viewPager.getCurrentItem());
    }
});

Also, if within the onPageSelected handler you need a reference to the fragment, you'll have to do that yourself. I use an abstract base class for my FragmentStatePagerAdapter which overrides the instantiate and destroy methods, and adds/removes the fragments from a SparseArray.

Solution 2:

Ok, so I haven't been able to figure out whether this is a bug (or a feature). But I thought I'd share how a possible solution to the problem could look..

Write the functionality you wish to perform in a method in the Activity and then call this in the onPageSelected method.

mPager.setOnPageChangeListener(new OnPageChangeListener() {
    @Override
    public void onPageSelected(int index) {
        myOnPageSelectedLogic(index);
    }
    ...
}

And then right after calling

setCurrentItem(index);

in the Activity, add the following if statement

if(index == 0) {
    myOnPageSelectedLogic(0);
}

It's not super pretty but I hope it helps someone :)

Solution 3:

Actually I think this is a BUG. I've check the source code of ViewPager and found the only trigger of onPageSelected:

if (dispatchSelected && mOnPageChangeListener != null) {
            mOnPageChangeListener.onPageSelected(item);
        }

Above, variable dispatchSelected is determined by code:

final boolean dispatchSelected = mCurItem != item; 
//item is what you passed to setCurrentItem(item)

And, variable mCurItem is defined and initialized as:

private int mCurItem; // Index of currently displayed page.

So mCurItem is default to 0. When calling setCurrentItem(0), dispatchSelected will be false thus onPageSelected() will not be dispatched.

Now we should understand why calling setCurrentItem(0) is a problem while setCurrentItem(1, or 2, 3...) is not.

How to solve this problem:

  1. Copy the code of ViewPager to your own project and fix it:

    from 
    final boolean dispatchSelected = mCurItem != item; //the old line
    to
    final boolean dispatchSelected = mCurItem != item || mFirstLayout; //the new line
    

    then use your own ViewPager.

  2. Since you consciously called setCurrentItem(0) and have a refrence to the OnPageChangedListener, dispatch the onPageSelected() by yourself.

  3. 听楼下怎么说...