Viewpager setcurrentitem动画

时间:2015-03-15 02:36:15

标签: android android-viewpager

(我的观看者)

public class MyViewPager extends ViewPager 
{
  public MyViewPager( Context context, AttributeSet attrs) 
  {
    super( context, attrs );
    setMyScroller();
  }
  private void setMyScroller() 
  {
    try 
    {
            Class<?> viewpager = ViewPager.class;
            Field scroller = viewpager.getDeclaredField("mScroller");
            scroller.setAccessible(true);
            scroller.set(this, new MyScroller(getContext()));
    } catch (Exception e) 
    {
        e.printStackTrace();
    }
  }

  public class MyScroller extends Scroller 
  {
    public MyScroller(Context context) 
    {
        super(context, new DecelerateInterpolator());
    }

    @Override
    public void startScroll(int startX, int startY, int dx, int dy, int duration) 
    {
        super.startScroll(startX, startY, dx, dy, 5000);
    }
  }
} 

(proguard-rules.pro)

-keepclassmembers class android.support.v4.view.ViewPager { private android.widget.Scroller mScroller; }

我知道我给了5秒。当我用手指改变页面时,页面变为慢速(5秒)

当我在方法中更改页面时:setCurrentItem(1,true);页面变化太快了。 我使用android.support.v4.view.ViewPager

那我怎么能解决这个问题?

1 个答案:

答案 0 :(得分:1)

您无需扩展Viewpager以进行scrollTime更改..此

private void setScrollSpeedCustomViewPager(ViewPager pager, int duration) {
        try {
            Field mScroller;
    mScroller = ViewPager.class.getDeclaredField("mScroller");
            mScroller.setAccessible(true);
            FixedSpeedScroller scroller = new FixedSpeedScroller(
                    pager.getContext(), new LinearInterpolator(), duration,
                    true);
            // scroller.setFixedDuration(5000);
            mScroller.set(pager, scroller);
        } catch (NoSuchFieldException e) {
        } catch (IllegalArgumentException e) {
        } catch (IllegalAccessException e) {
        }
    }

并从oncreate set viewpagerscrollspeed这样 - setScrollSpeedCustomViewPager(pager,5000);