Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android viewpager animation

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?

like image 288
user1330739 Avatar asked Jun 05 '12 06:06

user1330739


1 Answers

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;
        }
    }
}
like image 103
xiaolv Avatar answered Nov 06 '22 23:11

xiaolv