0

设置Viewpager的viewPager.setCurrentItem有一个动画滑动效果

设置Viewpager的viewPager.setCurrentItem(currentIndex+1);有一个动画滑动效果

ViewPagerScroller scroller = new ViewPagerScroller(this);
scroller.initViewPagerScroll(viewPager);

/**
* ViewPager 滚动速度设置
*
*/
public class ViewPagerScroller extends Scroller {
private int mScrollDuration = 2000; // 滑动速度

/**
* 设置速度速度
*
* @param duration
*/
public void setScrollDuration(int duration) {
this.mScrollDuration = duration;
}

public ViewPagerScroller(Context context) {
super(context);
}

public ViewPagerScroller(Context context, Interpolator interpolator) {
super(context, interpolator);
}

@SuppressLint(“NewApi”)
public ViewPagerScroller(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, mScrollDuration);
}

@Override
public void startScroll(int startX, int startY, int dx, int dy) {
super.startScroll(startX, startY, dx, dy, mScrollDuration);
}

public void initViewPagerScroll(ViewPager viewPager) {
try {
Field mScroller = ViewPager.class.getDeclaredField(“mScroller”);
mScroller.setAccessible(true);
mScroller.set(viewPager, this);
} catch (Exception e) {
e.printStackTrace();
}
}
}

天边的星星