The processing of Android touch events mainly involves several methods: Onintercepttouchevent (), Dipatchtouchevent (), Ontouchevent (), OnTouch ().
Onintercepttouchevent () is used to intercept events and change the direction of event delivery. Explain the event delivery. For example, an activity that shows a user may be a multi-layered nesting of viewgroup and view, by default the touch event is delivered from the outermost layer to the innermost level, and then begins to respond from the innermost layer. From the innermost layer, the dispatchtouchevent () is called at each level to distribute, and dispatchtouchevent () responds to touch events in the call Ontouch/ontouchevent.
The Onintercepttouchevent () method truncates the delivery of the touch event so that the touch event is not passed below a layer and begins to call the layer's dispatchtouchevent () and begins to return to the upper level. If a layer needs to be intercepted, the Onintercepttouchevent () method of the layer needs to be replicated and the method returns True. Explain it through a picture.
Suppose an activity shows an interface that has a->b->c->d four layers, and when the event occurs, first passes through A's onintercepttouchevent (), if A's onintercepttouchevent () Returns false, which is passed to the onintercepttouchevent () of B, and is passed down (the inner layer) once if False is returned. If the onintercepttouchevent () of a layer returns true, then the disatchtouchevent () distribution event for that layer is called, and the event is no longer passed down. If there are no interception events at each layer, call Dispatchtouchevent () from the topmost layer, and if the dispatchtouchevent () of each layer returns true, then the event is consumed by that layer, then the dispatchtouevent () of the upper layer Will not be called. To give an example:
The onintercepttouchevent () in layer B returns true, then the event is intercepted, and the dispatchtouchevent () that starts the call to Layer B () returns one Response touch event.
Do you know what the egg is for this mechanism?
A simple example, we put a picture in the ScrollView, the picture allows zooming and dragging, but you drag the picture when you will find ScrollView also follow the move, so the experience will be very bad, how to do?
Combined with the above analysis, we can know that if the touch event passed to the inner layer of the image, and then in the picture of the dispatchtouchevent () the touch event consumption is not OK?
The specific procedure in the Ontouch () method of the picture, Action_down set ScrollView does not intercept the event, by Scrollview.requestdisallowintercepttouchevent (true) to complete, After you finish the desired processing, the Ontouch () method of the picture returns true at the end of the process.
What's the difference between OnTouch (View V, motionevent event) and Ontouchevent (Motionevent event)? Looks like that, wouldn't it be the same function? This is not superfluous, why not a good.
To figure this out, you first need to look at the Dispatchtouchevent () method in the view:
Public Booleandispatchtouchevent (Motionevent event) {//If The event should is handled by accessibility focus first. if(Event.istargetaccessibilityfocus ()) {//We don't have a focus or no virtual descendant has it, does not handle the event. if(!Isaccessibilityfocusedvieworhost ()) { return false; } //We have a focus and got the event, then use Normal event dispatch.Event.settargetaccessibilityfocus (false); } Booleanresult =false; if(Minputeventconsistencyverifier! =NULL) {minputeventconsistencyverifier.ontouchevent (event,0); } Final intactionmasked =event.getactionmasked (); if(actionmasked = =Motionevent.action_down) { //defensive Cleanup for new gestureStopnestedscroll (); } if(Onfiltertoucheventforsecurity (event)) {//noinspection simplifiableifstatementListenerinfo Li =Mlistenerinfo; if(Li! =NULL&& Li.montouchlistener! =NULL&& (Mviewflags & enabled_mask) = =ENABLED&& Li.montouchlistener. OnTouch ( This, event)) {Result=true; } if(!result && ontouchevent(event)) {Result=true; } } if(!result && Minputeventconsistencyverifier! =NULL) {minputeventconsistencyverifier.onunhandledevent (event,0); } //nested Scrolls If this is the end of a gesture; //also cancel it if we tried an action_down but we didn ' t want the rest//of the gesture. if(actionmasked = = Motionevent.action_up | |actionmasked= = Motionevent.action_cancel | |(actionmasked= = Motionevent.action_down &&!result)) {Stopnestedscroll (); } returnresult;}
It can be seen from the above that Ontouch () executes before ontouchevent (). By looking at the source of the view also found, or simply reasoning that we set Setontouchlistener () set the Who will know, when we need to call the Ontouch () method to make it work rather than let Ontouchevent () to respond. View source can be seen Ontouch is a interface way to implement, the implementation of processing logic to the developer to customize, so you can draw the Android system comes with the control we use Ontouch processing events, if we need to expand view, A ontouchevent () is required to implement the event processing. In fact Ontouch (view V, motionevent event) and Ontouchevent (Motionevent event) can be seen differently from the parameters accepted by both methods, Ontouch contains a parameter of the View type, So it can be set to a view, and Ontouchevent () is for a particular view.
Now that the view's event response is mentioned, how does the onclick event respond? Through the production of source code can be found that the onclick is executed in the ontouchevent, but also in the ontouchevent of the Action_up event executed. Therefore, if the view's Ontouch () returns True, it causes the onclick to not execute, because Ontouchevent () is not executed.
There is also a ontouchevent () member method in the activity, and if the view in the activity does not handle the event then the activity's ontouchevent () is called.
Android Touch Event handling mechanism