Because the touch event will trigger the action is down, this is also the first trigger, so the title of the three methods to study the passing of this down event.
The following directly put the test code, while watching the side of the Talk:
There are only dispatchtouchevent and Ontouchevent methods in a activity,activity.
Public classMainactivityextendsactivity{@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); } @Override Public Booleandispatchtouchevent (motionevent ev) {//TODO auto-generated Method StubSystem.out.println (GetClass (). Getsimplename () + "Dispatchtouchevent"); return Super. dispatchtouchevent (EV); } @Override Public Booleanontouchevent (Motionevent event) {//TODO auto-generated Method StubSystem.out.println (GetClass (). Getsimplename () + "Ontouchevent"); return Super. Ontouchevent (event); }}
Three custom controls, the first two being the ViewGroup type, and the latter being the view type.
ViewGroup have Dispatchtouchevent,onintercepttouchevent and Ontouchevent methods.
Public classMyLinearLayout1extendslinearlayout{ PublicMYLINEARLAYOUT1 (Context context) {Super(context); //TODO auto-generated Constructor stub } PublicMYLINEARLAYOUT1 (context context, AttributeSet attrs,intDefstyle) { Super(context, attrs, Defstyle); //TODO auto-generated Constructor stub } PublicMYLINEARLAYOUT1 (Context context, AttributeSet attrs) {Super(context, attrs); //TODO auto-generated Constructor stub} @Override Public Booleandispatchtouchevent (motionevent ev) {//TODO auto-generated Method StubSystem.out.println (GetClass (). Getsimplename () + "Dispatchtouchevent"); return Super. dispatchtouchevent (EV); } @Override Public Booleanonintercepttouchevent (motionevent ev) {//TODO auto-generated Method StubSystem.out.println (GetClass (). Getsimplename () + "Onintercepttouchevent"); return Super. onintercepttouchevent (EV); } @Override Public Booleanontouchevent (Motionevent event) {//TODO auto-generated Method StubSystem.out.println (GetClass (). Getsimplename () + "Ontouchevent"); return Super. Ontouchevent (event); }}
Public classMyLinearLayout2extendslinearlayout{ PublicMyLinearLayout2 (Context context) {Super(context); //TODO auto-generated Constructor stub } PublicMyLinearLayout2 (context context, AttributeSet attrs,intDefstyle) { Super(context, attrs, Defstyle); //TODO auto-generated Constructor stub } PublicMyLinearLayout2 (Context context, AttributeSet attrs) {Super(context, attrs); //TODO auto-generated Constructor stub} @Override Public Booleandispatchtouchevent (motionevent ev) {//TODO auto-generated Method StubSystem.out.println (GetClass (). Getsimplename () + "Dispatchtouchevent"); return Super. dispatchtouchevent (EV); } @Override Public Booleanonintercepttouchevent (motionevent ev) {//TODO auto-generated Method StubSystem.out.println (GetClass (). Getsimplename () + "Onintercepttouchevent"); return true; } @Override Public Booleanontouchevent (Motionevent event) {//TODO auto-generated Method StubSystem.out.println (GetClass (). Getsimplename () + "Ontouchevent"); return true; }}
Public classMytextviewextendstextview{ PublicMytextview (Context context) {Super(context); //TODO auto-generated Constructor stub } PublicMytextview (context context, AttributeSet attrs,intDefstyle) { Super(context, attrs, Defstyle); //TODO auto-generated Constructor stub } PublicMytextview (Context context, AttributeSet attrs) {Super(context, attrs); //TODO auto-generated Constructor stub} @Override Public Booleandispatchtouchevent (Motionevent event) {//TODO auto-generated Method StubSystem.out.println (GetClass (). Getsimplename () + "Dispatchtouchevent"); return Super. Dispatchtouchevent (event); } @Override Public Booleanontouchevent (Motionevent event) {//TODO auto-generated Method StubSystem.out.println (GetClass (). Getsimplename () + "Ontouchevent"); return Super. Ontouchevent (event); }}
<COM.TESTTOUCHEVENT.MYLINEARLAYOUT1 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"android:gravity= "Center"android:orientation= "Vertical" > <Com.testtouchevent.MyLinearLayout2 android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:background= "@android: Color/darker_gray"android:orientation= "Vertical"android:padding= "20DP" > <Com.testtouchevent.MyTextView android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:background= "@android: Color/white"android:gravity= "Center"android:padding= "20DP"Android:text= "Me is TextView"/> </com.testtouchevent.MyLinearLayout2></com.testtouchevent.MyLinearLayout1>
The order in which the corresponding methods are executed for each control in the above code is this:
Mainactivity MYLINEARLAYOUT1 MyLinearLayout2 Mytextview
Dispatchtouchevent->dispatchtouchevent->onintercepttouchevent->dispatchtouchevent-> Onintercepttouchevent->dispatchtouchevent
ontouchevent<-ontouchevent<-ontouchevent<- Ontouchevent
Dispatchtouchevent: Determines whether the event continues to be distributed and responds to events, false: Continue distribution, true: Do not continue distribution, this event ends, there will be no control to execute the Ontouchevent method.
Onintercepttouchevent: Determines whether to intercept the event, false: Do not intercept, true: Intercept, at which time the current control executes the Ontouchevent method.
Ontouchevent: Determines whether to consume the event, false: No consumption, true: consumption.
Dispatchtouchevent, Onintercepttouchevent, and Ontouchevent event analysis in Android