Increase the length of smooth viewing of the viewer

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; /** * Override the Scroller instance with our own class so we can change the * duration */ 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()); } } /** * Set the factor by which the duration will change */ 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); } /** * Set the factor by which the duration will change */ 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?

+6
android android-viewpager android-animation
Jan 08 '13 at 13:13
source share
1 answer

Can you modify this part of the code and insert the log statement?

 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)); Log.d("TAG", "mScroller is: " + mScroller + ", " + mScroller.getClass().getSuperclass().getCanonicalName() + "; this class is " + this + ", " + getClass().getSuperclass().getCanonicalName()); scroller.set(this, mScroller); } catch (Exception e) { Log.e("MyPager", e.getMessage()); } 

And then publish the conclusion?

EDIT : the problem in the import statements is incorrect. mScroller is android.widget.Scroller , and you cannot assign com.WazaBe.HoloEverywhere.widget.Scroller to it.

+6
Jan 08 '13 at 16:11
source share



All Articles