Android Rookie road-event distribution mechanism summary (ii)

Source: Internet
Author: User

ViewGroup event distribution mechanism itself defines a linearlayout,imageview and button, waiter, on the code
<linearlayout 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 "tools:c Ontext= ". Mainactivity "> <com.example.aaaaa.mylinear android:id=" @+id/linear "android:layout_width=" Match_ Parent "android:layout_height=" match_parent "android:background=" #956456 "> <com.example . aaaaa.         MyImage android:id= "@+id/id_ll" android:layout_width= "150DP" android:layout_height= "150DP"          android:src= "@drawable/ic_launcher" android:text= "click Me"/> <com.example.aaaaa.mybutton Android:id= "@+id/id_btn" android:layout_width= "150DP" android:layout_height= "150DP" Android: src= "@drawable/ic_launcher" android:text= "click Me"/> </com.example.aaaaa.mylinear></linearlayout >

public class Mylinear extends linearlayout{private static final String TAG =mylinear.class.getsimplename ();p ublic Mylinear (context context, AttributeSet Attrs) {Super (context, attrs);} Public Mylinear (Context context) {super (context);}  @Override public boolean ontouchevent (Motionevent event) {int action = event.getaction ();  Switch (action) {case MOTIONEVENT.ACTION_DOWN:LOG.E (TAG, "linearlayout+ontouchevent action_down");  Break  Case MOTIONEVENT.ACTION_MOVE:LOG.E (TAG, "linearlayout+ontouchevent action_move");  Break  Case MOTIONEVENT.ACTION_UP:LOG.E (TAG, "linearlayout+ontouchevent action_up");  Break  Default:break;  } return Super.ontouchevent (event);  } @Override public boolean dispatchtouchevent (Motionevent event) {int action = event.getaction (); Switch (action) {case MOTIONEVENT.ACTION_DOWN:LOG.E (TAG, "linearlayout+dispatchtouchevent action_down");//return tru  E;break; Case MOTIONEVENT.ACTION_MOVE:LOG.E (TAG, "Linearlayout+dispatchtouchevent Action_movE ");  Break  Case MOTIONEVENT.ACTION_UP:LOG.E (TAG, "linearlayout+dispatchtouchevent action_up");  Break  Default:break; } Boolean A=super.dispatchtouchevent (event);//LOG.E (TAG, "linearlayout:a=" +a);  return A; } @Overridepublic Boolean onintercepttouchevent (motionevent ev) {return super.onintercepttouchevent (EV);}}

public class MyButton extends Button {private static final String TAG = MyButton.class.getSimpleName ();  Public MyButton (context context, AttributeSet Attrs) {Super (context, attrs);  } @Override public boolean ontouchevent (Motionevent event) {int action = event.getaction ();  Switch (action) {case MOTIONEVENT.ACTION_DOWN:LOG.E (TAG, "button+ontouchevent action_down");  Break  Case MOTIONEVENT.ACTION_MOVE:LOG.E (TAG, "button+ontouchevent action_move");  Break  Case MOTIONEVENT.ACTION_UP:LOG.E (TAG, "button+ontouchevent action_up");  Break  Default:break;  } return Super.ontouchevent (event);  } @Override public boolean dispatchtouchevent (Motionevent event) {int action = event.getaction (); Switch (action) {case MOTIONEVENT.ACTION_DOWN:LOG.E (TAG, "button+dispatchtouchevent action_down");//return True;brea  K  Case MOTIONEVENT.ACTION_MOVE:LOG.E (TAG, "button+dispatchtouchevent action_move");  Break Case MOTIONEVENT.ACTION_UP:LOG.E (TAG, "Button+dispatchtoUchevent action_up ");  Break  Default:break; } Boolean A=super.dispatchtouchevent (event);//LOG.E (TAG, "button:a=" +a);  Return A;//return super.dispatchtouchevent (event); }  }

public class MyImage extends ImageView {private static final String TAG = MyImage.class.getSimpleName ();  Public MyImage (context context, AttributeSet Attrs) {Super (context, attrs);  } @Override public boolean ontouchevent (Motionevent event) {int action = event.getaction ();  Switch (action) {case MOTIONEVENT.ACTION_DOWN:LOG.E (TAG, "ontouchevent action_down");  Break  Case MOTIONEVENT.ACTION_MOVE:LOG.E (TAG, "ontouchevent action_move");  Break  Case MOTIONEVENT.ACTION_UP:LOG.E (TAG, "ontouchevent action_up");  Break  Default:break;  } return Super.ontouchevent (event);  } @Override public boolean dispatchtouchevent (Motionevent event) {int action = event.getaction ();  Switch (action) {case MOTIONEVENT.ACTION_DOWN:LOG.E (TAG, "dispatchtouchevent Action_down"); return false;//break;  Case MOTIONEVENT.ACTION_MOVE:LOG.E (TAG, "dispatchtouchevent action_move");  Break  Case MOTIONEVENT.ACTION_UP:LOG.E (TAG, "dispatchtouchevent action_up");  Break  DefaultBreak } Boolean A=super.dispatchtouchevent (event);//LOG.E (TAG, "imageview:a=" +a);  Return A;//return super.dispatchtouchevent (event);  }  }
public class Mainactivity extends Activity {protected static final String TAG = "mainactivity"; Private myimage imageview;private MyButton button; @Override protected void OnCreate (Bundle savedinstancestate) {super.  OnCreate (savedinstancestate);  Setcontentview (R.layout.activity_main); imageview= (myimage) Findviewbyid (R.ID.ID_LL); button= (MyButton) Findviewbyid (R.ID.ID_BTN); Imageview.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {log.e (TAG, " setOnClickListener000 ");}); Button.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {log.e (TAG, "button+ setOnClickListener000 ");}); Final LinearLayout ll = (linearlayout) Findviewbyid (r.id.linear);//ll.setonclicklistener (new Onclicklistener () {//@ override//public void OnClick (View v) {//log.e (TAG, "linearlayout+setonclicklistener000");//}//});// Ll.setontouchlistener (New Ontouchlistener () {@Overridepublic Boolean onTouch (View V, motionevent event) {int action = Eve  Nt.getaction ();Switch (action) {case MOTIONEVENT.ACTION_DOWN:LOG.E (TAG, "Linearlayout+ontouch action_down");  LOG.E (TAG, "linearlayout+000" +ll.isclickable ());//return False;break;  Case MOTIONEVENT.ACTION_MOVE:LOG.E (TAG, "Linearlayout+ontouch action_move");  LOG.E (TAG, "linearlayout+000" +ll.isclickable ());//return False;break; Case MOTIONEVENT.ACTION_UP:LOG.E (TAG, "Linearlayout+ontouch action_up");  LOG.E (TAG, "linearlayout+000" +ll.isclickable ());  Default:break;  } return true;  }}); Imageview.setontouchlistener (new Ontouchlistener () {@Override public boolean onTouch (View V, motionevent event)  {int action = event.getaction (); Switch (action) {case MOTIONEVENT.ACTION_DOWN:LOG.E (TAG, "OnTouch action_down");  LOG.E (TAG, "+imageview.isclickable");//return False;break;  Case MOTIONEVENT.ACTION_MOVE:LOG.E (TAG, "OnTouch action_move");  LOG.E (TAG, "+imageview.isclickable");//return False;break; Case MOTIONEVENT.ACTION_UP:LOG.E (TAG, "OnTouch action_up"); LOG.E (TAG,"+imageview.isclickable" (); break;  Default:break;  } return true;  }  }); Button.setontouchlistener (New Ontouchlistener () {@Override public boolean onTouch (View V, motionevent event) {int a  ction = Event.getaction (); Switch (action) {case MOTIONEVENT.ACTION_DOWN:LOG.E (tag, "Button+ontouch Action_down");//LOG.E (tag, "button+000" +but  Ton.isclickable ());//return False;break;  Case MOTIONEVENT.ACTION_MOVE:LOG.E (TAG, "Button+ontouch action_move");  LOG.E (TAG, "button+000" +button.isclickable ());//return False;break; Case MOTIONEVENT.ACTION_UP:LOG.E (TAG, "Button+ontouch action_up");  LOG.E (TAG, "button+000" +button.isclickable ());  Default:break;  } return false;  }  }); }  }
Code is easy, basic information is printed, OK. Let's start with a test. Suppose you click on your own button and shake your hand. What information does it print? Let's look at a look.
Suppose you click on your own definition ImageView. And shake hands, what information will be printed? Let's look at a look.

why is that? Since I returned false in the Motionevent.action_down event inference in my own definition of ImageView's Dispatchtouchevent method, do you remember what we said at the end of the last article?
* * * Remember, there is only one action_down each. The corresponding dispatchtouchevent method of the Action_move,action_up event returns true. Ability to run backwards, that is, assuming the Dispatchtouchevent method Event.getaction () ==action_down, you return False, then the Dispatchtouchevent method will not run ( Plainly speaking, each action event will run once dispatchtouchevent, run Action_down return false, after a series of other action will not be run again, that is action_move,action The _up event will not get run.
When I get here, I suggest combining the source code of the two gods to see it http://blog.csdn.net/guolin_blog/article/details/9153747

http://blog.csdn.net/lmj623565791/article/details/39102591

Look at this picture after reading, is not an instant clear

A friend who really wants to be ambiguous can copy the code above me. Change it according to your own ideas, then execute it, very quickly you will be enlightened.

Summary: When you click on a control in the layout. The Dispatchtouchevent method of the ViewGroup is called first, assuming that the Onintercepttouchevent method returns False, then the Dispatchtouchevent method of the child view is called ( This method returns true to indicate that the child view has consumed the event. Assuming that the Onintercepttouchevent method returns True, the Dispatchtouchevent method of ViewGroup's parent class (that is, view) is called, followed by the Ontouch of the layout, Ontouchevent method.Be able to remember this: the layout of the thing to the child view processing, the Dispatchtouchevent method of the child view returns true means that it has done the job, the layout is not processed. If you return false, the child view is not finished, then the layout will be processed.

(Go back to see * * * Remember there, clear it? Friends. Suppose an event you return False, followed by the layout is processed, so the event behind the child view will not run)

All right. I admit that this is a bit of water, in fact the main thing is to provide code for everyone to carry out the test. Do it yourself. Clothed.



Android Rookie road-event distribution mechanism summary (ii)

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.