How to do circular scrolling on ViewPager?

后端 未结 10 1551
别那么骄傲
别那么骄傲 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:54

    Solved the missing of animation while going from right to left on swiping first page to last and vice-versa

    A slight modification in @tobi_b answer,solved my issue. for this add copy of the last page in the starting and the first page to the last.Then just remove the NOT operator from the below method in PagerAdapter.And change setCurrentItem(lastposition,false) to setCurrentItem(lastposition-1,false) and setCurrentItem(0,false) to setCurrentItem(1,false).

    private void setNextItemIfNeeded() {
        if (isScrollStateSettling()) {
            handleSetNextItem();
        }
    }
    
    private boolean isScrollStateSettling() {
        return mScrollState == ViewPager.SCROLL_STATE_SETTLING;
    }
    private void handleSetNextItem() {
        final int lastPosition = pager.getAdapter().getCount() - 1;
        if(mCurrentPosition == 0) {
            pager.setCurrentItem(lastPosition-1, false);
        } else if(mCurrentPosition == lastPosition) {
           pager.setCurrentItem(1, false);
        }
    }
    

    And in your MainActivity.java dont forget to set the current item as 1. Hoping this will help some.

提交回复
热议问题