Android_ Touch Event Delivery mechanism

Source: Internet
Author: User

Android in Dispatchtouchevent,onintercepttouchevent, ontouchevent understanding EC
The types of events in Android are key events and screen touch events, and touch events are the underlying events of screen touch events, and it is necessary to understand them in depth. One of the simplest screen touch actions triggered a series of touch events:action_down->action_move->action_move->action_move...->action_move-> Action_up
Android event handling is divided into 3 steps.
1) public booleandispatchtouchevent (motionevent ev) This method is used to distribute TouchEvent 2) public boolean onintercepttouchevent ( Motionevent ev) This method is used to intercept touchevent 3) public boolean ontouchevent (motionevent ev) This method is used to handle touchevent
Suppose the current activity layout is as follows:


Dispatchtouchevent Event Distribution
When touchevent occurs, the activity first passes the touchevent to the topmost view, TouchEvent first to the dispatchtouchevent of the topmost view. It is then distributed by the Dispatchtouchevent method, and if Dispatchtouchevent returns True, the ontouchevent processing of the view is given, and if Dispatchtouchevent returns False, The Onintercepttouchevent method of the view to decide whether to intercept this event,
If Onintercepttouchevent returns True, that is, it is intercepted, it is given to its ontouchevent to handle, if Onintercepttouchevent returns false, then passed to the child view, the Dispatchtouchevent again to begin the distribution of this event.




event blocker: onintercepttouchevent
Onintercepttouchevent is used to change the direction in which events are passed. The return value that determines the direction of delivery is returned as false when the event is passed to the child control, and the return value is true when the event is passed to the current control's ontouchevent (), which is known as intercept (intercept).
[Tisa PS: The correct way to use this method is to only determine if an event needs to be intercepted and then return. Even if interception is required, it should return true directly and then be processed by the Ontouchevent method. ]
Ontouchevent is used to handle events, and the return value determines whether the current control consumes (consume) the event. In particular, for the Action_down event, returns True, indicating that I want to handle subsequent events (Action_move or ACTION_UP); returns false, which indicates that this event is not cared for and is returned by the parent class for processing.
In cases where onintercepttouchevent () and ontouchevent () are not overridden (their return values are false), the order of the Motionevent events is passed as follows:


When a control's onintercepttouchevent () return value is true, truncation occurs, and the event is passed to the current control's ontouchevent (). If we return the LayoutView2 onintercepttouchevent () value to True, then the delivery process becomes:



If we set LayoutView2 's Onintercepttouchevent () and Ontouchevent () to true at the same time, then LAYOUTVIEW2 will consume the event that is passed, while subsequent events (such as following Action_ Down Action_move or ACTION_UP) is passed directly to LayoutView2 's ontouchevent () and does not pass any function to any other control. A Action_cancel event is passed to the subspace at the same time. The transfer process becomes (no Action_cancel event is drawn in the diagram):



Small summary: Onintercepttouchevent is self-rootiew downward passing, ontouchevent just the opposite.

Android_ Touch Event Delivery mechanism

Related Article

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.