欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ViewPager设置自定义滚动时间

程序员文章站 2022-06-09 15:55:30
...
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) {

}

 

相关标签: ViewPager