Android Event distribution mechanism

Source: Internet
Author: User

first, distinguish between view and ViewGroup, a control, and a control container (meaning that it can contain controls)Next, familiarize yourself with three methods:

public boolean dispatchtouchevent (Motioneventev) //distribution TouchEvent

By default, intercepttouchevent processing

When True is returned, handle it yourself

Returns False when the Ontouchevent

Public Booleanon intercepttouchevent (motionevent ev) //intercept TouchEvent

When returned true, is handled by his own ontouchevent

Returns false when the distribution continues

public boolean ontouchevent (motionevent ev) //processing TouchEvent

When Ontouchevent returns True, handle it yourself

When Ontouchevent returns false, it continues to pass

Continue delivery By default

The view class has Dispatchtouchevent and Ontouchevent two methods, viewgroup inherit view, and a new Onintercepttouchevent method is added. There are no onintercepttouchevent methods in activity, but there are two other methods.

, is the default propagation order:


The layout is as follows:

<span style= "Font-family:microsoft Yahei;" ><relativelayout 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:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: Context= ". Mainactivity "> <com.example.dispatchtest.myviewgroup android:id=" @+id/myviewgroup "android:layout_width=" f Ill_parent "android:layout_height=" fill_parent "android:orientation=" vertical "> <com.example.di Spatchtest. MyView android:id= "@+id/myview" android:layout_width= "wrap_content" android:layout_height= "Wrap_cont Ent "/> </com.example.dispatchtest.MyViewGroup></RelativeLayout></span>

The custom viewgroup and view are as follows:

<span style= "Font-family:microsoft Yahei;" >package Com.example.dispatchtest;import Android.content.context;import Android.util.attributeset;import Android.view.motionevent;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.linearlayout;public class Myviewgroup extends Linearlayout{public myviewgroup (context context,  AttributeSet attrs) {super (context,attrs); Todo auto-generated constructor stub} @Overridepublic boolean dispatchtouchevent (motionevent ev) {//TODO Auto-generated method StubSystem.out.println ("Myviewgroup dispatchtouchevent"); return super.dispatchtouchevent (EV) ;} @Overridepublic boolean ontouchevent (Motionevent event) {//TODO auto-generated method StubSystem.out.println (" Myviewgroup ontouchevent "); return super.ontouchevent (event);} @Overridepublic boolean onintercepttouchevent (motionevent ev) {//TODO auto-generated method StubSystem.out.println (" Myviewgroup onintercepttouchevent "); return super.onintercepttouchevent (EV);}} </sPan> 

<span style= "Font-family:microsoft Yahei;" >package Com.example.dispatchtest;import Android.content.context;import Android.util.attributeset;import Android.view.motionevent;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.imageview;public class MyView extends Imageview{public MyView (Context context,attributeset attrs) { Super (CONTEXT,ATTRS); Todo auto-generated constructor stub} @Overridepublic boolean Dispatchtouchevent (Motionevent event) {//TODO Auto-generated method StubSystem.out.println ("MyView dispatchtouchevent"); return super.dispatchtouchevent (event);} @Overridepublic boolean ontouchevent (Motionevent event) {//TODO auto-generated method StubSystem.out.println ("MyView Ontouchevent "); return super.ontouchevent (event);}} </span>

Mainactivity as follows:

<span style= "Font-family:microsoft Yahei;" >package Com.example.dispatchtest;import Android.os.bundle;import Android.app.activity;import Android.view.layoutinflater;import Android.view.menu;import Android.view.motionevent;import Android.view.View; Import Android.view.view.onclicklistener;import Android.widget.toast;public class Mainactivity extends Activity Implements onclicklistener{@Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.activity_main); MyView MyView = (MyView) Findviewbyid (R.id.myview); Myview.setbackgroundresource (R.drawable.ic_launcher); Myview.setonclicklistener (This), Findviewbyid (R.id.myviewgroup). Setonclicklistener (this);} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;} @Overridepublic boolean dispatchtouchevent (motionevent ev) {//TODO auto-generated method stubsYstem.out.println ("Mainactivity dispatchtouchevent"); return super.dispatchtouchevent (EV);} @Overridepublic boolean ontouchevent (Motionevent event) {//TODO auto-generated method StubSystem.out.println (" Mainactivity ontouchevent "); return super.ontouchevent (event);} @Overridepublic void OnClick (View v) {//TODO auto-generated method Stubswitch (V.getid ()) {case R.ID.MYVIEWGROUP:TOAST.M Aketext (Mainactivity.this, "Myviewgroup", Toast.length_short). Show (); Break;case R.id.myview:toast.maketext ( Mainactivity.this, "MyView", Toast.length_short). Show (); break;default:break;}}} </span>

This time is the toast content is MyView, if the Myviewgroup onintercepttouchevent return value to False, then the value of toast is myviwgroup. This is because when the Onintercepttouchevent return value is true, the touch event is not passed down and left to Myviewgroup to handle itself.

The distribution of the event is from top to bottom layer (activity--viewgroup--view)

The handling of the event is transmitted from the bottom to the next level (view--viewgroup--activity)

For example, to change the ontouchevent return value of MyView to False, the MyView does not handle touch events, processed by Myviewgroup, and the content of the toast is myviewgroup.



Android Event distribution 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.