Dispatchtouchevent (), onintercepttouchevent () and ontouchevent () event Distribution

Source: Internet
Author: User

In the layout file, assume there are three layers: buttons, textview, and other common components; relativelayout and linearlayout. A touch screen event is nothing more than action_down, action_move, action_up. however, when the finger is pressed to loosen the screen, the event distribution has passed through these three layers. let's talk about its processing process, which is also a matter of consolidating memory.

First, three methods, dispatchtouchevent (), onintercepttouchevent (), and ontouchevent (), are available in each view. No matter the buttons, textview, linearlayout, or relativelayout methods, they ultimately inherit from the view, viewgroup, so these three methods each have. dispatchtouchevent () is responsible for the distribution of touch-screen events (down-Move-up), onintercepttouchevent () is responsible for the interception of the event, ontouchevent () is the event processing.

Next, let's talk about how the three methods in the three layers are involved in this process:

Starting from the activity, you receive the finger-pressed screen event, that is, action_down, which first reaches linearlayout. linearlayout calls dispatchtouchevent (). dispatchtouchevent () is generally not used for rewriting, it is automatically distributed to onintercepttouchevent (). If fasle is returned in action_down of onintercepttouchevent (), it means that it does not process the event, but is handed over to the next one to process it, therefore, we continue to distribute this event to the next layer of relativelayout. Similarly, in relativelayout, if the action_down of onintercepttouchevent () still returns false, it will arrive at textview. if true is returned in onintercepttouchevent () of textview, it means that the event is handled by the user, and the event is intercepted. after processing, if true is returned, the event processing result is submitted to the activity in reverse order and the activity is ready to process. then the next action_move and action_up events will be uploaded to textview and processed.

If the first action_down is processed in relativelayout (that is, onintercepttouchevent () in relativelayout returns true and is handled by ontouchevent, the textview in the lower layer cannot participate in the delivery and distribution of this touch event. The subsequent action_move and action_up will make textview unable to be passed ). so if you want to intercept a touch action, you can get started with onintercepttouchevent .) as long as action_down is intercepted, the subsequent move and up events will not be passed down, but only the move event will be intercepted, the up event will not be passed down. however, no matter which view handles this touch event, the final processing result must be submitted to the activity in reverse order, so that the user can obtain the corresponding feedback after touching the screen.

Dispatchtouchevent (), onintercepttouchevent () and ontouchevent () event Distribution

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.