Animation Animations for Android

I want to receive information when the animation ends after calling this function:

viewPager.setCurrentItem(2, true);

Does anyone know how to do this?

+5
source share
2 answers

I ran into the same problem. The following is my conclusion:

When the page is actually resized, it is called PageSelected. But it is called before the animation.

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

So, you need to combine these two function calls to call your function.

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;
        }
    }
}
+25
source

You can use OnPageChangeListener ..

viewPager.setOnPageChangeListener(new OnPageChangeListener() {

    @Override
    public void onPageSelected(int arg0) {
        // The code you want to run when scrolling is complete              
    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {}

    @Override
    public void onPageScrollStateChanged(int arg0) {}
});

When a new page is selected, onPageSelected () is called.

-1
source

All Articles