Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make ViewPager snap with shorter drag

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.

like image 498
Juhani Avatar asked Jun 26 '13 11:06

Juhani


2 Answers

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;
    }
}
like image 87
straya Avatar answered Oct 02 '22 23:10

straya


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

like image 25
astuetz Avatar answered Oct 02 '22 23:10

astuetz