ViewPager设置自定义滚动时间

public class ViewPagerScroller extends Scroller {
    private int mDuration = 1500;
    
    public ViewPagerScroller(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;
    }
}

下面直接调用上面的方法即可:

scroller.setmDuration(1500);是设置viewpager自定义滚动时间,根据自己的需求进行设置

try {
    Field field = ViewPager.class.getDeclaredField("mScroller");
    field.setAccessible(true);         ViewPagerScroller scroller = new ViewPagerScroller (mViewPager.getContext(),            new AccelerateInterpolator());
    field.set(mViewPager, scroller);
    scroller.setmDuration(1500);
} catch (Exception e) {

}

 

你可能感兴趣的:(Android)