I use a subclass of ViewPager MyPager (which is pretty much the same), and I use its setCurrentItem(int index, boolean smooth) method, with the smooth parameter setting to true. This is actually a little smoother than with the false parameter, but I would like to increase the duration of the animation to make the transition more noticeable.
I collected some information from different posts and this solution looks perfect. I ended up with this code "
MyPager.java:
public class MyPager extends ViewPager { public MyPager(Context context, AttributeSet attrs) { super(context, attrs); this.enabled = true; postInitViewPager(); } private ScrollerCustomDuration mScroller = null; private void postInitViewPager() { try { Class<?> viewpager = ViewPager.class; Field scroller = viewpager.getDeclaredField("mScroller"); scroller.setAccessible(true); Field interpolator = viewpager.getDeclaredField("sInterpolator"); interpolator.setAccessible(true); mScroller = new ScrollerCustomDuration(getContext(), (Interpolator) interpolator.get(null)); scroller.set(this, mScroller); } catch (Exception e) { Log.e("MyPager", e.getMessage()); } } public void setScrollDurationFactor(double scrollFactor) { mScroller.setScrollDurationFactor(scrollFactor); } }
ScrollerCustomDuration.java
public class ScrollerCustomDuration extends Scroller { private double mScrollFactor = 2; public ScrollerCustomDuration(Context context) { super(context); } public ScrollerCustomDuration(Context context, Interpolator interpolator) { super(context, interpolator); } public ScrollerCustomDuration(Context context, Interpolator interpolator, boolean flywheel) { super(context, interpolator, flywheel); } public void setScrollDurationFactor(double scrollFactor) { mScrollFactor = scrollFactor; } @Override public void startScroll(int startX, int startY, int dx, int dy, int duration) { super.startScroll(startX, startY, dx, dy, (int) (duration * mScrollFactor)); } }
The fact is that I can not get rid of this exception when passing the line scroller.set(this, mScroller); MyPager:
java.lang.IllegalArgumentException: invalid value for field
Any idea?
android android-viewpager android-animation
elgui Jan 08 '13 at 13:13 2013-01-08 13:13
source share