Make ViewPager snap with shorter drag

烈酒焚心 提交于 2019-12-29 14:31:31

问题


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 snaps back to the previous page when I let go. I'd like to make the snap threshold shorter and make the ViewPager snap to the next page instead.

Note that this applied to drag gesture. A fling gesture requires much shorter gesture already.


回答1:


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;
    }
}



回答2:


Looks like these values are hard-coded in a private method, so there's no simple way to override them.

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.2.2_r1/android/support/v4/view/ViewPager.java#2075




回答3:


I did this using reflection. By setting a private field mMinimumVelocity to -1, you make the ViewPager take all your scrolls as a fling scroll - which needs much shorter drag to switch to the next page. Works like a charm!

Field mMinimumVelocity;
mMinimumVelocity = ViewPager.class.getDeclaredField("mMinimumVelocity");
mMinimumVelocity.setAccessible(true);
mMinimumVelocity.set(mPager, -1);

The source code from which I know the private fields is here.

Also if you want to make the threshold a little bigger than the default 25dp of the fling scroll, you can set the mFlingDistance field to some higher value, e.g. 45dp. But remember it also affects the required drag distance of the actual fling scroll.

Field mFlingDistance;
mFlingDistance= ViewPager.class.getDeclaredField("mFlingDistance");
mFlingDistance.setAccessible(true);
mFlingDistance.set(mPager, (int) (45 * context.getResources().getDisplayMetrics().density));



回答4:


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
        }
    }

 }


来源:https://stackoverflow.com/questions/17318625/make-viewpager-snap-with-shorter-drag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!