如何使用图像创建无限循环ViewPager(循环方式)

时间:2016-02-29 14:49:53

标签: android

我有一个带有滑动的5个图像的视图寻呼机,图像应该在此manner(Circular)中轻扫1-2-3-4-5-1-2-3。我还有viewpager指示符.i已经应用于代码onPageScrollStateChanged(),但是在最后一张图片上它没有被刷过。并且pagerindicator已停止工作已应用代码。

 @Override
            public void onPageScrollStateChanged(int state) {
                int currentPage =  mPager.getCurrentItem();

Log.d("currentpage","currentpage"+currentPage );
                if(currentPage==3) {
                    mPager.setCurrentItem(0);
                }

                if (state == ViewPager.SCROLL_STATE_IDLE) { //this is triggered when the switch to a new page is complete
                    final int lastPosition =       mPager.getAdapter().getCount() - 1;
                    if (currentPage== lastPosition) {
                        mPager.setCurrentItem(1, false); //false so we don't animate
                    } else if (currentPage == 0) {
                        mPager.setCurrentItem(lastPosition -1, false);
                    }
                }
            }
                });

我不想使用第三方库。请帮助

1 个答案:

答案 0 :(得分:0)

这对我有用!

 private int prevState, curState;

@Override
public void onPageScrollStateChanged(int state) {
    int currentViewPagerItem = viewPager.getCurrentItem();
    if (currentViewPagerItem == viewPager.getAdapter().getCount()-1 || currentViewPagerItem == 0){
        prevState = curState;
        curState = state;
        if (prevState == 1 && curState == 0){

            viewPager.setCurrentItem(currentViewPagerItem == 0 ? viewPager.getAdapter().getCount()-1 : 0,false);

        }

    }
}