5

我有一个连接到 ListView 的 GestureDetector.OnGestureListener。当用户向下滚动时,我正在尝试监听滚动事件:

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”值是负数(出乎意料),但随后的都是正数。这有什么可以解释的?我每次都可以重现这种行为。

谢谢

4

2 回答 2

1

distanceY是当前事件和上一个事件之间的距离onScroll。由于这是第一个此类事件,我假设它从零开始计算。

distanceY: ____-417.02252____  // Same value as...
e1: null
e2: MotionEvent{4097ac08 action=2 x=233.6657 y=____417.02252____ // ...here...
于 2012-09-17T19:13:58.833 回答
0

我有同样的问题。

但就我而言,我对 onInterceptTouchEvent(...) 和 onTouchEvent(...) 方法使用不同的 OnGestureListener-s。

因此,我只是在拦截侦听器中设置了一个标志,以忽略传递给实际触摸事件侦听器的第一个滚动事件

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;
    }

}

为我工作

于 2017-02-24T16:09:47.937 回答