我正在使用VIEwPager子类MyPager(几乎相同),并且我使用其setCurrentItem(int index,boolean smooth)方法,并将smooth参数设置为true.它实际上比参数设置为’false’更平滑,但我想增加动画持续时间以使转换更加明显.
我从不同的帖子收集了一些信息,this solution看起来很完美.我最终得到了这段代码“
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)); }}
问题是,当通过行scrolller.set(this,mScroller)运行时,我无法摆脱这个异常; MyPager:
java.lang.IllegalArgumentException: invalID value for fIEld
任何的想法?
解决方法 你能修改这部分代码并插入一个Log语句吗?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()); }
然后发布输出?
编辑:问题是导入语句不正确. mScroller是一个androID.Widget.Scroller,你不能给它分配一个com.WazaBe.HoloEverywhere.Widget.Scroller.
总结以上是内存溢出为你收集整理的android – 增加viewpager smoothscroll持续时间全部内容,希望文章能够帮你解决android – 增加viewpager smoothscroll持续时间所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)