Specific explanation of the Android event interception processing mechanism

Source: Internet
Author: User

The previous time has just come into contact with Android phone development. To its event dissemination mechanism is not very understanding, although the online also looked up the relevant information, but always think understanding vague, specious, so I wrote a small demo test a bit.

Finally made clear its detailed mechanism. Write down your own conclusions. Share it and hope to help people who are beginners to Android

What the layout effect sees:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvy2h1bnfpdxdlaq==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center "/>

Figure 1

Let's take a look at the detailed conclusions:

1)onintercepttoucheventresponsible forTouchevent to intercept. For nestedViewThe first thing to run is the event interception method, the outermost one .Viewof theonintercepttoucheventmethod, and then run the child view in sequenceOnintercepttouchevent,Then in the child view of the running child view of the event interception method(Of course, this assumes that all nested views areonintercepttoucheventwill get run,let each view of theonintercepttoucheventreturnfalse'll be). Participation, soonintercepttoucheventThe order of operation isA--->b--->c--->d.That is, the parent view is passed to the child view.

Anyway. The event interception mechanism is initiated by the parent view to intercept the event ( Something happened to me first, son later ).

When the finger touches the event. Parent View A First initiates an interception of the event. If A intercept fails, it is passed to its sub-view b to intercept, assuming that B intercept failure is given to b 's child view C to intercept again . until a child view has successfully intercepted the event.

2) the inference that a view interception event succeeds or not is onintercepttouchevent The return value of the method, when the return true when the interception is successful. Returns false to indicate that the current view failed to intercept the event.

3) The following is a case of interception success, assuming that C view on the current Touch event interception succeeded. The interception success means that this event will no longer be passed to D view.

So at this point,Dof the ViewonintercepttoucheventIt's not going to work.(The incident cannot be reached, and who is it to intercept? ). after the event interception succeeds. The event will be processed shortly thereafter ., the method of processing teachesontoucheventmethod is processed. At thisCThe view interception succeeds, then it runs immediatelyCof 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)inference runs the related logic.

assume that the returned false, Description C The view does not handle this event or can not handle it, what to do? No, my son. Dad came, so the incident was in the ontouchevent method of the B view . The same b for this event or not to see the ontouchevent return value of B. The detailed explanation is the same as C , no more words.

4) in the A B C D of the onintercepttouchevent and the ontouchevent are returned false the case. The order in which the method runs is a.onintercepttouchevent-->b.onintercepttouchevent-->c.onintercepttouchevent--> D.touchevent ( The deepest child 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 .


Analyze event handling mechanism with event source code to read the principle of interpreting Android events from source code perspective

The following is a detailed explanation of how the above conclusions are drawn, with a two-part plan for a step-by-step explanation . Assuming that the above is clear, the following content will not be seen, because it will be very verbose .

The layout code of Figure 1 looks like the following:

<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 self-defined textview. The difference between a three view and a B C is that D simply rewrites the Ontouchevent method. A B C these three self-defined controls also override the Oninterceptevent method.

D code for example, the 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 this, just post one of them.

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 for Aview is almost the same as the general 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:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvy2h1bnfpdxdlaq==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "/>

Conversion becomes:


As can be seen from this diagram, the Onintercepttouchevent event is run in the order of the parent control to the child control. And the Ontouchevent method that takes precedence over its own control runs, the Ontouchevent event runs exactly the opposite of the child control to the parent control. Note that because it returns false at this point, 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 matched (processed), and in this case even if you write in the Ontouchevent method of D, such as the following code, it will not be run.


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

1) Assuming that the intercepttouchevent of a returns true and the rest still returns false, then the log for the output is:

Conversion becomes:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvy2h1bnfpdxdlaq==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "/>


Be able to discover at this point a intercepts the touch event. The event is no longer passed 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 given to A's Ontouchevent method (of course, this is the case when the Ontouchevent method returns True. It is assumed that returning false will not correspond to these actions when tested. B,c, the D control is an event-handling interception method and event-handling method that cannot be run.

2) only b the Oninterceptetouchevent event returns true in the case that the printed log is

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 is returned as false.

4) Similarly, the Onintercept method of the C control returns the true case. The remainder still returns false in case the output log is

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvy2h1bnfpdxdlaq==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "/>

Converted Into

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvy2h1bnfpdxdlaq==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "/>

The following is a case where the ontouchevent of each view returns True

Because the Ontouchevent event is passed from the child control to the parent control. When the ontouchevent of D returns True, the test output effects such as the following

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvy2h1bnfpdxdlaq==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "/>

Conversion becomes:

It was discovered by Test. At this point D handled the various action,c B D of the touch event, and the Ontouchevent did not get run.

Similarly, when the Ontouchevent method of C returns true, the output log such as the following

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvy2h1bnfpdxdlaq==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "/>

Convert to such as the following:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvy2h1bnfpdxdlaq==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "/>

After the test found that the rest of the analogy, it is not wordy. After a step-by-step test to get the article at the beginning of the knot is a bit verbose, hoping to read this article to help people.



Analyze event handling mechanism with event source code to read the principle of interpreting Android events from source code perspective

Specific 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.