Make ViewPager snap with shorter drag

前端 未结 4 1586
囚心锁ツ
囚心锁ツ 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:15

    Here is my code used in Librera Reader

    public class MyViewPager extends ViewPager {
    
      public MyViewPager(Context context, AttributeSet attrs) {
            super(context, attrs);
            initMyScroller();
        }
    
        private void initMyScroller() {
            try {
                Class viewpager = ViewPager.class;
                Field scroller = viewpager.getDeclaredField("mScroller");
                scroller.setAccessible(true);
                scroller.set(this, new MyScroller(getContext())); // my liner scroller
    
                Field mFlingDistance = viewpager.getDeclaredField("mFlingDistance");
                mFlingDistance.setAccessible(true);
                mFlingDistance.set(this, Dips.DP_10);//10 dip short drag
    
                Field mMinimumVelocity = viewpager.getDeclaredField("mMinimumVelocity");
                mMinimumVelocity.setAccessible(true);
                mMinimumVelocity.set(this, 0); //0 velocity
    
            } catch (Exception e) {
                LOG.e(e);
            }
    
        }
    
        public class MyScroller extends Scroller {
            public MyScroller(Context context) {
                super(context, new LinearInterpolator()); // my LinearInterpolator
            }
    
            @Override
            public void startScroll(int startX, int startY, int dx, int dy, int duration) {
                super.startScroll(startX, startY, dx, dy, 175);//175 duration
            }
        }
    
     }
    

提交回复
热议问题