Android Event Distribution detailed (v)--touch event Delivery Validation

Source: Internet
Author: User

Mainactivity as follows:
Package Cn.c;import android.os.bundle;import Android.app.activity;import android.view.motionevent;/** * Demo Description: * Analyze the Android event distribution and processing mechanism * * Summary: * 1 ViewGroup inherited from the view * event is passed in the direction: from the outermost (Activity) to the most inner (a view) * Event The consumption direction is: From the most inner layer (a view) passed to   Outermost (Activity) * The two directions are reversed * 2 ViewGroup the process of event handling is: * dispatchtouchevent->onintercepttouchevent->ontouchevent * The process of event handling in view is: * dispatchtouchevent->ontouchevent * 3 viewgroup onintercepttouchevent Default value is False * indicates not intercepted * Viewgrou P Ontouchevent Default value is False * indicates not consumed * View Ontouchevent return default value is True * indicates consumed * Summary: * (1) Each time the Action_down and Action_mo ve and ACTION_UP will cause each layer of dispatchtouchevent () * So: the Order of Dispatchtouchevent () is from top to bottom!!!!!!!!!!!!!!! * (2) But each layer of ontouchevent () will not necessarily be executed. For example, the lower level has already consumed the event, then the upper layer will not respond to ontouchevent (). * So: Ontouchevent () is from bottom to top. Of course, the premise is that events are not consumed. The case of!!!!!!!!!!!!! * * There are three custom view in this example: * Outermost layout myframelayout * Inner layer layout mylinearlayout * Innermost custom button MyButton * * Now perform a series of tests and explain the phenomenon * Test One: * in Myframelayout's DispatchtoucheveThe NT () method returns false directly. * Then you can see that Myframelayout's Onintercepttouchevent () and ontouchevent () are not executed. * The sub-view included in Myframelayout does not reflect any of the touch events as well. * In fact, it is quite brutal to return false directly. Because the method is rewritten, so that the Dispatchtouchevent () method in the source code is not executed, so its onintercepttouchevent () and Ontouchevent () is not called and there is no continuation of the distribution of any events. * because it returns false to the activity directly, the Action_down event is not processed. So subsequent events such as Action_down and action_up * are not eligible for processing, and are directly handled by the activity. * * * Test Two: * called in the Dispatchtouchevent () method of Myframelayout: Super.dispatchtouchevent (EV); * and returns return super.dispatchtouchevent (EV); * Return true is returned directly in the Onintercepttouchevent () method of Myframelayout, indicating that interception * can see that the distribution of the event has stopped at Myframelayout and will not be distributed to its child view. * * The rest of the validation will be given in the next example.        * */public class Mainactivity extends Activity {@Override public void onCreate (Bundle savedinstancestate) {        Super.oncreate (savedinstancestate);        Setcontentview (R.layout.main);        System.out.println ("Call OnCreate ()" in ===> mainactivity);    System.out.println ("--------------------------------------------------"); } @Override PublIC boolean dispatchtouchevent (motionevent ev) {System.out.println (called mainactivity () in ===> dispatchtouchevent); System.out.println ("===> super.dispatchtouchevent () returns True by default");    System.out.println ("--------------------------------------------------");    return super.dispatchtouchevent (EV); } @Overridepublic void Onuserinteraction () {System.out.println ("Call Onuserinteraction ()" in ===> mainactivity); System.out.println ("--------------------------------------------------"); Super.onuserinteraction ();} @Overridepublic boolean ontouchevent (Motionevent event) {switch (event.getaction ()) {case Motionevent.action_down: System.out.println ("Call Ontouchevent ()--->action_down" in ===> mainactivity); Break;case Motionevent.action_move : System.out.println ("Call Ontouchevent ()--->action_move" in ===> mainactivity); Break;case motionevent.action_up: System.out.println ("Call Ontouchevent ()--->action_up" in ===> mainactivity);d Efault:break;} System.out.println ("Super.ontouchevent () returns false by default tableConsumption events "); System.out.println ("--------------------------------------------------"); return super.ontouchevent (event);}}


Myframelayout as follows:

