问题描述
有没有什么办法可以减缓滚动速度,在Android上viewpager适配器?
Is there any way to slow the scroll speed with the viewpager adaptor in android?
您知道吗,我一直在看这个code。我想不出什么,我错了洞。
You know, I've been looking at this code. I can't figure out what I'm dong wrong.
try{
Field mScroller = mPager.getClass().getDeclaredField("mScroller");
mScroller.setAccessible(true);
Scroller scroll = new Scroller(cxt);
Field scrollDuration = scroll.getClass().getDeclaredField("mDuration");
scrollDuration.setAccessible(true);
scrollDuration.set(scroll, 1000);
mScroller.set(mPager, scroll);
}catch (Exception e){
Toast.makeText(cxt, "something happened", Toast.LENGTH_LONG).show();
}
它不会改变任何事情没有发生异常?
It doesn't change anything yet no exceptions occur?
推荐答案
我已经开始与这的确改变了mScroller场(这是一个很好的开始)HighFlyer的code,但没有帮助延长的期限由于滚动请求ViewPager滚动时的持续时间明确地传递给mScroller。
I've started with HighFlyer's code which indeed changed the mScroller field (which is a great start) but didn't help extend the duration of the scroll since ViewPager explicitly pass the duration to the mScroller when requesting to scroll.
扩展ViewPager没有工作的重要方法(smoothScrollTo)不能被覆盖。
Extending ViewPager didn't work as the important method (smoothScrollTo) can't be overridden.
我结束了与此code延长滚轮固定的:
I ended up fixing this by extending Scroller with this code:
public class FixedSpeedScroller extends Scroller {
private int mDuration = 5000;
public FixedSpeedScroller(Context context) {
super(context);
}
public FixedSpeedScroller(Context context, Interpolator interpolator) {
super(context, interpolator);
}
public FixedSpeedScroller(Context context, Interpolator interpolator, boolean flywheel) {
super(context, interpolator, flywheel);
}
@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);
}
}
和使用它是这样的:
try {
Field mScroller;
mScroller = ViewPager.class.getDeclaredField("mScroller");
mScroller.setAccessible(true);
FixedSpeedScroller scroller = new FixedSpeedScroller(mPager.getContext(), sInterpolator);
// scroller.setFixedDuration(5000);
mScroller.set(mPager, scroller);
} catch (NoSuchFieldException e) {
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
}
我已经基本上很难codeS的持续时间为5秒,使我ViewPager使用它。
I've basically hard codes the duration to 5 seconds and made my ViewPager use it.
希望这有助于。
这篇关于在Android的Viewpager控制器的速度放缓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!