Understanding of the event distribution mechanism

Source: Internet
Author: User

There are only two main characters in touch event distribution: ViewGroup and view. The activity's touch event is actually called the ViewGroup touch event inside it, and can be treated directly as ViewGroup.

View within the ViewGroup, ViewGroup can also be in other viewgroup, this time the internal viewgroup as a view to analyze.

There are three related events in ViewGroup: Onintercepttouchevent, Dispatchtouchevent, Ontouchevent. There are only two related events for view: Dispatchtouchevent, Ontouchevent.

First analyze the ViewGroup process: first, there must be a structural model concept: ViewGroup and view constitute a tree structure, the top of the activity of the viewgroup, there are a number of viewgroup nodes, There are several ViewGroup nodes or view nodes under each node, and so on.

When a touch event (for example, a touch event) reaches the root node, the acitivty ViewGroup, it is issued sequentially, and the process is implemented by invoking the Dispatchtouchevent method of the Child view (ViewGroup). Simply put, viewgroup iterates through the child view it contains, invokes the Dispatchtouchevent method of each view, and when the child view is ViewGroup, The Dispatchtouchevent method of its internal view is resumed by calling Viwgroup's Dispatchtouchevent method. The order of the messages in the above example is this: ①-②-⑤-⑥-⑦-③-④. The Dispatchtouchevent method is only responsible for the distribution of the event, which has a Boolean return value, and when returned to true, the order is interrupted. In the above example, if ⑤ 's Dispatchtouchevent returns True, ⑥-⑦-③-④ will not receive this touch event.

A summary is:

There are only two characters in the 1.Touch event distribution: ViewGroup and view. ViewGroup contains three related events for Onintercepttouchevent, Dispatchtouchevent, Ontouchevent. View contains Dispatchtouchevent, ontouchevent two related events. Where ViewGroup is inherited from view.

The 2.ViewGroup and view form a tree structure, and the root node is a viwgroup contained within the activity.

3. Touch events consist of Action_down, Action_move, aciton_up, one complete touch event, down and up are only one, Move has several, can be 0.

4. When acitivty receives a touch event, it traverses the sub view to distribute the down event. The traversal of ViewGroup can be seen as recursive. The purpose of the distribution is to find the view that really deals with this complete touch event, which returns true on the ontouchuevent result.

5. When a child view returns true, the distribution of the down event is aborted, and the child view is recorded in the ViewGroup. The next move and up event will be processed directly by the child view. Because child view is a VIEWGROUP node structure saved in ViewGroup, the ancestor ViewGroup holds the ViewGroup object where the view of the real processing event is located: As in the structure of Viewgroup0-viewgroup1-textview, TextView returns True, it is saved in ViewGroup1, and ViewGroup1 returns True, which is saved in ViewGroup0. When the move and up events come, they are passed from ViewGroup0 to ViewGroup1 and then ViewGroup1 to TextView.

6. When all child view in ViewGroup does not capture the down event, the ViewGroup itself's Ontouch event is triggered. The trigger is to invoke the Super.dispatchtouchevent function, which is the Dispatchtouchevent method of the parent view. Triggers the Ontouchevent method of the acitivity in cases where all sub-view is not processed.

7.onInterceptTouchEvent has two functions: 1. Intercept the distribution of the down event. 2. Abort the up and move events to the target view so that the viewgroup of the target view captures the up and move events.

Add:

"Touch events consist of Action_down, Action_move, ACITON_UP, and one complete touch event, down and up are only one, and the Move has several, which can be 0. "Here is a fact that the up event is probably 0," he added.

Understanding of the event distribution mechanism

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.