2

我一直在创建一个Gallery使用有限数量的Views 的衍生产品,因此Adapter需要能够View在滚动或投掷期间提前填充这些 s。为此,我需要从onFling(...)onScroll(...)事件中获取运动方向。

如何使用distanceX参数 inonScroll(...)velocityX参数 inonFling(...)来确定Gallery行进的方式,以及View接下来要准备的方式?

4

1 回答 1

3

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 是一个相当不错的值——滚动仍然感觉很直观,但甩动不那么猛烈。

于 2011-11-24T15:59:05.370 回答