点击打开链接
我们都知道,当我们使用setCurrentItem方法切换ViewPager的页面时,会非常迅速,那么我们该如何改变这个速度呢。
因为Google官方将速度写死了,所以我们需要通过反射机制来修改
底下修改的代码:
public class FixedSpeedScroller extends Scroller {
private int mDuration = 300;
public FixedSpeedScroller(Context context) {
super(context);
}
public FixedSpeedScroller(Context context, Interpolator interpolator) {
super(context, interpolator);
}
@Override
public void startScroll(int startX, int startY, int dx, int dy, int duration) {
// Ignore received duration, use fixed one instead
super.startScroll(startX, startY, dx, dy, mDuration);
}
@Override
public void startScroll(int startX, int startY, int dx, int dy) {
// Ignore received duration, use fixed one instead
super.startScroll(startX, startY, dx, dy, mDuration);
}
public void setmDuration(int time) {
mDuration = time;
}
public int getmDuration() {
return mDuration;
}
}
在使用viewpager的时候调用该方法
try {
Field field = ViewPager.class.getDeclaredField("mScroller");
field.setAccessible(true);
FixedSpeedScroller scroller = new FixedSpeedScroller(viewPager.getContext(),
new DecelerateInterpolator());
field.set(viewPager, scroller);
scroller.setmDuration(2000);
} catch (Exception e) {
}