设置ViewPager的滑动速度

用按钮的点击事件切换ViewPager到某一个子页面,我们一般使用

mViewPager.setCurrentItem(positon),这时你会发现ViewPager快速的闪到你所设置的页面,如果我们要控制这个切换过程有个过渡效果,该怎么办呢

 

自定义一个Scroller类,控制ViewPager的滑动速度

 

import android.content.Context;
import android.view.animation.Interpolator;
import android.widget.Scroller;

public class SpeedScroller extends Scroller {
	private int mDuration = 300;

	public SpeedScroller(Context context) {
		super(context);
	}

	public SpeedScroller(Context context, Interpolator interpolator) {
		super(context, interpolator);
	}

	@Override
	public void startScroll(int startX, int startY, int dx, int dy, int duration) {
		// Ignore received duration, use fixed one instead
		super.startScroll(startX, startY, dx, dy, mDuration);
	}

	@Override
	public void startScroll(int startX, int startY, int dx, int dy) {
		// Ignore received duration, use fixed one instead
		super.startScroll(startX, startY, dx, dy, mDuration);
	}

	public void setmDuration(int time) {
		mDuration = time;
	}

	public int getmDuration() {
		return mDuration;
	}

}

通过反射机制设置ViewPager的滑动速度

ViewPager mViewPager= (ViewPager) findViewById(R.id.mViewPager);
try {
<span style="white-space:pre">	</span>Field mField = ViewPager.class.getDeclaredField("mScroller");
	mField.setAccessible(true);
	SpeedScroller mScroller =new SpeedScroller
 	(mViewPager.getContext(),new AccelerateInterpolator());
	mField.set(mViewPager,mScroller);
} catch (Exception e) {
<span style="white-space:pre">	</span>e.printStackTrace();
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值