Event distribution test results for Android

Source: Internet
Author: User

Simple version, no energy to improve more things.

Test object:

Eventactivity: Rewritten dispatchtouchevent and Ontouchevent

EventViewGroup1: Rewritten dispatchtouchevent and Onintercepttouchevent and Ontouchevent

EventViewGroup2: Rewritten dispatchtouchevent and Onintercepttouchevent and Ontouchevent

Eventview: Rewritten dispatchtouchevent and Ontouchevent

Test Case one: Dispatchtouchevent and Onintercepttouchevent and ontouchevent Three overridden methods are returned SUPER.XXX

The output is:

06-15 08:14:56.962 1695-1695/com.aprz.delak e/eventtest:eventactivity---dispatchtouchevent---

06-15 08:14:56.962 1695-1695/com.aprz.delak e/eventtest:eventviewgroup1---dispatchtouchevent---

06-15 08:14:56.962 1695-1695/com.aprz.delak e/eventtest:eventviewgroup1---onintercepttouchevent---

06-15 08:14:56.962 1695-1695/com.aprz.delak e/eventtest:eventviewgroup2---dispatchtouchevent---

06-15 08:14:56.962 1695-1695/com.aprz.delak e/eventtest:eventviewgroup2---onintercepttouchevent---

06-15 08:14:56.962 1695-1695/com.aprz.delak E/eventtest:eventview---dispatchtouchevent---

06-15 08:14:56.963 1695-1695/com.aprz.delak E/eventtest:eventview---ontouchevent---

06-15 08:14:56.963 1695-1695/com.aprz.delak e/eventtest:eventviewgroup2---ontouchevent---

06-15 08:14:56.963 1695-1695/com.aprz.delak e/eventtest:eventviewgroup1---ontouchevent---

06-15 08:14:56.963 1695-1695/com.aprz.delak e/eventtest:eventactivity---ontouchevent---

06-15 08:14:57.070 1695-1695/com.aprz.delak e/eventtest:eventactivity---dispatchtouchevent---

06-15 08:14:57.070 1695-1695/com.aprz.delak e/eventtest:eventactivity---ontouchevent---

06-15 08:14:57.086 1695-1695/com.aprz.delak e/eventtest:eventactivity---dispatchtouchevent---

06-15 08:14:57.086 1695-1695/com.aprz.delak e/eventtest:eventactivity---ontouchevent---

06-15 08:14:57.103 1695-1695/com.aprz.delak e/eventtest:eventactivity---dispatchtouchevent---

06-15 08:14:57.103 1695-1695/com.aprz.delak e/eventtest:eventactivity---ontouchevent---

The last only print eventacticity is because: EventViewGroup1 and EventViewGroup2 and EventViewGroup1 are not consumption events, so the subsequent events will not be passed down, the activity of their own processing.

To prove the above conclusion, do such a test, let EventViewGroup2 to consume this event (return true in ontouchevent), the output prints the result as follows:

06-15 08:16:52.590 1800-1800/com.aprz.delak e/eventtest:eventactivity---dispatchtouchevent---

06-15 08:16:52.590 1800-1800/com.aprz.delak e/eventtest:eventviewgroup1---dispatchtouchevent---

06-15 08:16:52.590 1800-1800/com.aprz.delak e/eventtest:eventviewgroup1---onintercepttouchevent---

06-15 08:16:52.590 1800-1800/com.aprz.delak e/eventtest:eventviewgroup2---dispatchtouchevent---

06-15 08:16:52.590 1800-1800/com.aprz.delak e/eventtest:eventviewgroup2---onintercepttouchevent---

06-15 08:16:52.590 1800-1800/com.aprz.delak E/eventtest:eventview---dispatchtouchevent---

06-15 08:16:52.590 1800-1800/com.aprz.delak E/eventtest:eventview---ontouchevent---

06-15 08:16:52.590 1800-1800/com.aprz.delak e/eventtest:eventviewgroup2---ontouchevent---

06-15 08:16:52.703 1800-1800/com.aprz.delak e/eventtest:eventactivity---dispatchtouchevent---

06-15 08:16:52.703 1800-1800/com.aprz.delak e/eventtest:eventviewgroup1---dispatchtouchevent---

06-15 08:16:52.703 1800-1800/com.aprz.delak e/eventtest:eventviewgroup1---onintercepttouchevent---

