An explanation of the Android event distribution mechanism (Ontouchlistener,onclick)

Source: Internet
Author: User

Yesterday to do something to touch the event conflict, before also often encounter event conflict, think to study the Android event conflict mechanism, so from yesterday to today to a full day to understand this knowledge, this only understand the Android touch and click event mechanism. Explore the following:

First rewrite the three view layouts for testing:

    

 Packagecom.example.yzj.android_8_10;ImportAndroid.content.Context;ImportAndroid.util.AttributeSet;ImportAndroid.util.Log;Importandroid.view.MotionEvent;Importandroid.widget.LinearLayout;/*** Created by YZJ on 2016/8/10.*/ Public classV1extendslinearlayout{ PublicV1 (Context context, AttributeSet attrs) {Super(context, attrs); } @Override Public Booleandispatchtouchevent (motionevent ev) {LOG.V ("MSG", "V1-dispatch"); return Super. dispatchtouchevent (EV); } @Override Public Booleanonintercepttouchevent (motionevent ev) {LOG.V ("MSG", "V1-onintercept"); return false; } @Override Public Booleanontouchevent (Motionevent event) {LOG.V ("MSG", "V1-ontouch"); return false; }}
 Packagecom.example.yzj.android_8_10;ImportAndroid.content.Context;ImportAndroid.util.AttributeSet;ImportAndroid.util.Log;Importandroid.view.MotionEvent;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;Importandroid.widget.LinearLayout;/*** Created by YZJ on 2016/8/10.*/ Public classV2extendslinearlayout{ PublicV2 (Context context, AttributeSet attrs) {Super(context, attrs); } @Override Public Booleandispatchtouchevent (motionevent ev) {LOG.V ("MSG", "V2-dispatch"); return Super. dispatchtouchevent (EV); } @Override Public Booleanonintercepttouchevent (motionevent ev) {LOG.V ("MSG", "V2-onintercept"); return false; } @Override Public Booleanontouchevent (Motionevent event) {LOG.V ("MSG", "V2-ontouch"); return false; }}
 Packagecom.example.yzj.android_8_10;ImportAndroid.content.Context;ImportAndroid.util.AttributeSet;ImportAndroid.util.Log;Importandroid.view.MotionEvent;ImportAndroid.view.View;/*** Created by YZJ on 2016/8/10.*/ Public classV3extendsView { PublicV3 (Context context, AttributeSet attrs) {Super(context, attrs); } @Override Public Booleandispatchtouchevent (motionevent ev) {LOG.V ("MSG", "V3-dispatch"); return Super. dispatchtouchevent (EV); }//@Override//Public Boolean ontouchevent (Motionevent event) {//log.v ("msg", "V3-ontouch");//return true;//    }}

Then there is the XML code for mainactivity:

<?xml version= "1.0" encoding= "Utf-8"? ><com.example.yzj.android_8_10.v1 xmlns:android= "/http Schemas.android.com/apk/res/android "Android:id= "@+id/v1"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:background= "@color/coloraccent"Tools:context= "Com.example.yzj.android_8_10.MainActivity" > <com.example.yzj.android_8_10.V2 android:layout_gravity= "Center"Android:id= "@+id/v2"Android:layout_width= "400DP"Android:layout_height= "400DP"Android:background= "@color/colorprimarydark" > <com.example.yzj.android_8_10.V3 Android:background= "#F00000"Android:id= "@+id/v3"android:layout_gravity= "Center"Android:layout_width= "300DP"Android:layout_height= "300DP" ></com.example.yzj.android_8_10.V3> </com.example.yzj.android_8_10.v2></ Com.example.yzj.android_8_10.v1>

    

Then is the Java code for mainactivity:

  

 Packagecom.example.yzj.android_8_10;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.util.Log;Importandroid.view.MotionEvent;ImportAndroid.view.View; Public classMainactivityextendsappcompatactivity {View v1, v2, v3; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main);    Init (); } @Override Public Booleandispatchtouchevent (motionevent ev) {LOG.V ("MSG", "Mainactivity-dispatch"); return Super. dispatchtouchevent (EV); } @Override Public Booleanontouchevent (Motionevent event) {LOG.V ("MSG", "Mainactivity-ontouch"); return false; }    Private voidinit () {v1=Findviewbyid (R.ID.V1); V2=Findviewbyid (R.ID.V2); V3=Findviewbyid (R.ID.V3); V3.setontouchlistener (NewView.ontouchlistener () {@Override Public BooleanOnTouch (View V, motionevent event) {Switch(Event.getaction ()) { CaseMOTIONEVENT.ACTION_DOWN:LOG.V ("MSG", "Action_down");  Break;  CaseMOTIONEVENT.ACTION_MOVE:LOG.V ("MSG", "Action_move");  Break;  CaseMOTIONEVENT.ACTION_UP:LOG.V ("MSG", "Action_up");  Break; }                return false;      }        }); V3.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {LOG.V ("MSG", "V3-onclick");    }      }); }}

