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