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