我正在使用 v4 ViewPager 并点击以下链接能够 使用插值器设置滚动速度和滚动动画。
Slowing speed of Viewpager controller in android
当在构造函数中这样问时,我使用了线性插值器
private void setScroller() {
try {
Field mScroller;
mScroller = ViewPager.class.getDeclaredField("mScroller");
mScroller.setAccessible(true);
FixedSpeedScroller scroller =
new FixedSpeedScroller(this.getContext(), new LinearInterpolator());
mScroller.set(this, scroller);
} catch (NoSuchFieldException e) {
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
}
}
现在我的 viewpages 中有很多页面的 View 。在它下面我添加了 pagerindicator 由点组成。
O O O O O O O
按一个点可以使用 smoothScrolling 设置当前页面。 使用触摸事件我可以获得我需要显示的正确页码。 知道这一点后,我调用此方法进行平滑滚动。
public void snapToPage(int whichPage) {
int numperOfSwapsRequired = whichPage - getCurrentPageIndex();
mScrollingDuration *= numperOfSwapsRequired;
if(numperOfSwapsRequired == 0) return;
if(numperOfSwapsRequired > 0) {
for(int i = 0; i<numperOfSwapsRequired; i++){
setCurrentItem(getCurrentItem()+1, true);
}
} else {
for(int i = 0; i>numperOfSwapsRequired; i--){
setCurrentItem(getCurrentItem()-1, true);
}
}
mScrollingDuration = 300;
}
现在一切正常。但只有当我选择一个与所选页面点正确的点时。它以线性动画正确滚动。
但是当我选择留给所选页面点的点时。它不滚动。它只是直接跳转到没有滚动动画的页面。
所以我很困惑如何解决这个问题。这与插值器或滚动器有关吗?
如有任何帮助,我们将不胜感激。
谢谢
最佳答案
ahhhhh... 到头来总是会犯下一个愚蠢但很小的错误。 但非常感谢我捕获了它。我什至开始阅读 Scroller Google 代码文件 :)
mScrollingDuration *= numperOfSwapsRequired;
当我们从右向左滚动导致动画时间为零时,此数字为负数。
这就解决了
mScrollingDuration *= Math.abs(numperOfSwapsRequired);
关于java - 当 ViewPager 从右向左滚动时,LinearInterpolator 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13196158/