我需要检测列表视图中的水平滑动。我应该使用手势检测器还是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天前,这个问题被无意识地关闭,这无关紧要:Swipe Gesture inside ListView - Android