我一直在创建一个Gallery
使用有限数量的View
s 的衍生产品,因此Adapter
需要能够View
在滚动或投掷期间提前填充这些 s。为此,我需要从onFling(...)
和onScroll(...)
事件中获取运动方向。
如何使用distanceX
参数 inonScroll(...)
和velocityX
参数 inonFling(...)
来确定Gallery
行进的方式,以及View
接下来要准备的方式?
onFling(...)
速度参数 in和距离参数 on的符号onScroll(...)
相反。为了正确判断a在哪个方向Gallery
移动,代码应该如下:
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
//if distanceX is POSITIVE, the Views are travelling left
//therefore the selection position is INCREASING
return super.onScroll(e1, e2, distanceX*mVelocityFactor, distanceY*mVelocityFactor);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
//if velocityX is NEGATIVE, the Views are travelling left
//therefore the selection position is DECREASING
return super.onFling(e1, e1, velocityX*mVelocityFactor, velocityY*mVelocityFactor);
}
顺便说一句,mVelocityFactor
这只是我引入的一个常数,以使滚动/甩动不那么精力充沛。我发现 0.6 是一个相当不错的值——滚动仍然感觉很直观,但甩动不那么猛烈。