The way the ViewPager scrolls right now is by one item per gesture. It treats flinging gesture the same way no matter if it's full screen fast fling or slow dragging; at the end page advances one step only.
Is there any projects perhaps or examples that would add velocity-based flinging that scrolls multiple items based on velocity of the existing fling (if it still in progress) and scrolls further if the flinging gesture is wide and fast?
And if there's none where to start with something like this?
P.S. The bounty is offered. Please no answers with references to Gallery or HorizontalScrollView
Another option is to copy a whole
ViewPager
implementation source code from the support library and customize adetermineTargetPage(...)
method. It's responsible for determining to which page to scroll to on fling gesture. This approach is not super convenient, but works pretty well. See implementation code below:ViewPager is class from support library. Download support library source code and change about 10 lines of code in onTouchEvent method to add desired feature.
I use modified support library in my projects for about a year, because sometimes I need to modify several lines of code to make a little change or to add new method and I dont want to copy components source code. I use modified version of fragments and viewpager.
But there is one problem you'll get: once in about 6 mounth you have to merge custom support library with new official version if you need new features. And be careful with changes, you dont want to break support library classes compatibility.
The technique here is to extends
ViewPager
and mimic most of what the pager will be doing internally, coupled with scrolling logic from theGallery
widget. The general idea is to monitor the fling (and velocity and accompanying scrolls) and then feed them in as fake drag events to the underlyingViewPager
. If you do this alone, it won't work though (you'll still get only one page scroll). This happens because the fake drag implements caps on the bounds that the scroll will be effective. You can mimic the calculations in the extendedViewPager
and detect when this will happen, then just flip the page and continue as usual. The benefit of using fake drag means you don't have to deal with snapping to pages or handling the edges of theViewPager
.I tested the following code on the animation demos example, downloadable from http://developer.android.com/training/animation/screen-slide.html by replacing the ViewPager in
ScreenSlideActivity
with thisVelocityViewPager
(both in the layoutactivity_screen_slide
and the field within the Activity).There are a few minor issues with this, which can be resolved easily but I will leave up to you, namely things like if you scroll (dragging, not flinging) you can end up half way between pages (you'll want to snap on the ACTION_UP event). Also, touch events are being completely overridden in order to do this, so you will need to feed relevant events to the underlying
ViewPager
where appropriate.You can override ScrollView or HorizontalScrollView class, and add that behavior. There are many bugs in Gallery, and as I remember it's deprecated since api level 14.
I've found a better realization for me than in the checked answer, this ViewPager behaves better with touches when I want to stop scrolling https://github.com/Benjamin-Dobell/VelocityViewPager