Skip to content

Instantly share code, notes, and snippets.

@tcw165
Last active March 21, 2017 01:06
Show Gist options
  • Save tcw165/84629d3e2e71d1ce2e57d72cfc5537d9 to your computer and use it in GitHub Desktop.
Save tcw165/84629d3e2e71d1ce2e57d72cfc5537d9 to your computer and use it in GitHub Desktop.
private void cancelChildrenTouchEvent(MotionEvent event) {
// MotionEvent.obtain creates a new MotionEvent`, copying from an existing one.
final MotionEvent canceledEvent = MotionEvent.obtain(event);
// The event was handled by the certain child view. In order to make the touch event
// lifecycle complete, we have to fake a ACTION_CANCEL for the child view.
// Otherwise, it would be chaos.
canceledEvent.setAction(MotionEvent.ACTION_CANCEL);
super.dispatchTouchEvent(canceledEvent);
canceledEvent.recycle();
// Fake our touch event for the same reason.
event.setAction(MotionEvent.ACTION_DOWN);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment