0

How do I block onTouchEvent from a view's siblings? I have a ViewGroup with a custom button in it. When the button receives an OnTouchEvent, I want to block further OnTouchEvents from going to the button's siblings. I do want to continue receiving OnTouchEvent in the button.

a.bertucci
  • 12,142
  • 2
  • 31
  • 32
Peri Hartman
  • 19,314
  • 18
  • 55
  • 101
  • Is your implementation of onTouchEvent() returning true (for the custom button)? – James Apr 21 '13 at 20:25
  • Yes, it returns true, though not on the first event. It waits till after a onLongClick comes through. That much seems to be working in that it allows a parent ViewPager to receive events as long as they happen right away. – Peri Hartman Apr 21 '13 at 20:54
  • You could take a look at: http://developer.android.com/reference/android/view/ViewGroup.html#onInterceptTouchEvent(android.view.MotionEvent). – James Apr 21 '13 at 21:02

1 Answers1

1

I think you should use a custom ViewGroup as well. In order to get what you want you should override the onInterceptTouchEvent() method, like stated here:

The onInterceptTouchEvent() method gives a parent the chance to see any touch event before its children do. If you return true from onInterceptTouchEvent(), the child view that was previously handling touch events receives an ACTION_CANCEL, and the events from that point forward are sent to the parent's onTouchEvent() method for the usual handling. onInterceptTouchEvent() can also return false and simply spy on events as they travel down the view hierarchy to their usual targets, which will handle the events with their own onTouchEvent().

Basically, according to your own logic (button onTouchEvent, etc), you should instruct your ViewGroup.onInterceptTouchEvent() to return true if MotionEvent raw coords are not included in the visible rect of your button.

a.bertucci
  • 12,142
  • 2
  • 31
  • 32
  • I've tried that. It blocks further events to the button as well as the siblings. That breaks my button! – Peri Hartman Apr 21 '13 at 21:46
  • I'm going to post a different question - what I'm really trying to do is prevent siblings from receving onDrag. I think I'm on the wrong approach looking at MotionEvents. – Peri Hartman Apr 21 '13 at 21:47