Summary of event distribution mechanism in Android

Source: Internet
Author: User

The event distribution mechanism for Android

First, View Summary of event distribution:

Ontouchevent and Ontouch differences in view
Take the example of a custom Testbutton.

We can handle messages such as down move up by overriding the Ontouchevent method:

public class Testbutton extends Button {

Publictestbutton (Context context) {

Super (context);

TODO auto-generated Constructor stub

}

Publictestbutton (context context, AttributeSet AttributeSet) {

Super (Context,attributeset);

TODO auto-generated Constructor stub

}

@Override

Publicboolean ontouchevent (Motionevent event) {

Booleanvalue = Super.ontouchevent (event);

System.out.println ("super.ontouchevent:" + value+ "event:" + event.getaction ());

returnvalue;

}

You can also achieve the same goal by implementing the Ontouchlistener interface, and then setting the Testbutton Ontouchlistener

Class Ontouchlistenertest implements view.ontouchlistener{

@Override

Publicboolean OnTouch (View V, motionevent event) {

Returnfalse;

}

}

Testbutton B = (Testbutton) Findviewbyid (R.id.button);

Ontouchlistenertest listener = new Ontouchlistenertest ();

B.setontouchlistener (listener);

But what is the difference between these two types of monitoring?

First look at the Android source code in the view of the implementation of Dispatchtouchevent:

Publicboolean dispatchtouchevent (Motionevent event) {

......

if (onfiltertoucheventforsecurity (event)) {

Listenerinfoli = Mlistenerinfo;

if (li!= null && li.montouchlistener! = null && (mviewflags &enabled_mask) = = ENABLED

&&li.montouchlistener.ontouch (this, event)) {

Returntrue;

}

if (Ontouchevent (event)) {

Returntrue;

}

}

......

Returnfalse;

}


You can see that the priority of the Ontouchlistener interface is higher than ontouchevent, if the Ontouch method in Ontouchlistener returns True,

Said the incident has been consumed, the ontouchevent is not receiving news.

Because the button's PerformClick is implemented using Ontouchevent, if ontouchevent is not called, then the button's Click event is not responding.


In general terms:

Ontouchlistener's Ontouch method has a higher priority than ontouchevent, which is triggered first.

If the Ontouch method returns False, then the ontouchevent is triggered, and the Ontouchevent method is not called.

Built-in implementations such as the Click event are based on Ontouchevent, and if Ontouch returns True, these events will not be triggered.

Second, ViewGroup Summary of event distribution:

1. The Android event distribution is delivered first to ViewGroup and then by ViewGroup to view.

2. In ViewGroup, event passing can be intercepted by means of the Onintercepttouchevent method, and the Onintercepttouchevent method returns true to indicate that the event is not allowed to continue to pass to the child view. A return of false indicates that the event is not blocked and returns false by default.

3. If the passed event is consumed in the child view, no events will be received in the ViewGroup.

Summary of event distribution mechanism in Android

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.