Android viewpager animation

android, android-viewpager, animation

Solution

I have come across the same issue. the following is my conclusion:

When the page is actually changed onPageSelected will be called. But it's called before the animation.

When the animation stopped , onPageScrollStateChanged will be called with state SCROLL_STATE_IDLE.

So you have to combine this two function calls to get your function called.

Good Luck.

private class PageChangeListener implements OnPageChangeListener {

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
   }

    @Override
    public void onPageSelected(int position) {
        isPageChanged = true;
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        switch (state) {
        case ViewPager.SCROLL_STATE_IDLE:
            if (isPageChanged) {
                updateCurrentPage();//this will be called when animation ends
                isPageChanged = false;
            }
            break;
        case ViewPager.SCROLL_STATE_DRAGGING:
            break;
        case ViewPager.SCROLL_STATE_SETTLING:
            break;
        }
    }
}

Problem

I would like to be informed when the animation ends after this function call: ``` viewPager.setCurrentItem(2, true); ``` Does anyone know how to accomplish this?

Original source