06-15 08:16:52.703 1800-1800/com.aprz.delak e/eventtest:eventviewgroup2---dispatchtouchevent---

06-15 08:16:52.703 1800-1800/com.aprz.delak e/eventtest:eventviewgroup2---ontouchevent---

06-15 08:16:52.720 1800-1800/com.aprz.delak e/eventtest:eventactivity---dispatchtouchevent---

06-15 08:16:52.720 1800-1800/com.aprz.delak e/eventtest:eventviewgroup1---dispatchtouchevent---

06-15 08:16:52.721 1800-1800/com.aprz.delak e/eventtest:eventviewgroup1---onintercepttouchevent---

06-15 08:16:52.721 1800-1800/com.aprz.delak e/eventtest:eventviewgroup2---dispatchtouchevent---

06-15 08:16:52.721 1800-1800/com.aprz.delak e/eventtest:eventviewgroup2---ontouchevent---

06-15 08:16:52.736 1800-1800/com.aprz.delak e/eventtest:eventactivity---dispatchtouchevent---

06-15 08:16:52.736 1800-1800/com.aprz.delak e/eventtest:eventviewgroup1---dispatchtouchevent---

06-15 08:16:52.736 1800-1800/com.aprz.delak e/eventtest:eventviewgroup1---onintercepttouchevent---

06-15 08:16:52.736 1800-1800/com.aprz.delak e/eventtest:eventviewgroup2---dispatchtouchevent---

06-15 08:16:52.736 1800-1800/com.aprz.delak e/eventtest:eventviewgroup2---ontouchevent---

06-15 08:16:52.753 1800-1800/com.aprz.delak e/eventtest:eventactivity---dispatchtouchevent---

06-15 08:16:52.753 1800-1800/com.aprz.delak e/eventtest:eventviewgroup1---dispatchtouchevent---

06-15 08:16:52.753 1800-1800/com.aprz.delak e/eventtest:eventviewgroup1---onintercepttouchevent---

06-15 08:16:52.753 1800-1800/com.aprz.delak e/eventtest:eventviewgroup2---dispatchtouchevent---

06-15 08:16:52.753 1800-1800/com.aprz.delak e/eventtest:eventviewgroup2---ontouchevent---

06-15 08:16:52.770 1800-1800/com.aprz.delak e/eventtest:eventactivity---dispatchtouchevent---

06-15 08:16:52.770 1800-1800/com.aprz.delak e/eventtest:eventviewgroup1---dispatchtouchevent---

06-15 08:16:52.770 1800-1800/com.aprz.delak e/eventtest:eventviewgroup1---onintercepttouchevent---

06-15 08:16:52.770 1800-1800/com.aprz.delak e/eventtest:eventviewgroup2---dispatchtouchevent---

06-15 08:16:52.770 1800-1800/com.aprz.delak e/eventtest:eventviewgroup2---ontouchevent---

You can see the subsequent events, each pass to the EventViewGroup2 will not pass to the following, which is also explained:

The Dispatchtouchevent () method has the function of "memory", if the first event is passed down to a view, and it continues to pass the event to its child view, it will record whether the event was successfully processed by the view below it, (how can I know?). If the event is passed up again to me here for my ontouchevent (), it means that the view below is not able to handle the event successfully); When the second event is passed down to the view, the view's Dispatchtouchevent () Method opportunity to judge, if the last event was successfully processed by the following view, then the event will continue to the following to deal with, if the last event is not successfully processed by the following, then this event will not be passed down, the view directly call their own ontouchevent () method to handle the event.

The information of the "memory" function is only valid until a series of events have been completed, such as from the Action_down event, until the end of the subsequent event action_move,action_up, the "Memory" information will be erased. That is, if a view processing Action_down event fails (Ontouchevent () returns false), subsequent events such as ACTION_MOVE,ACTION_UP are no longer passed to the view, which is handled by the parent view itself. The next time the Action_down event occurs, it will still be delivered to the view.

Test Scenario Two: Look at EventViewGroup1 's dispatchtouchevent returns True, what happens?

06-15 07:11:02.797 2154-2154/com.aprz.delak e/eventtest:eventactivity---dispatchtouchevent---

06-15 07:11:02.797 2154-2154/com.aprz.delak e/eventtest:eventviewgroup1---dispatchtouchevent---

06-15 07:11:02.938 2154-2154/com.aprz.delak e/eventtest:eventactivity---dispatchtouchevent---

