我一直在使用有限数量的Gallery
创建View
衍生产品,因此,Adapter
需要能够填充这些View
个在滚动或投掷期间提前。为此,我需要从onFling(...)
和onScroll(...)
事件中获取动作方向。
如何使用distanceX
中的onScroll(...)
参数和velocityX
中的onFling(...)
参数来确定Gallery
的旅行方式,以及View
准备下一个?
答案 0 :(得分:3)
onFling(...)
中的速度参数和onScroll(...)
上的距离参数的符号相反。为了正确确定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是一个相当不错的价值 - 它仍然感觉直观滚动但是飘动不那么暴力。