我有一个GestureDetector.OnGestureListener连接到ListView。我正在尝试在用户向下滚动时侦听滚动事件:
private GestureDetector.OnGestureListener mOnGesture = new GestureDetector.OnGestureListener() {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
}
}
但我看到一个非常奇怪的“distanceY”值流:
distanceY: -417.02252 // <-- huh?
e1: null
e2: MotionEvent{4097ac08 action=2 x=233.6657 y=417.02252 pressure=0.79999995 size=0.33333334}
distanceY: 1.5640259
e1: null
e2: MotionEvent{4097ac08 action=2 x=233.6657 y=415.4585 pressure=0.79999995 size=0.33333334}
distanceY: 1.5640259
e1: null
e2: MotionEvent{4097ac08 action=2 x=233.6657 y=413.89447 pressure=0.79999995 size=0.40000004}
... rest of values are positive, as expected ...
第一个“distanceY”值为负(意外),但后续的值均为正值。有什么可以解释这个?我每次都可以重现这种行为。
由于
答案 0 :(得分:0)
distanceY
是当前和之前onScroll
事件之间的距离。由于这是第一个这样的事件,我假设它从零开始计算。
distanceY: ____-417.02252____ // Same value as...
e1: null
e2: MotionEvent{4097ac08 action=2 x=233.6657 y=____417.02252____ // ...here...
答案 1 :(得分:0)
我有同样的问题。
但在我的情况下,我对onInterceptTouchEvent(...)和onTouchEvent(...)方法使用不同的OnGestureListener-。
因此我只是在拦截侦听器中设置一个标志,忽略传递给实际触摸事件监听器的第一个滚动事件
private class GestureInterceptListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
final boolean shouldIntercept = ...; // here we decide should we intercept this event, or should it be passed further
if (shouldIntercept) {
mGestureProcessListener.setFirstScrollEventIgnored(true);
}
return shouldIntercept;
}
}
private class GestureProcessListener extends GestureDetector.SimpleOnGestureListener {
private boolean ignoreFirstBuggyScrollEvent = false;
/**
* For some reason first scroll event on half-visible settings list gets strange incorrect negative values
* @param isIgnored - should next scroll event be ignored? value resets to false afterwards
*/
public void setFirstScrollEventIgnored(boolean isIgnored) {
ignoreFirstBuggyScrollEvent = isIgnored;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
if (ignoreFirstBuggyScrollEvent) {
ignoreFirstBuggyScrollEvent = false;
return true;
}
... // process scroll event here
return true;
}
}
为我工作