增加viewpager smoothscroll时间 [英] Increase viewpager smoothscroll duration

查看:502
本文介绍了增加viewpager smoothscroll时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了一个 ViewPager MyPager (这几乎是一样的),我用它的 setCurrentItem(INT指数,布尔光滑)的方法,与光滑参数设置为true。它实际上比参数设置为假,更顺畅一点,但我想增加动画的持续时间,以使过渡更加明显。

I'm using a ViewPager subclass MyPager (which is almost the same), and I use its setCurrentItem(int index, boolean smooth) method, with the smooth parameter set to true. It's actually a little smoother than with parameter set to 'false', but I'd like to increase the animation duration to make the transition more visible.

我已经收集来自不同的岗位,一些信息<一个href="http://stackoverflow.com/questions/10812009/change-viewpager-animation-duration-when-sliding-programmatically/14179739#comment19704416_14179739">this解决方案看起来很完美。我已经结束了这个code

I have gathered some information from different posts and this solution looks perfect. I've ended up with this code"

MyPager.java:

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

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));
    }

}

的事情是,我无法摆脱这种异常,经行 scroller.set(这一点,mScroller)运行时; MyPager的:

The thing is, I can't get rid of this exception, when running through the line scroller.set(this, mScroller); of MyPager :

java.lang.IllegalArgumentException: invalid value for field

你知道吗?

推荐答案

你能修改这部分的code和插入登录声明?

Can you modify this part of the code and insert a 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 post the output?

修改:这个问题是import语句是不正确的。 mScroller android.widget.Scroller ,你不能分配给它的 com.WazaBe .HoloEverywhere.widget.Scroller

EDIT: The issue is the import statements are not correct. mScroller is a android.widget.Scroller and you cannot assign to it a com.WazaBe.HoloEverywhere.widget.Scroller.

这篇关于增加viewpager smoothscroll时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