Package Cn.c;import Android.content.context;import Android.util.attributeset;import android.view.MotionEvent; Import Android.widget.framelayout;public class Myframelayout extends Framelayout{public myframelayout (context context , AttributeSet Attrs) {Super (context, attrs);} @Overridepublic boolean dispatchtouchevent (motionevent ev) {super.dispatchtouchevent (EV); System.out.println (called "dispatchtouchevent ()" in the outer myframelayout); System.out.println ("Super.dispatchtouchevent () returns true by default to indicate continued distribution"); System.out.println ("--------------------------------------------------"); return super.dispatchtouchevent (EV);// return false;} Overwrite from Viewgroup@overridepublic boolean onintercepttouchevent (motionevent ev) {System.out.println ("outer myframelayout Called Onintercepttouchevent () "); System.out.println ("super.onintercepttouchevent () default return false means no interception"); System.out.println ("--------------------------------------------------");//return super.onintercepttouchevent ( EV); return true;} Note://1 ViewGroup is a subclass of view//2 ViewGroup OntoucheThe vent () method returns the False@overridepublic boolean ontouchevent (Motionevent event) {super.ontouchevent (event) By default, and switch ( Event.getaction ()) {case MotionEvent.ACTION_DOWN:System.out.println ("Call Ontouchevent () in the outer myframelayout)---> Action_down "); break;case MotionEvent.ACTION_MOVE:System.out.println (Call Ontouchevent () in the outer myframelayout---> Action_move "); break;case MotionEvent.ACTION_UP:System.out.println (Call Ontouchevent () in the outer myframelayout---> Action_up ");d Efault:break;} System.out.println ("Super.ontouchevent () returns false by default indicates no consumption event"); System.out.println ("--------------------------------------------------"); return super.ontouchevent (event);// return true;}}

Mylinearlayout as follows:
Package Cn.c;import Android.content.context;import Android.util.attributeset;import android.view.MotionEvent; Import Android.widget.linearlayout;public class Mylinearlayout extends LinearLayout {public mylinearlayout (Context Context, AttributeSet Attrs) {Super (context, attrs);} @Overridepublic boolean dispatchtouchevent (motionevent ev) {super.dispatchtouchevent (EV); System.out.println ("Call Dispatchtouchevent ()" In the inner layer mylinearlayout); System.out.println ("Super.dispatchtouchevent () returns true by default to indicate continued distribution"); System.out.println ("--------------------------------------------------");//return super.dispatchtouchevent (EV); return false;} Overwrite from Viewgroup@overridepublic boolean onintercepttouchevent (motionevent ev) {super.onintercepttouchevent (EV); System.out.println ("Call Onintercepttouchevent ()" In the inner layer mylinearlayout); System.out.println ("super.onintercepttouchevent () default return false means no interception"); System.out.println ("--------------------------------------------------"); return super.onintercepttouchevent (EV) ;} Note://1 ViewGroup is a viewThe subclass//2 ViewGroup in the Ontouchevent () method returns the False@overridepublic boolean ontouchevent (Motionevent event) {switch ( Event.getaction ()) {case MotionEvent.ACTION_DOWN:System.out.println ("Call ontouchevent ()---in the inner layer mylinearlayout > Action_down "); Break;case MotionEvent.ACTION_MOVE:System.out.println (" Call Ontouchevent () in the inner Layer Mylinearlayout ()---> Action_move "); Break;case MotionEvent.ACTION_UP:System.out.println (" Call Ontouchevent () in the inner Layer Mylinearlayout ()---> Action_up ");d Efault:break;} System.out.println ("Super.ontouchevent () returns false by default indicates no consumption event"); System.out.println ("--------------------------------------------------"); return super.ontouchevent (event);}}

MyButton as follows:
Package Cn.c;import Android.content.context;import Android.util.attributeset;import android.view.MotionEvent; Import Android.widget.button;public class MyButton extends Button{public MyButton (context context, AttributeSet Attrs) { Super (context, attrs);} @Overridepublic boolean dispatchtouchevent (Motionevent event) {System.out.println ("dispatchtouchevent" in the custom button ( )"); SYSTEM.OUT.PRINTLN ("Super.dispatchtouchevent returns True by default"); System.out.println ("--------------------------------------------------"); return Super.dispatchtouchevent (event) ;} Note://The Ontouchevent () method in the view's subclass returns the True@overridepublic boolean ontouchevent (Motionevent event) {switch ( Event.getaction ()) {case MotionEvent.ACTION_DOWN:System.out.println ("Call Ontouchevent () in the custom button)--->action_ Break;case MotionEvent.ACTION_MOVE:System.out.println ("Call Ontouchevent ()--->action_move" in the "Custom Button"); Break;case MotionEvent.ACTION_UP:System.out.println ("Call Ontouchevent ()--->action_up" in the custom button); Break;default : Break;} System.out. println ("Super.ontouchevent () returns True by default"); System.out.println ("--------------------------------------------------"); return super.ontouchevent (event);}}

Main.xml as follows:
<!--a custom layout, place a custom control--><cn.c.myframelayout     xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" fill_parent "    android:layout_height=" fill_parent ">    < Cn.c.mylinearlayout         android:layout_width= "wrap_content"        android:layout_height= "wrap_content" >   <cn.c.mybutton     android:layout_width= "200dip"     android:layout_height= "200dip"     android:text= " Custom Button "     android:textcolor=" @android: Color/black "   />    </cn.c.mylinearlayout></ Cn.c.myframelayout>


Android Event Distribution detailed (v)--touch event Delivery Validation

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.