06-15 07:11:02.938 2154-2154/com.aprz.delak e/eventtest:eventviewgroup1---dispatchtouchevent---

06-15 07:11:02.952 2154-2154/com.aprz.delak e/eventtest:eventactivity---dispatchtouchevent---

06-15 07:11:02.952 2154-2154/com.aprz.delak e/eventtest:eventviewgroup1---dispatchtouchevent---

06-15 07:11:02.968 2154-2154/com.aprz.delak e/eventtest:eventactivity---dispatchtouchevent---

06-15 07:11:02.969 2154-2154/com.aprz.delak e/eventtest:eventviewgroup1---dispatchtouchevent---

06-15 07:11:02.984 2154-2154/com.aprz.delak e/eventtest:eventactivity---dispatchtouchevent---

06-15 07:11:02.985 2154-2154/com.aprz.delak e/eventtest:eventviewgroup1---dispatchtouchevent---

It can be seen that the event was consumed by EventViewGroup1 dispatchtouchevent.

Test Scenario Three: Look at EventViewGroup1 's dispatchtouchevent return false, what happens?

06-15 07:16:51.100 2358-2358/com.aprz.delak e/eventtest:eventactivity---dispatchtouchevent---

06-15 07:16:51.100 2358-2358/com.aprz.delak e/eventtest:eventviewgroup1---dispatchtouchevent---

06-15 07:16:51.100 2358-2358/com.aprz.delak e/eventtest:eventactivity---ontouchevent---

06-15 07:16:51.168 2358-2358/com.aprz.delak e/eventtest:eventactivity---dispatchtouchevent---

06-15 07:16:51.168 2358-2358/com.aprz.delak e/eventtest:eventactivity---ontouchevent---

06-15 07:16:51.184 2358-2358/com.aprz.delak e/eventtest:eventactivity---dispatchtouchevent---

06-15 07:16:51.184 2358-2358/com.aprz.delak e/eventtest:eventactivity---ontouchevent---

06-15 07:16:51.201 2358-2358/com.aprz.delak e/eventtest:eventactivity---dispatchtouchevent---

06-15 07:16:51.201 2358-2358/com.aprz.delak e/eventtest:eventactivity---ontouchevent---

The event is returned to the upper source, which is ontouchevent for processing.

Test situation four: Look at EventViewGroup2 's dispatchtouchevent return false, what happens?

06-15 07:20:03.892 2463-2463/com.aprz.delak e/eventtest:eventactivity---dispatchtouchevent---

06-15 07:20:03.892 2463-2463/com.aprz.delak e/eventtest:eventviewgroup1---dispatchtouchevent---

06-15 07:20:03.892 2463-2463/com.aprz.delak e/eventtest:eventviewgroup1---onintercepttouchevent---

06-15 07:20:03.892 2463-2463/com.aprz.delak e/eventtest:eventviewgroup2---dispatchtouchevent---

06-15 07:20:03.892 2463-2463/com.aprz.delak e/eventtest:eventviewgroup1---ontouchevent---

06-15 07:20:03.892 2463-2463/com.aprz.delak e/eventtest:eventactivity---ontouchevent---

06-15 07:20:04.002 2463-2463/com.aprz.delak e/eventtest:eventactivity---dispatchtouchevent---

06-15 07:20:04.002 2463-2463/com.aprz.delak e/eventtest:eventactivity---ontouchevent---

06-15 07:20:04.018 2463-2463/com.aprz.delak e/eventtest:eventactivity---dispatchtouchevent---

06-15 07:20:04.019 2463-2463/com.aprz.delak e/eventtest:eventactivity---ontouchevent---

Events are returned to the upper layer to deal with, if the upper level is not processed, continue to return upward, until the activity, you can see activity and viewgroup is not the same, even if the event is not handled, will still receive the next event.

The final conclusions are as follows:

? Event Distribution:publicboolean 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:
      1. If the current View gets events directly from the activity, the event is returned to the activity's ontouchevent for consumption;
      2. 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 you return the system's default Super. Dispatchtouchevent (EV), the event is automatically distributed to the current View's Onintercepttouchevent method.

? Event interception:publicboolean 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 same as when returning false.

? Event Response:publicboolean ontouchevent (motionevent ev)

Ontouchevent is called when Dispatchtouchevent returns Super.dispatchtouchevent (EV) and Onintercepttouchevent returns True. 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.

Event distribution test results for Android

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.