Android event distribution mechanism instance validation

Source: Internet
Author: User

Android event distribution mechanism instance validation

Welcome reprint, Reproduced please indicate the source http://blog.csdn.net/lavor_zl/article/details/51198634, thank you.

The event distribution mechanism is mainly learned from the following two blog posts:

    • Android Event delivery mechanism
    • The distribution and consumption mechanism of Touch events under Android programming

      These two blog posts are very good, but after reading these two posts there are still some areas of misunderstanding, lacking a hint of enlightenment. Then personally write down the code of several cases, to see the results of event distribution, so as to verify the event distribution mechanism. After the verification of an instant with a sense of enlightenment, feel enlightened. Paper to the end of the light, I know this matter to preach.

My program has a custom mylayout layout in mainactivity, mylayout layout below has a custom mybutton.

Scenario 1


PS: In a table, super represents a method that returns the same name in the parent class – No such method is represented, and no return value indicates that the return type of the method is void.

Click the non-MyButton area and the results are as follows:

Click on the MyButton area and the results are as follows:

With these two results we can find that the event is activity->viewgroup->view from the top down.
However, there is a non-correspondence between the two results, that is, the first result of the Mylayout Onintercepttouchevent method is not executed when lifting, and the second result is executed. This is because the Dispatchtouchevent method in ViewGroup calls the Onintercepttouchevent method only when it is pressed, or if the event continues to distribute downward.

Scenario 2

Click the non-MyButton area and the results are as follows:

Click on the MyButton area and the results are as follows:

The output of scenario 2 is significantly less than that of 1, because the Onintercepttouchevent method and Ontouchevent method in Mylayout are called in the Dispatchtouchevent method in ViewGroup. The return value of the Dispatchtouchevent method in the following mylayout is false, not the super.dispatchtouchevent (EV), The Dispatchtouchevent method in ViewGroup is not called. Because the return value is False, the event is returned to the ancestor's Ontouchevent method for consumption without further distribution, so the Ontouch method and the OnClick method in Mylayout are not executed. This results in case 2 clicking on the non-MyButton area below is the same as clicking on the MyButton area.

Scenario 3

Click the non-MyButton area and the results are as follows:

Click on the MyButton area and the results are as follows:

The output of case 3 is significantly less than the case 2, because the return value of the Dispatchtouchevent method in case 3 Mylayout is true, and the event is consumed by the Dispatchtouchevent method in Mylayout. Will not be passed up by the superior ontouchevent method for consumption, so 3 of the output results than the case 2 less than 1.

Scenario 4

Click the non-MyButton area and the results are as follows:

Click on the MyButton area and the results are as follows:

The output of case 4 is exactly the same as in case 1, because the return value in the Onintercepttouchevent method in Mylayout is false, one is Super.dispatchtouchevent (event), There is nothing in ViewGroup's Dispatchtouchevent method, that is, the return value is false.

Scenario 5

Click the non-MyButton area and the results are as follows:

Click on the MyButton area and the results are as follows:

The result of the 5 click on the non-MyButton area and click on the MyButton area is the same because the return value of the Onintercepttouchevent method in Mylayout is true, which means that the event is intercepted, The event is handed over to the current view's ontouchevent for processing, and the event will not continue to be distributed down.

Scenario 6

Click the non-MyButton area and the results are as follows:

Click on the MyButton area and the results are as follows:

In case 6, click on the non-MyButton area and click on the non-MyButton region results in case 1, this is because the Ontouchevent method return value in case 6 is false, indicating that the current view is not resolved, Will pass the event up to the superior ontouchevent method processing. We found that when the event was lifted, Mainactivity did not distribute the event to Mylayout because, in the event of a previous press, Mainactivity distributed the event to Mylayout, and the event was not successfully processed before mylayout, so when the event was lifted , mainactivity does not distribute events to mylayout. However, the next time the event is lifted, Mainactivity will distribute the event to Mylayout because the memory mylayout has not handled the event successfully before the element is emptied by the parent's Dispatchtouchevent method each time it raises an event.

Scenario 7

Click the non-MyButton area and the results are as follows:

Click on the MyButton area and the results are as follows:

In case 7, click on the non-MyButton area to click on the MyButton area of the non-the result is less than 1, because the return value of the Ontouchevent method in Mylayout is true, indicating that the current view accepts and consumes the event. However, the Ontouchevent method in view is not invoked at this time, and the Click event cannot be responded to without calling the method.

Scenario 8

Click the non-MyButton area and the results are as follows:

Click on the MyButton area and the results are as follows:

In case 8, click on the non-MyButton area to click on the MyButton area than the result of clicking on the non-Mylayout region, this is because the Ontouch method return value is true, if the Ontouch method return value of a view is true, the end is If the return value is false, the view's Ontouchevent method is called.

Scenario 9

Click the non-MyButton area and the results are as follows:

Click on the MyButton area and the results are as follows:

Scenario 10

Click the non-MyButton area and the results are as follows:

Click on the MyButton area and the results are as follows:

Summarize

This example verifies the touch event analysis in the distribution and consumption mechanism of the touch event in Android programming.

The following is a reference to the Touch Event Analysis section of this blog post:

Event Distribution: Public boolean dispatchtouchevent (motionevent ev)

When the Touch event occurs, the Dispatchtouchevent (motionevent ev) method of the Activity passes the event to the outermost layer by tunneling (passing from the root element down to the topmost child element or in an intermediate element because a condition stops passing). View's dispatchtouchevent (motionevent ev) method, and the event is distributed by the view's dispatchtouchevent (Motionevent ev) method. The event distribution logic for Dispatchtouchevent is as follows:

    • If return true, the event is distributed to the current View and consumed by the Dispatchtouchevent method, while the event stops passing down;
    • If return is false, event distribution is divided into two scenarios:
      * If the current View gets events directly from the activity, the event is returned to the activity's ontouchevent for consumption;
      * If the current view gets an event from the outer parent control, the event is returned to the parent view's ontouchevent for consumption.
    • If the system default super.dispatchtouchevent (EV) is returned, the event is automatically distributed to the current View's Onintercepttouchevent method.

Event interception: Public boolean onintercepttouchevent (motionevent ev)

When the dispatchtouchevent (motionevent ev) method of the outer view returns to the system's default super.dispatchtouchevent (EV) case, the event is automatically distributed to the current view's Onintercepttouchevent method. The Onintercepttouchevent event interception logic is as follows:

    • If Onintercepttouchevent returns True, the event is intercepted and the intercepted event is referred to the current View's ontouchevent for processing;
    • If Onintercepttouchevent returns FALSE, the event is released, the event on the current view is passed to the child view, and then the dispatchtouchevent of the view begins the distribution of the event;
    • If Onintercepttouchevent returns super.onintercepttouchevent (EV), the event is intercepted by default and the intercepted event is referred to the current View's ontouchevent for processing.

Event Response: Public boolean ontouchevent (motionevent ev)

The Dispatchtouchevent returns super.dispatchtouchevent (EV) and onintercepttouchevent returns TRUE or returns Super.onintercepttouchevent ( EV), the ontouchevent will be called. The event response logic for Ontouchevent is as follows:

    • If the event is passed to the current view's Ontouchevent method, and the method returns False, the event is passed up from the current view and is received by the ontouchevent of the upper view, if passed to the ontouchevent above Also returns false, the event "disappears" and the next event is not received.
    • If True is returned, the event is received and consumed.
    • If the return super.ontouchevent (EV) default handles the logic of the event and returns false, the same.

Situation 6,7,8 There are some knowledge points outside the summary, to be aware.

Android event distribution mechanism instance validation

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.