Here we look at the ViewPager extension and ViewPager most of what the pager will do internally, combined with the scrolling logic from the Gallery widget. The general idea is to control the outlier (both speed and accompanying scrolls) and then feed them as fake shuffle events to the base ViewPager . If you do this alone, it won’t work (you still get only one page scroll). This is because fake drag and drop implements caps at borders that scrolls will be effective. You can simulate the calculations in the advanced ViewPager and detect when this happens, and then just turn the page and continue as usual. The advantage of using fake drag and drop means that you don’t have to deal with page snapping or edge processing of ViewPager .
I tested the following animation demo example code, downloaded from http://developer.android.com/training/animation/screen-slide.html , replacing ViewPager with ScreenSlideActivity with this VelocityViewPager (both in layout activity_screen_slide and field inside Activity).
package com.example.android.animationsdemo; import android.content.Context; import android.support.v4.view.ViewPager; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.GestureDetector; import android.widget.Scroller; public class VelocityViewPager extends ViewPager implements GestureDetector.OnGestureListener { private GestureDetector mGestureDetector; private FlingRunnable mFlingRunnable = new FlingRunnable(); private boolean mScrolling = false; public VelocityViewPager(Context context) { super(context); } public VelocityViewPager(Context context, AttributeSet attrs) { super(context, attrs); mGestureDetector = new GestureDetector(context, this); }
There are a few minor problems with this that can be easily solved, but I will leave it to you, namely, things like if you scroll (drag and drop, and not drop), you may be halfway between the pages (you will want to bind to the ACTION_UP event) . In addition, touch events are completely redefined for this, so you will need to associate the corresponding events with the underlying ViewPager , if necessary.
Dororo Feb 19 '13 at 23:54 2013-02-19 23:54
source share