owen33519
owen33519

Reputation: 11

How does onInterceptTouchEvent work for two overlapping ViewGroup?

I may misunderstand the concept of onInterceptTouchEvent and need you to help.

So if I have the ViewGroup A and ViewGroup B which is overlapping with ViewGroup A, I want to know how onInterceptTouchEvent is invoked.

The view hierarchy is:

Activity -> ViewGroup A -> View A -> ViewGroup B -> View B

So both ViewGroup A and ViewGroup B are created under the same activity. ViewGroup B is overall overlapping ViewGroup A. View B is also overlapping View A. Now, if I tap View B, what is order of onInterceptTouchEvent?

My understanding is the order of onInterceptTouchEvent is Activity -> ViewGroup A -> View A -> ViewGroup B -> View B (see the photo), but the actually testing shows the order is Activity -> ViewGroup B -> View B. If the touch event isn't pass through ViewGroup B and View B, onInterceptTouchEvent on ViewGroup A and View A wouldn't be invoked. Can someone help me understand which order is correct and what the correct concept of onInterceptTouchEvent is? Thanks for helping!

Upvotes: 1

Views: 92

Answers (1)

Gabe Sechan
Gabe Sechan

Reputation: 93569

The answer to that is z-ordering. z ordering is basically "what's on top", the z refers to the z axis of a Cartesian plane. When a ViewGroup lays out its children, it lays out 1 element then the next. The second one is on top, and will draw over the other. That's the one that gets the touch commands first. If everything in the view group returns false and says it doesn't handle the touch, then it would fall through to the view group underneath it. That's for the actual onTouch commands

For intercept calls, each parent in the view's chain has a chance to intercept. This is not based on visual layout, but by view group parent chains. It won't call the other parallel view group at all, because it isn't in a parent child relationship. It would try all of View B, then if all return false ViewB would get the touch. If view B's onTouch returns false, then the touch would go to the next view in line (which would have its own intercept chain).

Upvotes: 0

Related Questions