How to do circular scrolling on ViewPager?

后端 未结 10 1532
别那么骄傲
别那么骄傲 2020-11-28 04:31

I would like to set my ViewPager to do circular scrolling. I want the first page to be able to scroll to page 2 AND the last page. And I would like my last page to scroll to

10条回答
  •  迷失自我
    2020-11-28 04:55

    I slightly modified the answer from @portfoliobuilder. It's very simple. Setting the current item with no smooth scroll until PageChangeState to be "0", so it would be very smooth.

    ((ViewPager)container).setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    
        }
    
        @Override
        public void onPageSelected(int position) {
            currentPage = position;
        }
    
        @Override
        public void onPageScrollStateChanged(int state) {
    
            // state equals to 0 means the scroll action is stop
            if(state == 0) {
    
                if(currentPage == 0)
                    ((ViewPager)container).setCurrentItem(imageResourceList.size()-2,false);
    
                if(currentPage == imageResourceList.size()-1)
                    ((ViewPager)container).setCurrentItem(1,false);
            }
        }
    });
    

提交回复
热议问题