场景:
ViewPager左右滑动需要滑动动画,但在调用setCurrentItem()跨页面滑动时不需要显示滑动动画,要平滑静态滑动
解决:
重写ViewPager的setCurrentItem方法,当跨页面滑动时,设置duration(scroller.setmDuration(0);)
private FixedSpeedScroller scroller;
try {
Field field = ViewPager.class.getDeclaredField("mScroller");
field.setAccessible(true);
scroller = new FixedSpeedScroller(getContext(),
new DecelerateInterpolator());
field.set(this, scroller);
scroller.setmDuration(300);
} catch (Exception e) {
e.printStackTrace();
}
@Override
public void setCurrentItem(int item) {
if (Math.abs(getCurrentItem() - item) > 1) {
scroller.setmDuration(0);
super.setCurrentItem(item);
scroller.setmDuration(300);
} else {
scroller.setmDuration(300);
super.setCurrentItem(item);
}
}
PS:
来源:http://www.open-open.com/lib/view/open1476773958602.html
setCurrentItem(position,false),无动画,但界面显示异常,待解
本文介绍了在使用ViewPager时遇到的问题:跨页面调用setCurrentItem()方法时出现滑动动画。为解决这个问题,提出了通过重写setCurrentItem方法并在跨页跳转时设置duration为0来实现平滑静态滑动的方案。然而,使用setCurrentItem(position, false)虽然能禁用动画,但可能导致界面显示异常,这是一个待解决的问题。"
122178171,11222870,Unity3D游戏开发:血条实现与优缺点分析,"['Unity', '3d', 'C#', '游戏开发']

1191

被折叠的 条评论
为什么被折叠?



