9: Android Event distribution mechanism

Source: Internet
Author: User

1:

? Public Boolean dispatchtouchevent (Motioneventev) This method distributes touchevent

? Public booleanonintercepttouchevent (motionevent ev) This method intercepts touchevent

? Public Boolean ontouchevent (motionevent ev) This method handles touchevent

The view class has Dispatchtouchevent and Ontouchevent two methods, viewgroup inherit view, and a new Onintercepttouchevent method is added. There are no onintercepttouchevent methods in activity, but there are two other ways

1. Action_down is first passed to the onintercepttouchevent () method

2. If the viewgroup onintercepttouchevent () on receiving the Span style= "COLOR: #222222" >down After event processing is complete return false< Span style= "COLOR: #222222", then the subsequent move, Upviewgroupdownviewontouchevent () processing.

3. If the viewgroup onintercepttouchevent () on receiving the Span style= "COLOR: #222222" >down After event processing is complete return true< Span style= "COLOR: #222222", then the subsequent move, Uponintercepttouchevent () down event passed to the viewgroup< Span style= "COLOR: #222222" > ontouchevent () processing, note that the target Span style= "COLOR: #222222" >view will not receive any events.

4.view< Span style= "COLOR: #222222" > ontouchevent () returned falseview< Span style= "COLOR: #222222" > ontouchevent () processing.

5. if the ontouchevent () of the view that eventually needs to handle the event returns true, then subsequent events will continue to be passed to the view 's Ontouchevent () processing.

When the touch event Action_down occurs, the Dispatchtouchevent function in activity is called first, and then the Action_down event is passed to the Dispatchtouchevent function in ViewGroup. The Action_down event in Dispatchtouchevent in ViewGroup is then passed to the Onintercepttouchevent function in the call ViewGroup, which is responsible for intercepting the Action_down event. Because ViewGroup also contains child view, the default return value is False, which means that this action_down event is not intercepted. If False is returned, the Action_down event continues to be passed to its child view. Because child view is not a ViewGroup control, the Action_down event is then passed to ontouchevent for processing events. At this point, the delivery of the message basically ends. From the above can be analyzed, the Motionevent event is passed by tunnel mode. Tunnel mode, which is passed from the root element down to the most inner child element or in an intermediate element because a condition stops passing.

The next step is to analyze and handle the event. Just now the Action_down event was passed to the view's Ontouchevent function, which returns true by default, and then the dispatchtouchevent of the view returns TRUE. Then the ViewGroup dispatchtouchevent returns True, and the last activity Dispatchtouchevent returns true. We found that the Motionevent event was handled in a bubbling fashion. The bubbling way, which is passed from the most inner child element to the root element, or to an element in the middle, in order to stop passing by a certain condition.

9: Android Event distribution mechanism

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.