一、自定义一个Scroll类,用于控制ViewPager滑动速度:
import android.view.animation.Interpolator;
import android.widget.Scroller;
public class FixedSpeedScroller extends Scroller {
private int mDuration = 0;
public FixedSpeedScroller(Context context) {
super(context);
}
public FixedSpeedScroller(Context context, Interpolator interpolator) {
super(context, interpolator);
}
public FixedSpeedScroller(Context context, Interpolator interpolator, boolean flywheel) {
super(context, interpolator, flywheel);
}
@Override
public void startScroll(int startX, int startY, int dx, int dy, int duration) {
super.startScroll(startX, startY, dx, dy, mDuration);
}
@Override
public void startScroll(int startX, int startY, int dx, int dy) {
super.startScroll(startX, startY, dx, dy, mDuration);
}
}
二、在初始化ViewPager时,对ViewPager作如下设置:
* 设置ViewPager的滑动速度
*
* */
private void setViewPagerScrollSpeed( ){
try {
Field mScroller = null;
mScroller = ViewPager.class.getDeclaredField("mScroller");
mScroller.setAccessible(true);
FixedSpeedScroller scroller = new FixedSpeedScroller( mViewPager.getContext( ) );
mScroller.set( mViewPager, scroller);
}catch(NoSuchFieldException e){
}catch (IllegalArgumentException e){
}catch (IllegalAccessException e){
}
}