8

我需要检测列表视图中的水平滑动。我应该使用手势检测器还是 onTouch 事件。我需要支持Android 2.1+

一篇文章指出需要在 ListView 中覆盖 onInterceptTouchEvent,如下所示:

 @Override public boolean onInterceptTouchEvent(MotionEvent ev) { 
    switch (ev.getAction()) {  case   MotionEvent.ACTION_DOWN: // reset difference values mDiffX = 0; mDiffY = 0;

        mLastX = ev.getX();
        mLastY = ev.getY();
        break;

    case MotionEvent.ACTION_MOVE:
        final float curX = ev.getX();
        final float curY = ev.getY();
        mDiffX += Math.abs(curX - mLastX);
        mDiffY += Math.abs(curY - mLastY);
        mLastX = curX;
        mLastY = curY;

        // don't intercept event, when user tries to scroll vertically
        if (mDiffX > mDiffY) {
            return false; // do not react to horizontal touch events, these events will be passed to your list item view
        }
}

return super.onInterceptTouchEvent(ev);
}

不确定这是否有效或者是最好的方法。这个问题在 8 天前被无意识地关闭并没有帮助: 在 ListView 中滑动手势 - Android

4

0 回答 0