An explanation of the Android event interception processing mechanism

Source: Internet
Author: User

The previous period of time just contact with the development of Android mobile phone, its event propagation mechanism is not very understanding, although the Internet also looked up the relevant information, but always feel vaguely understood, specious, so I wrote a small demo test a bit. Finally figured out its specific mechanism. Write down your conclusions, share them, and hope to help people who are beginners to Android

Layout effect:

Figure 1

Refer to the first to say the concrete results:

1)onintercepttoucheventresponsible forTouchevent to intercept, for nestedViewThe first thing to do is the event interception method is the outermost one .Viewof theonintercepttoucheventmethod, and then in turn executes the child view of theOnintercepttouchevent,Then in the child view of the execution of the child view of the event interception method(Of course, this assumes that all nested viewsonintercepttoucheventwill be executed .,make each view'sonintercepttoucheventreturnfalsecan be). Reference, soonintercepttoucheventThe order of execution isA--->b--->c--->d.That is, the parent view is passed to the child view. In summary, the event interception mechanism is initiated by the parent view to intercept the event(Something's wrong, I'll go first, son .). Reference when the finger touches the event, the parent viewAfirst initiate an interception of the incident, ifAIntercept failed, just give it a child viewBto intercept;Bintercept failure will be given toBthe child ViewCto intercept again ...until a child view has successfully intercepted the event.

2) the identification of the success or absence of a view interception event is onintercepttouchevent The return value of the method, when the return true when the interception succeeds and returns false indicates that the current view failed to intercept the event.

3)Let 's talk about the success of the interception, assumingCview on the currentTouchevent interception succeeded. The interception of success means that this incident will not be transmitted toDthe view. So at this point,Dof the ViewonintercepttoucheventIt's not going to work.(The incident cannot be reached, and who is it to intercept? ). once event interception is successful, the event is immediately processed, the method of processing teachesontoucheventmethod is processed. At thisCThe view intercept succeeds, and then it executes immediately.Cof the ViewOntouchevenTMethod,does that mean that the currentTouchevent is byCof the Viewontoucheventmethod to deal with it? It is up toCof the Viewontoucheventmethod to determine the return value of the. whenCof the Viewontoucheventreturntrue, the current event isCDiscretionary Treatment, dealing with the various of course eventsAction, whatmotionevent.action_move,action_upit's all over.Cof theontoucheventmethod for processing. So at this point, you canCof theontoucheventmethod, doswitch (event.getaction)judge the execution of the relevant logic. if the returnedfalse,DescriptionCThe view does not handle this event or can not handle it, what to do? My son, Dad came, so the incident was made.Bof the Viewontoucheventmethod in. SameBWhether to deal with this event or not is to seeBof theontoucheventreturn value, the specific explanation isCthe same, no more words.

4) in the A B C D of the onintercepttouchevent and the ontouchevent are returned false , the order in which the methods are executed is A.onintercepttouchevent-->b.onintercepttouchevent-->c.onintercepttouchevent-->d.touchevent ( The deepest sub-view did not rewrite onintercepttouchevent)-->c.touchevent-->b.touchevent--> A.touchevent. That is, the interception event is that the parent view takes precedence over the child view to intercept, and the processing event is the child view precedence parent view for processing.

Summary: Onintercepttouchevent is responsible for the interception of events, the interception succeeds to the first encounter Ontouchevent return true of the view be processed .

The following will explain in detail how the above conclusions are drawn, and prepare to be divided into two parts for a step-by-step explanation . If the above is clear, the following content will not look, because it will be very verbose .

The layout code for Figure 1 looks like this:

<com.example.demo.aview xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent " >    <com.example.demo.bview        android:layout_width= "match_parent"        android:layout_height= "Match_ Parent ">        <com.example.demo.cview            android:layout_width=" match_parent "            android:layout_height=" Match_parent ">            <com.example.demo.dview                android:layout_width=" match_parent "                android:layout_ height= "Match_parent"                android:text= "test demo"/>        </com.example.demo.CView>    </ Com.example.demo.bview></com.example.demo.aview>

The last D is a custom textview, and the difference between a three view and a B c is that D only overrides the Ontouchevent method, and a B C three custom controls also override the Oninterceptevent method.

D code as follows, a B C code basically except the class name and output log is not the same as the rest of the same, so in order to reduce the only one of the paste.

Dview's Code:

public class DView extends textview{    private static String tag = "D";p ublic DView (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle);} Public DView (context context, AttributeSet Attrs) {Super (context, attrs);} Public DView (Context context) {super (context);}  @Overridepublic boolean ontouchevent (Motionevent event) {LOG.E (tag, "--ontouchevent--d"); return false;}}

The code of Aview is similar to the whole of C D, and it is posted as one:

public class Aview extends relativelayout{    private static String tag = "A";p ublic Aview (context context) {Super (Contex t);} Public Aview (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle);} Public Aview (context context, AttributeSet Attrs) {Super (context, attrs);} @Overridepublic Boolean Onintercepttouchevent (motionevent ev) {LOG.E (tag, "--onintercepttouchevent--a"); return false;} @Overridepublic boolean ontouchevent (Motionevent event) {LOG.E (tag, "--ontouchevent---A"); return false;}}

At the beginning of the rewrite method all return False to run the Click Effect Output log is:


Conversion becomes:


As you can see from this diagram, the order of execution of the Onintercepttouchevent event is performed by the parent control to the child control and takes precedence over the Ontouchevent method of its own control, and the Ontouchevent event executes exactly the opposite of the control to the parent control. Note that since false is returned, there is no view to handle each action of the touch event, which is why Ontouchevent has been passed to a. So events such as Action_move and action_up are not dealt with, and in this case you will not be executed even if you write the following code in the Ontouchevent method of D.

if (Event.getaction () ==motionevent.action_move) {LOG.E (tag, "--ontouchevent--*****");}

1) If the intercepttouchevent of a returns true, the rest still returns false, then the log of the execution output is:

Conversion becomes:



It can be found that at this point a intercepts the touch event, and the event no longer passes to the child control B C D of a. At this point all action events such as the finger Move event Action_move or Action_up event are all given to A's Ontouchevent method (which is, of course, in the case where the Ontouchevent method returns True, Returns false if the action is not appropriate when tested. B,c, the D control is an event-handling interception method and event-handling method that cannot be executed.

2) only the Oninterceptetouchevent event of B returns true in the case of the printed log as

Conversion becomes:

At this point, the touch event is intercepted by B and will not be passed to the C-D child control, and the various actions of the event.getaction () of this event will not be processed because the Ontouchevent event returns false.

4) in the same way, the C control's Onintercept method returns True if the remainder of the case still returns false, and the output log is

Converted Into

Let's talk about the ontouchevent of each view returning true.

Since the Ontouchevent event is passed from the child control to the parent control, when the ontouchevent of D returns True, the test output is as follows

Conversion becomes:

The test found that at this point D had handled the various action,c B D of the Touch event, and Ontouchevent was not executed.

Similarly, when the Ontouchevent method of C returns true, the output log is as follows

Converted to the following:

It has been tested that each ACTION of the event is responded to in the CView Ontouchevent method, and the ontouchevent of D is not motionevent.action_xx accordingly. The rest of the time, and so on, is not wordy. After a step-by-step test to get the article at the beginning of the knot is a bit verbose, I hope it can be helpful to people who read this article.




An explanation of the Android event interception processing 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.