How to do circular scrolling on ViewPager?

后端 未结 10 1553
别那么骄傲
别那么骄傲 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 05:00

    its a simple example , important part of listener is on STATE_IDLE detection of cycle end point. so first implement OnPageChangeListener class then on your handler class add this part :

    @Override
        public void onPageScrollStateChanged(int state) {
    
            if (state == ViewPager.SCROLL_STATE_IDLE) {
    
                if (mPreviousPosition == mCurrentPosition && !mIsEndOfCycle) {
    
                    if (mCurrentPosition == 0) {
                        mViewPager.setCurrentItem(getAdapterItemsCount() - 1);
                    } else {
                        mViewPager.setCurrentItem(0);
                    }
    
                    mIsEndOfCycle = true;
                } else {
                    mIsEndOfCycle = false;
                }
    
                mPreviousPosition = mCurrentPosition;
            }
    
        }
    

提交回复
热议问题