I need to detect a horizontal swipe within a list view. Should I use a gesture detector or onTouch event. I need to support Android 2.1+
One post indicated the need to override onInterceptTouchEvent in the ListView like below:
@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);
}
Not sure if this works or is the best way however. It did not help that the question was mindlessly closed 8 days ago: Swipe Gesture inside ListView - Android