How to create an endless round ViewPager with images (circular way)

I have a pager with 5 images with drawing, the image should be scrolled 1-2-3-4-5-1-2-3 in this manner(Circular). I also have the viewpager.i indicator applicable to the code onPageScrollStateChanged(), but it is not broken in the last image. and viewing pagerindicator- termination of work, applied the code.

 @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);
                    }
                }
            }
                });

I do not want to use a third-party library library.

+4
source share
1 answer

It works for me!

 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);

        }

    }
}
0
source

All Articles