7

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?

4

2 回答 2

29

我遇到了同样的问题。以下是我的结论:

当页面实际改变时 onPageSelected 将被调用。但它是在动画之前调用的。

当动画停止时,onPageScrollStateChanged 将被调用,状态为 SCROLL_STATE_IDLE。

所以你必须结合这两个函数调用来调用你的函数。

祝你好运。

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;
        }
    }
}
于 2012-10-15T03:51:09.640 回答
-1

您可以使用 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) {}
});

选择新页面时,将调用 onPageSelected()。

于 2012-06-05T08:50:09.803 回答