时间:2021-08-16 09:22:12 | 栏目:Android代码 | 点击:次
1、自定义Scroller
public class ViewPageFixedSpeedScroller extends Scroller { private int mDuration = 300; public ViewPageFixedSpeedScroller(Context context) { super(context); // TODO Auto-generated constructor stub } public ViewPageFixedSpeedScroller(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; } }
2、通过反射修改viewpage 的scoller<pre name="code" class="java">try { //通过反射找到当前class 文件里的mScroller的mScrooller对象 //getDeclaredField是可以获取一个类的所有字段. //getField只能获取类的public 字段. Field mField = ViewPager.class.getDeclaredField("mScroller"); mField.setAccessible(true);//设置了private属性的对象也能获取的 Scroller mScroller = new ViewPageFixedSpeedScroller(page.getContext(), new LinearInterpolator()); mField.set(page, mScroller); } catch (Exception e) { e.printStackTrace(); } </pre><br> <pre></pre> <pre name="code" class="java"></pre><br>
总结