Android event mechanism

Source: Internet
Author: User

First, Introduction

Touch events are events that occur when a touch screen is captured. Android encapsulates a class--motionevent for touch events, and if you override the Ontouchevent (Motionevent event) method, you'll find that the parameter to the method is a Motionevent class instance.

Event triggering is divided into three stages, capturing, targeting, bubbling.

The first stage is event capture, and the event flows from the root node to the target object node. The route passes through the view nodes at each level and triggers the capture event on each section until the target node of the event is reached.

The second phase is the target stage, the event goes into the target phase, the event is triggered on the target node, and then it is reversed until it propagates to the outermost root node.

The third stage is the event bubbling, and after the event is triggered on the catalog view node, it does not terminate on this element, and it bubbles upward as the node tree layers up and back to the root node.

analysis of event interception mechanism

Android's view structure is a tree structure, the view can be placed in a viewgroup inside, the ViewGroup is placed in another viewgroup inside, and even may continue to nest, layer by layer stacked up. Touch events on one, who should be assigned? The same event, both the child view and the parent viewgroup, are likely to be processed, resulting in an "event interception mechanism."

Suppose that your company has a general manager with the highest rank; he has a minister below; the lowest level is the work you, no level. Now the Board of directors to the general manager of a task, the general manager of the task assigned to the Minister, the minister has arranged the task to you. And when you finish the task, you put the task to the Minister, the Minister felt that the task was done well, so he signed his name, the task to the general manager, the general manager looked also feel good, also signed the name to the board. This, a task has been completed successfully.

A general manager--myviewgroupa, the outermost of the viewgroup;

A minister--myviewgroupb, middle-tier viewgroup;

A work of you--myview, at the lowest level;

For ViewGroup, you need to rewrite three methods:

1 @Override2  Public Booleandispatchtouchevent (motionevent event)3 {4LOG.D (TAG, "ViewGroup dispatchtouchevent" +event.getaction ());5     return Super. Dispatchtouchevent (event);6 }7 8 @Override9  Public Booleanonintercepttouchevent (motionevent event)Ten { OneLOG.D (TAG, "ViewGroup onintercepttouchevent" +event.getaction ()); A     return Super. Onintercepttouchevent (event); - } -  the @Override -  Public Booleanontouchevent (motionevent event) - { -LOG.D (TAG, "ViewGroup ontouchevent" +event.getaction ()); +     return Super. Ontouchevent (event); -}

For view, you need to rewrite the following two methods:

1 @Override2  Public Booleandispatchtouchevent (motionevent event)3 {4LOG.D (TAG, "View dispatchtouchevent" +event.getaction ());5     return Super. Dispatchtouchevent (event);6 }7 8 @Override9  Public Booleanontouchevent (motionevent event)Ten { OneLOG.D (TAG, "View ontouchevent" +event.getaction ()); A     return Super. Ontouchevent (event); -}

When you click on the view, log is as follows:

1 d/myviewgroupa:viewgroupa dispatchtouchevent2 d/Myviewgroupa:viewgroupa Onintercepttouchevent3 d/myviewgroupb:viewgroupb dispatchtouchevent4 d/  MYVIEWGROUPB:VIEWGROUPB onintercepttouchevent5 d/myview:view dispatchtouchevent6 d/myview:view ontouchevent7 d/MYVIEWGROUPB:MYVIEWGROUPB ontouchevent8 D/myviewgroupa:myviewgroupa ontouchevent
As you can see, the order in which the events are passed is:

General Manager (Myviewgroupa)-Minister (MYVIEWGROUPB)-You (MyView), this process is the capture phase of events;

PS: Event delivery, or capture, is the execution of the Dispatchtouchevent () method, and then the Onintercepttouchevent () method.

The order in which events are processed:

The process in which you go to work is the target phase of the event, the Ontouchevent () method of the view;

You (MyView)-Minister (MYVIEWGROUPB)-General manager (MYVIEWGROUPA), this process is the bubbling phase of events;

PS: Event handling is the execution of the Ontouchevent () method.

Hypothesis 1: The General Manager (MYVIEWGROUPA) found the task too simple, feel that they can complete, there is absolutely no need to find subordinates. Therefore, the event is intercepted by the general Manager (Myviewgroupa) onintercepttouchevent () method, i.e. let the Onintercepttouchevent () method return a value of true, and then look at log:

1 d/myviewgroupa:viewgroupa dispatchtouchevent2 d/Myviewgroupa:viewgroupa Onintercepttouchevent3 d/myviewgroupa:myviewgroupa ontouchevent

Hypothesis 2: the Minister (MYVIEWGROUPB) found the task too simple, felt that he could finish it, and had no need to look for subordinates at all. As a result, the event is intercepted by the Minister (MYVIEWGROUPB) Onintercepttouchevent () method, i.e. let the Onintercepttouchevent () method return a value of true, and then look at log:

1 d/myviewgroupa:viewgroupa dispatchtouchevent2 d/Myviewgroupa:viewgroupa Onintercepttouchevent3 d/myviewgroupb:viewgroupb dispatchtouchevent4 d/  MYVIEWGROUPB:VIEWGROUPB onintercepttouchevent5 d/MYVIEWGROUPB:MYVIEWGROUPB ontouchevent 6 D/myviewgroupa:myviewgroupa ontouchevent

In both cases, the general manager or the minister, Myviewgroupa or MYVIEWGROUPB, can be seen to intercept the incident.

The return value of the event means:

  When the event is passed, the return value: True, intercept, do not continue, False, do not intercept, continue the process, pass to the next level.

When the event is in process, the return value: True, processed, not audited; False, to the upper level processing.

  

 

Android event 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.