The event interception mechanism of the Android Development series

Source: Internet
Author: User

For Android developers to understand the importance of the event delivery mechanism, I think it should be self-evident. In an activity, we often rewrite the Ontouchevent event, but after the rewrite is over, it is confusing to return TRUE or return false. When the mood is good, return true, when the mood is bad, return false.

To fully understand the event interception mechanism, we first need to understand the role of these methods: Dispatchtouchevent (motionevent ev), onintercepttouchevent (motionevent ev), Ontouchevent (motionevent ev).

First, let's throw a question, what happens when we click on a button in the LinearLayout and follow the event-passing mechanism? Will find viewgroup inside, we can rewrite the above three methods, but in a separate sub-view, we can only rewrite: dispatchtouchevent (motionevent ev), Ontouchevent (motionevent EV). Why is it? So here's what we're going to do to each of these three methods.

The dispatchtouchevent (motionevent ev) method acts as a distribution event, and when an event arrives at the current view, view calls the method to distribute the event, correctly passing it to the control that can handle the response. The parameter motionevent that the method carries has encapsulated the response parameter information of the event.

The onintercepttouchevent (motionevent ev) method action is whether to intercept the event, when an event arrives at the view, if the view overrides the method returns True, then the view is able to handle the response to the event, Then the event will not continue to pass down the surface. If the overridden method returns false, the event will continue to be passed to the child control on the lower side, eventually reaching the child view that can respond.

The ontouchevent (motionevent ev) method is where the response event is processed, and a Boolean value is returned when the method executes, except that the Boolean value is to determine if a view is needed to feedback information to the parent. Just like when you complete a task, you need to report the results to the project manager. If it returns true, then the feedback is not needed and I have handled it well. If False is returned, then it is necessary to give the parent view feedback so that it determines the final result state.

Of course, the above three method parameters are motionevent, indicating that the parameter information of the event will be carried.

The event interception mechanism of the Android Development series

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.