Detailed explanation of Android event distribution mechanism

Android event distribution mechanism

We only consider the four most important touch events: down, move, up and cancel. A gesture is an event column, starting with a down event (generated when the user touches the screen), followed by 0 or more move events (generated when the user moves his finger around), and finally followed by a separate up or cancel event (generated when the user's finger leaves the screen or the system tells you that the gesture ends for other reasons). When we say "the rest of the gesture", we refer to the subsequent move event and the last up or cancel event of the gesture.

Here, I do not consider the multi touch gesture (we only assume to use one finger) and ignore the fact that multiple move events can be grouped into a group. Finally, let's assume that none of the views in this article are registered with ontouchlistener.

The view hierarchy we will discuss is as follows: the outermost layer is a ViewGroup a, which contains one or more child views (children), one of which is ViewGroup B, and ViewGroup B contains one or more child views, one of which is view C, which is not a ViewGroup. Here, we ignore the possible cross superposition between views at the same level.

Suppose that the point on the screen first touched by the user is a point on C, which is marked as a touch point, and the down event is generated at this point. Then the user moves his finger and finally leaves the screen. In this process, it doesn't matter whether the finger leaves the area of C. The key is where the gesture starts.

Default

Assuming that a, B and C above do not override the default event propagation behavior, the following is the process of event propagation:

Since no view cares about the gesture, they will no longer receive any events from the "rest of the gesture".

Handling events

Now, let's assume that C actually cares about the gesture. The reason may be that C is set to clickable or you override C's ontouchevent method.

Personal understanding: it can be seen from here that the processing of the down event by the ontouchevent method of each view represents the view's willingness to process the whole gesture starting from this down. Returning true means willing to process the gesture, and returning false means unwilling to process the gesture.

onInterceptTouchEvent

Now we will discuss a new method: onintercepttouchevent, which only exists in ViewGroup, but not in ordinary view. Before any ontouchevent of a view is called, its parents will get a chance to intercept the event. In other words, they can steal the event. In the "handling events" section just now, we have omitted this process. Now, let's add it:

Personal understanding: thanks to the reminder of @ programming world children, it can be seen that the processing of down event actually goes through two processes: one is a - > B's onintercepttouchevent, the other is C - > b - > A's ontouchevent. Of course, returning true in any method can prevent it from spreading.

Interception event

Now, let's go a step further and assume that B does not intercept the down event, but it intercepts the next move event. The reason may be that B is a scrolling view. When the user only taps in its area, the clicked element should be able to handle the click event. However, when the user's finger moves a certain distance, the gesture can no longer be regarded as a click - obviously, the user wants to scroll. This is why B takes over the gesture. The following is the sequence in which events are processed:

Here are some small things that may surprise you:

From then on, you can go further. For example, for the moushful method (I really don't know how to translate!) requestdisallowintercepttouchevent, C can use this method to prevent B from stealing events. If you want to be more crazy, you can directly override the dispatchtouchevent method in your own ViewGroup and do whatever you want to do with the passed in events. But in this case, you may break some agreements, so you should be careful.

If you have any questions, please leave a message or go to the community of this site for exchange and discussion. Thank you for reading. I hope it can help you. Thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>