2

I have a parent Viewgroup with a child View inside of it. I'm am noticing some behavior during touch events that differs from my mental model of how touch events are dispatched.

Viewgroup A takes up the full screen and View B is programmatically added to A (using A.addView(B)) and takes up only part of the screen. For both A and B I am overriding the onTouchEvent(MotionEvent event) method, but nothing else.

If I touch my finger to the screen and lift it within the bounds of View B, I notice that the touch event is directly routed to B's onTouchEvent() method without ever hitting A's onTouchEvent() method. B receives the DOWN, MOVE and UP actions and returns true in for each action.

If I then touch my finger to the screen and lift within the bounds of Viewgroup A but outside of the bounds of View B then I see behavior that violates my mental model of how touch events are dispatched. When I do this View B again receives the DOWN, MOVE and UP actions in its onTouchEvent() even though B was not clicked. I would have expected A to receive the actions. If I then do the exact same motion with my finger (touch and lift inside of A but outside of B) then A's onTouchEvent() receives the DOWN, MOVE and UP actions as expected.

It's almost as if after one of the Views receives a touch event, it is setup by the system to receive the next touch regardless of the location of the touch.

Is there something basic I'm not understanding here?

Also, one of my resources mentions that I need to manually manage hit testing within my Viewgroup / Views to determine if when a viewgroup receives a hit if it should dispatch the hit down to a child or not. That makes sense to me, however, it seems like the system already does that for me in the first case I mention as the hit is automatically dispatched to the child. Which is the right way to do it, let the system handle it or handle the hit logic myself?

I used these resources to get me this far but they don't seem to explicitly cover this:

http://codetheory.in/understanding-android-input-touch-events/ https://guides.codepath.com/android/Gestures-and-Touch-Events

neonDion
  • 2,278
  • 2
  • 20
  • 39

0 Answers0