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
The technique here is to extends
ViewPagerand mimic most of what the pager will be doing internally, coupled with scrolling logic from theGallerywidget. 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 extendedViewPagerand 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
ScreenSlideActivitywith thisVelocityViewPager(both in the layoutactivity_screen_slideand 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
ViewPagerwhere appropriate.