Here's a detailed explanation:

Android Touch and click events are actually tied together, or in more detail a bit of the onclick is dependent on ontouch, which is explained later in detail. First of all, the process of message delivery, Android and Windows Messaging mechanism is the same, is the bubbling pass, that is, from the bottom, go up, in turn, in my code is from Mainactivity->v1->v2->v3, so that the message, And to deal with it, or to use the term "consumption" in the jargon of Google's API documentation ... ), consumption is the opposite direction, that is, starting from the smallest V3 above, and gradually passing to the mainactivity,v3->v2->v1->mainactivity.

The following is the result of the code run:

Event Delivery Process

08-10 17:40:06.182 3926-3926/? V/msg:mainactivity-dispatch
08-10 17:40:06.182 3926-3926/? V/msg:v1-dispatch
08-10 17:40:06.182 3926-3926/? V/msg:v1-onintercept
08-10 17:40:06.182 3926-3926/? V/msg:v2-dispatch
08-10 17:40:06.182 3926-3926/? V/msg:v2-onintercept
08-10 17:40:06.182 3926-3926/? V/msg:v3-dispatch

Event consumption process
08-10 17:40:06.182 3926-3926/? V/msg:v3-ontouch
08-10 17:40:06.182 3926-3926/? V/msg:v2-ontouch
08-10 17:40:06.182 3926-3926/? V/msg:v1-ontouch
08-10 17:40:06.182 3926-3926/? V/msg:mainactivity-ontouch

It can be clearly seen that the direction of the event's transmission process and the direction of the consumption process.

  

    

3 methods related to touch events: Public Boolean dispatchtouchevent (motionevent ev); Used to assign an event Public Boolean onintercepttouchevent (motionevent ev);//To intercept event Public Boolean ontouchevent (motionevent ev); Used to handle event

Activity class: Activity Dispatchtouchevent ();
Ontouchevent ();
View container (subclass of ViewGroup): Framelayout, LinearLayout ...
ListView, ScrollVIew ...
Dispatchtouchevent ();
Onintercepttouchevent ();
Ontouchevent ();
View Control (non-viewgroup subclass): Button, TextView, EditText ... Dispatchtouchevent ();
Ontouchevent ();

   

three a party method of Use:
Dispatchtouchevent () Used to dispatch events.
where Onintercepttouchevent () and ontouchevent () are called, the method is not generally overridden
Onintercepttouchevent () Used to intercept events.
The source code implementation in the ViewGroup class is {return false;} Indicates that the event is not intercepted,
The event is passed down (passed to its child view);
If you manually override the method so that it returns true to indicate interception, the event will stop passing down,
Event is handled by the current ViewGroup class, which is called the Ontouchevent () method of the class
Ontouchevent () Used to handle events.
Returns true to indicate that the view handles the event, and the event terminates the pass-up (passed to its parent view);
Returns false to indicate that it cannot be processed, then the event is passed to its parent view's Ontouchevent () method to handle
The following is the sequence of the Acion_down,move,up,onclick, the test results are as follows:

08-10 17:49:30.182 5125-5125/? V/msg:mainactivity-dispatch
08-10 17:49:30.182 5125-5125/? V/msg:v1-dispatch
08-10 17:49:30.182 5125-5125/? V/msg:v1-onintercept
08-10 17:49:30.182 5125-5125/? V/msg:v2-dispatch
08-10 17:49:30.182 5125-5125/? V/msg:v2-onintercept
08-10 17:49:30.182 5125-5125/? V/msg:v3-dispatch
08-10 17:49:30.182 5125-5125/? V/msg:action_down
08-10 17:49:30.182 5125-5125/? V/msg:v2-ontouch
08-10 17:49:30.182 5125-5125/? V/msg:v1-ontouch
08-10 17:49:30.182 5125-5125/? V/msg:mainactivity-ontouch
08-10 17:49:30.202 5125-5125/? V/msg:mainactivity-dispatch
08-10 17:49:30.202 5125-5125/? V/msg:mainactivity-ontouch
08-10 17:49:30.232 5125-5125/? V/msg:mainactivity-dispatch
08-10 17:49:30.232 5125-5125/? V/msg:mainactivity-ontouch
08-10 17:49:30.242 5125-5125/? V/msg:mainactivity-dispatch
08-10 17:49:30.242 5125-5125/? V/msg:mainactivity-ontouch

Finally draw on a few others summed up a good map, you can more intuitive to see:

If the flase returned at V3 Ontouch, then

 

The rest of the situation, please test yourself, this article is written here.

Thank http://blog.csdn.net/morgan_xww/article/details/9372285/for giving me a hint here.

I hope you will test yourself, after all, I have moved to remember relatively solid.

An explanation of the Android event distribution mechanism (Ontouchlistener,onclick)

Related Article

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.