自定义Scroller
<span style="font-size:18px;">public class ViewPagerScroller extends Scroller { private int mDuration; public ViewPagerScroller(Context context) { super(context); } public ViewPagerScroller(Context context, Interpolator interpolator) { super(context, interpolator); } public void setDuration(int mDuration) { this.mDuration = mDuration; } @Override public void startScroll(int startX, int startY, int dx, int dy) { super.startScroll(startX, startY, dx, dy, this.mDuration); } @Override public void startScroll(int startX, int startY, int dx, int dy, int duration) { super.startScroll(startX, startY, dx, dy, this.mDuration); } }</span>
ViewPager.java源码内容里边有
private Scroller mScroller; void initViewPager() { setWillNotDraw(false); setDescendantFocusability(FOCUS_AFTER_DESCENDANTS); setFocusable(true); final Context context = getContext(); mScroller = new Scroller(context, sInterpolator); ......
私有属性没有对外提供接口那么就用反射吧
private void setViewPagerScrollSpeed(ViewPager viewPager, int speed) { try { Field field = ViewPager.class.getDeclaredField("mScroller"); field.setAccessible(true); ViewPagerScroller viewPagerScroller = new ViewPagerScroller(viewPager.getContext(), new OvershootInterpolator(0.6F)); field.set(viewPager, viewPagerScroller); viewPagerScroller.setDuration(speed); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } }这样就可以设置滑动切换速度了!