实现Fragment的切换和ViewPager自动循环设置切换时间

1.FragmentActivity与Fragment之间的用法

2.实现ViewPager自动轮换,设置移动的时间

 通过反射获取mScrooler这个对象:

    Field mScroller;
            mScroller = ViewPager.class.getDeclaredField("mScroller");
            mScroller.setAccessible(true);
            FixedSpeedScroller scroller = new FixedSpeedScroller(advPager.getContext(), new LinearInterpolator());
            scroller.setmDuration(500);
            mScroller.set(advPager, scroller);

自定义Scroller类设置移动的时间:


public
class FixedSpeedScroller extends Scroller { private int mDuration = 5000; public FixedSpeedScroller(Context context) { super(context); } public FixedSpeedScroller(Context context, Interpolator interpolator) { super(context, interpolator); } @TargetApi(Build.VERSION_CODES.HONEYCOMB) public FixedSpeedScroller(Context context, Interpolator interpolator, boolean flywheel) { super(context, interpolator, flywheel); } @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 mDuration) { this.mDuration = mDuration; } }

附件下载:.zip