Make ViewPager snap with shorter drag

前端 未结 4 1633
囚心锁ツ
囚心锁ツ 2021-01-31 10:51

Is there any way to make the support package ViewPager snap to the next page with a shorter drag? The default behaviour seems to be that even if I drag almost 75% the page still

4条回答
  •  半阙折子戏
    2021-01-31 11:34

    You can do this ad-hoc, without worrying too much about the internals of ViewPager as long as you want to increase the target zone:

    private class MyPageChangeListener implements OnPageChangeListener {
        private float mLastPositionOffset = 0f;
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            if(positionOffset < mLastPositionOffset && positionOffset < 0.9) {
                mViewPager.setCurrentItem(position);
            } else if(positionOffset > mLastPositionOffset && positionOffset > 0.1) {
                mViewPager.setCurrentItem(position+1);
            }
            mLastPositionOffset = positionOffset;
        }
    }
    

提交回复
热议问题