Android time interception mechanism

Source: Internet
Author: User

For Android event interception mechanism, I believe for most Android beginners is a scratching difficult to understand problems. In fact, it is not difficult to understand this problem.

First of all, what exactly is your understanding of the event interception mechanism? The event interception mechanism mentioned here refers to the interception mechanism for touch events. What is a touch event? The so-called touch event refers to the event generated by the touch screen captured by the system. When we click on a button, there are actually three events. Button pressed, this is one of the events; if you accidentally slide a little bit, this is the second of events; if you lift up, this is the third of events. Android encapsulates a class--motionevent for our touch event. In the Ontouchevent event, it is very convenient to listen to these three events.

Since it is possible to listen to this touch event, then what is the interception of the event? So, next, ask the students to imagine such a scene, okay? A view placed inside a viewgroup, the parent ViewGroup control is placed in another viewgroup inside, even can continue nesting, so that the descendants of the Endless Yan. So what's the problem? Touch events There's only one here, who am I going to give him to. Dad ViewGroup and son view all want to deal with this touch event, so "event interception" This very domineering name came into being.

To understand this event interception mechanism, I need to be placed in a scene. Like you are in a company, there is a Ceo,ceo under the director, director below is the manager, the manager under a bitter force of you. At this point, after a task, the CEO assigns him to the Director, the director assigns it to the manager, and the manager gives it to you. The process of uploading a task like this is quite similar to the event distribution and interception process.

In order to understand this case better, I use the control structure to simulate this kind of organization structure, to explain the event interception mechanism more deeply.

Let's take a look at this control UI frame composition:

How to judge that the parent container has an event interception with the child control, listens to the Ontouchevent () event in the parent container (ViewGroup), the Dispatchevent () event with the Oninterceptevent () event, The Ontouchevent () event is monitored in child view with the Dispatchevent () event. How to see these events are called, we print the corresponding log in each event, OK. Good, with such a general idea, we can be on the source code.

The child View source code is as follows:

 Public classMyViewextendsView { PublicMyView (Context context) {Super(context); }     PublicMyView (Context context, AttributeSet attrs) {Super(context, attrs); }     PublicMyView (Context context, AttributeSet attrs,intdefstyleattr) {        Super(context, attrs, defstyleattr); } @Override Public Booleanontouchevent (Motionevent event) {LOG.D ("ZCW", "View ontouchevent" +event.getaction ()); return Super. Ontouchevent (event); } @Override Public Booleandispatchtouchevent (Motionevent event) {LOG.D ("ZCW", "View dispatchtouchevent" +event.getaction ()); return Super. Dispatchtouchevent (event); }}

The parent ViewGroup source code is as follows:

 Public classMyviewgroupcextendsLinearLayout { PublicMYVIEWGROUPC (Context context) {Super(context); }     PublicMYVIEWGROUPC (Context context, AttributeSet attrs) {Super(context, attrs); }     PublicMYVIEWGROUPC (Context context, AttributeSet attrs,intdefstyleattr) {        Super(context, attrs, defstyleattr); } @Override Public Booleandispatchtouchevent (motionevent ev) {LOG.D ("ZCW", "VIEWGROUPC dispatchtouchevent" +ev.getaction ()); return Super. dispatchtouchevent (EV); } @Override Public Booleanonintercepttouchevent (motionevent ev) {LOG.D ("ZCW", "VIEWGROUPC onintercepttouchevent" +ev.getaction ()); return Super. onintercepttouchevent (EV); } @Override Public Booleanontouchevent (Motionevent event) {LOG.D ("ZCW", "VIEWGROUPC ontouchevent" +event.getaction ()); return Super. Ontouchevent (event); }}

The source code for the Zulu ViewGroup is as follows:

 Public classMyviewgroupbextendsLinearLayout { PublicMYVIEWGROUPB (Context context) {Super(context); }     PublicMYVIEWGROUPB (Context context, AttributeSet attrs) {Super(context, attrs); }     PublicMYVIEWGROUPB (Context context, AttributeSet attrs,intdefstyleattr) {        Super(context, attrs, defstyleattr); } @Override Public Booleandispatchtouchevent (motionevent ev) {LOG.D ("ZCW", "VIEWGROUPB dispatchtouchevent" +ev.getaction ()); return Super. dispatchtouchevent (EV); } @Override Public Booleanonintercepttouchevent (motionevent ev) {LOG.D ("ZCW", "VIEWGROUPB onintercepttouchevent" +ev.getaction ()); return Super. onintercepttouchevent (EV); } @Override Public Booleanontouchevent (Motionevent event) {LOG.D ("ZCW", "VIEWGROUPB ontouchevent" +event.getaction ()); return Super. Ontouchevent (event); }}

The source code for the great-grandfather ViewGroup control is as follows:

 Public classMyviewgroupaextendsLinearLayout { PublicMyviewgroupa (Context context) {Super(context); }     PublicMyviewgroupa (Context context, AttributeSet attrs) {Super(context, attrs); }     PublicMyviewgroupa (Context context, AttributeSet attrs,intdefstyleattr) {        Super(context, attrs, defstyleattr); } @Override Public Booleandispatchtouchevent (motionevent ev) {LOG.D ("ZCW", "Viewgroupa dispatchtouchevent" +ev.getaction ()); return Super. dispatchtouchevent (EV); } @Override Public Booleanonintercepttouchevent (motionevent ev) {LOG.D ("ZCW", "Viewgroupa onintercepttouchevent" +ev.getaction ()); return Super. onintercepttouchevent (EV); } @Override Public Booleanontouchevent (Motionevent event) {LOG.D ("ZCW", "Viewgroupa ontouchevent" +event.getaction ()); return Super. Ontouchevent (event); }}

We buried a little bit in every listening event.

We run the program to see the order in which each control's events are executed:

When we touch the Sub view, we find that the more top-level parent control control Dispatchevent,onintercepttouchevent the event first. When the inner Ontouchevent method executes completely, then the Ontouchevent method is executed from the inside out. The execution flowchart is this:

Let's change the code of the control a little bit and return true in the Ontouchevent method, and that's how it works at this point.

We can come to the conclusion that if the return is true, then the event is intercepted, or the event is passed down. This is the nature of the Android event blocker.

Return false--event Laissez-faire, the delivery of the transmission, return true--himself did, do not bother others, event interception, not to pass.

Android time interception 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.