更改ViewPager和setCurrentItem的转换速度

我有一个ViewPager,我想每5秒自动旋转一次,同时也允许用户滑动。 但是,当我设置自动更改时,页面之间的过渡动​​画发生得非常快,我希望它更慢。

我在这里看到了这个问题的答案

在Android中Viewpager控制器的速度减慢

但他们都使用reflection。 有没有人知道如何在不使用reflection的情况下减慢ViewPager的自动速度?

我想过使用PageTransformer,但不确定它是否会起作用,如果用户滑动和自动滑动,它可能会影响速度? 除非我能以某种方式检测到差异,然后做一个或另一个PageTransformation?

使用此自定义View

 public class ViewPagerCustomDuration extends ViewPager { private FixedSpeedScroller mScroller = null; public ViewPagerCustomDuration(Context context) { super(context); init(); } public ViewPagerCustomDuration(Context context, AttributeSet attrs) { super(context, attrs); init(); } /* * Override the Scroller instance with our own class so we can change the * duration */ private void init() { try { Class< ?> viewpager = ViewPager.class; Field scroller = viewpager.getDeclaredField("mScroller"); scroller.setAccessible(true); mScroller = new FixedSpeedScroller(getContext(), new DecelerateInterpolator()); scroller.set(this, mScroller); } catch (Exception ignored) { } } /* * Set the factor by which the duration will change */ public void setScrollDuration(int duration) { mScroller.setScrollDuration(duration); } private class FixedSpeedScroller extends Scroller { private int mDuration = 500; public FixedSpeedScroller(Context context) { super(context); } public FixedSpeedScroller(Context context, Interpolator interpolator) { super(context, interpolator); } 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 setScrollDuration(int duration) { mDuration = duration; } } } 

使用schedule timer选项自动旋转页面。

 int i = 0; static Timer timer = new Timer(); //Scroller scroll = new Scroller(this); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.simple_circles); mAdapter = new TestFragmentAdapter(getSupportFragmentManager()); mPager = (ViewPager) findViewById(R.id.pager); mPager.setAdapter(mAdapter); mIndicator = (CirclePageIndicator) findViewById(R.id.indicator); mIndicator.setViewPager(mPager); mPager.setCurrentItem(i); SwitchPage(3); } public void SwitchPage(int seconds) 

{

 if(timer != null) { timer.cancel(); } timer = new Timer(); // At this line a new Thread will be created timer.schedule(new SwitchPageTask(), 2000, seconds * 2000); // delay in milliseconds } class SwitchPageTask extends TimerTask { @Override public void run() { runOnUiThread(new Runnable() { public void run() { if(i < mAdapter.getCount()) { i++; mPager.setCurrentItem(i, true); } else { i=0; mPager.setCurrentItem(i, true); } } }); } } just cancel the timer like.. timer.cancel().. or cancel timer from another activity or fragment activity.. textView1_page3.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (SampleCirclesDefault.timer != null) { SampleCirclesDefault.timer.cancel(); Toast.makeText(getActivity(), "timer cancled", 1).show(); } } });