I. Dispatchtouchevent,onintercepttouchevent,ontouchevent difference
- Dispatchtouchevent is the first interface to receive an event, whether in view or ViewGroup ;
- onintercepttouchevent Intercept interface in ViewGroup , because the interception is only to not distribute the event to the child view, there is no child view in the view;
- ontouchevent interface is where the event is finally processed;
ontoucheventreturns ture, representing the event being processed, and subsequent move,up events will be distributed to the view to continue processing; ontoucheventReturns FALSE, the subsequent Move,up event is not distributed to the view;
two. Onintercepttouchevent detailedThis method is implemented to intercept all screen touch motion events. It can observe all the events that are distributed to the child view and have ownership of the current gesture at all times. The methodreturns true to intercept an event that viewgroup to a child view, false instead. Simply put.onintercepttouchevent handles events (similar to preprocessing, and can of course not) and alters the direction in which events are passed, that is, deciding whether to allow touch events to continue down (child controls) . One but returns True (representing the event being processed in the current viewgroup), the path down passes is truncated (all child controls will not have the opportunity to participate in the touch event), and the event is passed to the current control's ontouchevent () processing; returns false. The event is given to the child control's onintercepttouchevent (). Use this feature with great care, because he will interact with view.ontouchevent (motionevent) quite complex and require both methods to be implemented well. The events are received in the following order:
- The down event is received within the method.
- Down event either quilt view processing is not handled by ViewGroup own Ontouchevent () method. The deal means that ontoucheevent() returns True, and you continue to receive other gestures (rather than looking for the parent view to handle it). If ViewGroup's ontoucheevent() returns True, the next event will not be received in onintercepttouchevent (), and all touch processing occurs as normal ViewGroup in the ontouchevent ().
- For cases where false is returned, all subsequent events (up to the final up event) are passed here before being passed to the target's Ontouchevent () (Translator Note: The ontouchevent that returns True).
- If it returns true, the method will not receive all subsequent events: The target view receives the Cancel event, and all subsequent events are distributed to the ViewGroup ontouchevent () method, and the event does not appear here.
Action_cancelEvent: When the current control (child control, son) receives a precursor event (Action_move or Action_move), its parent control (dad) suddenly intervenes, truncating the delivery of the event, at which point the current control receives Action_cancel, and after receiving this event, Regardless of whether the child control returns TRUE or false at this time, it is assumed that this action is complete and will not be passed back to the parent control's ontouchevent, and subsequent events will be processed directly to the parent control through the Dispatchevent method. This is a bit contrary to the previous conclusion that if the ontouchevent of the child control returns false, indicating that the event has not been processed, is to be passed back to the parent control to handle, here to correct, only the Action_down event can be returned, Action_move and ACTION The _up event follows the Action_down event, that is, which control is handled by Action_down, and subsequent events are passed here, not on the parent control, and Action_cancel cannot be returned. Also, the scope of the touch area problem, if the touch area within the child control, while the parent control is not truncated event delivery, just regardless of whether the child control is intercepting this event, will be passed to the child control ontouchevent processing, can be regarded as a responsibility, because I ordered you, your father did not intercept, He doesn't want to deal with it, and it's up to you to deal with it, whether you stop it or not.
Conclusion: TheAction_cancel event is generated when a predecessor event is received and the subsequent event is intercepted by the parent control, and the Ontouchevent event is passed back to the parent control only in the Action_down event
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android event handling mechanism