Android event Distribution

Source: Internet
Author: User

Android event Distribution

Among the four Android components (Activity, Service, BroadCast, and ContentProvider), Activity is the most commonly used. Because Activity is responsible for providing intuitive pages and responding to user operations. In the layout file of an Activity, the ViewGroup (layout) on the outermost layer is nested, and the layout ends to the View (Control) to form a variety of user pages. Such as QQ. During the development of these pages, it is inevitable that there will be some event conflicts (in other words: a layout or control you want to click, and the response is another layout or control. What should I do? This introduces the questions we will discuss next.

When the mobile phone responds to the user's click operation, it follows certain rules from the Activity portal to submit the corresponding event to the specified ViewGroup or View to respond (consume this event ). Only when the rules are identified will you be able to handle such incidents again in the future. From the program perspective, Android provides three methods to handle event problems: dispatchTouchEvent (distribution Click Event), onInterceptTouchEvent (interception Click Event), and onTouchEvent (processing click events ). Returns true or false for each method to indicate whether to process its responsibilities. For example, if I intercept an event, it indicates that I want to handle the event and prevent other controls from receiving the event signal. In addition to true or false, the logic of the parent class can be executed by calling the method of the parent class. In short, the Activity, ViewGroup, and View classes process events. The related methods are dispatchTouchEvent, onInterceptTouchEvent, onTouchEvent, And the return values of each method can be true, false, and super. Note: Activity and View do not intercept event methods.

Compile a test demo, use LinearLayout to nest RelativeLayout in the layout file, and finally include a Button in RelativeLayout. Logs are output in each event processing method in sequence. Click the button to test the event distribution process through log information analysis. Is the layout effect.

For easy description, custom naming rules: Activity abbreviation A, LinearLayout abbreviation L, RelativeLayout abbreviation R, Button abbreviation B, dispatchTouchEvent abbreviation D, onInterceptTouchEvent abbreviation I, onTouchEvent abbreviation T, returns true to T, false to F, and super to S. For example, ADS indicates the dispatchTouchEvent method return super in the Activity.

Test Solution 1: ADS/ATS, LDS/LIS/LTS, RDS/RIS/RTS, BDS/BTS

The log is as follows:

 

12:44:44 04-01. 402: D/Activity (1234): dispatchTouchEvent ACTION_DOWN04-01 12:44:44. 402: D/MyLinearLayout (1234): dispatchTouchEvent ACTION_DOWN04-01 12:44:44. 402: D/MyLinearLayout (1234): onInterceptTouchEvent ACTION_DOWN04-01 12:44:44. 402: D/MyRelativeLayout (1234): dispatchTouchEvent ACTION_DOWN04-01 12:44:44. 402: D/MyRelativeLayout (1234): onInterceptTouchEvent ACTION_DOWN04-01 12:44:44. 402: D/MyButton (1234): dispatchTouchEvent ACTION_DOWN04-01 12:44:44. 402: D/MyButton (1234): onTouchEvent ACTION_DOWN04-01 12:44:44. 506: D/Activity (1234): dispatchTouchEvent ACTION_UP04-01 12:44:44. 506: D/MyLinearLayout (1234): dispatchTouchEvent ACTION_UP04-01 12:44:44. 506: D/MyLinearLayout (1234): onInterceptTouchEvent ACTION_UP04-01 12:44:44. 506: D/MyRelativeLayout (1234): dispatchTouchEvent ACTION_UP04-01 12:44:44. 506: D/MyRelativeLayout (1234): onInterceptTouchEvent ACTION_UP04-01 12:44:44. 506: D/MyButton (1234): dispatchTouchEvent ACTION_UP04-01 12:44:44. 506: D/MyButton (1234): onTouchEvent ACTION_UP04-01 12:44:44. 506: D/Button (1234): Click

 

The three-color arrow solid line indicates that the method returns a value and jumps to the next method. Both "true" returned by the Button and "super" returned by the button consume the event. If "true" is returned, the Button click event is no longer returned.

Test Solution 2: ADS/ATS, LDS/LIS/LTS, RDS/RIS/RTS, BDS/BTF

 

04-01 14:00:54.970: D/Activity(1387): dispatchTouchEvent ACTION_DOWN04-01 14:00:54.970: D/MyLinearLayout(1387): dispatchTouchEvent ACTION_DOWN04-01 14:00:54.970: D/MyLinearLayout(1387): onInterceptTouchEvent ACTION_DOWN04-01 14:00:54.970: D/MyRelativeLayout(1387): dispatchTouchEvent ACTION_DOWN04-01 14:00:54.970: D/MyRelativeLayout(1387): onInterceptTouchEvent ACTION_DOWN04-01 14:00:54.970: D/MyButton(1387): dispatchTouchEvent ACTION_DOWN04-01 14:00:54.970: D/MyButton(1387): onTouchEvent ACTION_DOWN04-01 14:00:54.970: D/MyRelativeLayout(1387): onTouchEvent ACTION_DOWN04-01 14:00:54.970: D/MyLinearLayout(1387): onTouchEvent ACTION_DOWN04-01 14:00:54.970: D/Activity(1387): onTouchEvent ACTION_DOWN04-01 14:00:55.066: D/Activity(1387): dispatchTouchEvent ACTION_UP04-01 14:00:55.066: D/Activity(1387): onTouchEvent ACTION_UP

Note: When onTouchEvent return false of a Button, the clicking event will return to the previous view. If no control has ever processed this event, it will be handed over to Activitiy's onTouchEvent for consumption. When an event is passed in again, each space is processed once instead of following the previous process. Instead, the onTouchEvent of the Activity is consumed directly. The dotted line.

 

Test Solution 3: ADS/ATS, LDS/LIS/LTS, RDS/RIS/RTS, BDT/BTF

 

04-01 14:12:47.918: D/Activity(1444): dispatchTouchEvent ACTION_DOWN04-01 14:12:47.918: D/MyLinearLayout(1444): dispatchTouchEvent ACTION_DOWN04-01 14:12:47.918: D/MyLinearLayout(1444): onInterceptTouchEvent ACTION_DOWN04-01 14:12:47.918: D/MyRelativeLayout(1444): dispatchTouchEvent ACTION_DOWN04-01 14:12:47.918: D/MyRelativeLayout(1444): onInterceptTouchEvent ACTION_DOWN04-01 14:12:47.918: D/MyButton(1444): dispatchTouchEvent ACTION_DOWN04-01 14:12:48.030: D/Activity(1444): dispatchTouchEvent ACTION_UP04-01 14:12:48.030: D/MyLinearLayout(1444): dispatchTouchEvent ACTION_UP04-01 14:12:48.030: D/MyLinearLayout(1444): onInterceptTouchEvent ACTION_UP04-01 14:12:48.030: D/MyRelativeLayout(1444): dispatchTouchEvent ACTION_UP04-01 14:12:48.030: D/MyRelativeLayout(1444): onInterceptTouchEvent ACTION_UP04-01 14:12:48.030: D/MyButton(1444): dispatchTouchEvent ACTION_UP

 

Note: The Button dispatchTouchEvent method returns true, indicating that the event needs to be consumed. It is not passed to the onTouchEvent method.

Test Solution 4: ADS/ATS, LDS/LIS/LTS, RDS/RIS/RTS, BDF/BTF

 

04-01 14:19:40.014: D/Activity(1502): dispatchTouchEvent ACTION_DOWN04-01 14:19:40.014: D/MyLinearLayout(1502): dispatchTouchEvent ACTION_DOWN04-01 14:19:40.014: D/MyLinearLayout(1502): onInterceptTouchEvent ACTION_DOWN04-01 14:19:40.014: D/MyRelativeLayout(1502): dispatchTouchEvent ACTION_DOWN04-01 14:19:40.014: D/MyRelativeLayout(1502): onInterceptTouchEvent ACTION_DOWN04-01 14:19:40.014: D/MyButton(1502): dispatchTouchEvent ACTION_DOWN04-01 14:19:40.014: D/MyRelativeLayout(1502): onTouchEvent ACTION_DOWN04-01 14:19:40.014: D/MyLinearLayout(1502): onTouchEvent ACTION_DOWN04-01 14:19:40.014: D/Activity(1502): onTouchEvent ACTION_DOWN04-01 14:19:40.022: D/Activity(1502): dispatchTouchEvent ACTION_UP04-01 14:19:40.022: D/Activity(1502): onTouchEvent ACTION_UP

 

Note: The "Button dispatchTouchEvent" method returns false, and the event is passed to the onTouchEvent method of the parent control.

Test Solution 5: ADS/ATS, LDS/LIS/LTS, RDS/RIS/RTT, BDF/BTF

 

04-01 14:26:03.306: D/Activity(1561): dispatchTouchEvent ACTION_DOWN04-01 14:26:03.306: D/MyLinearLayout(1561): dispatchTouchEvent ACTION_DOWN04-01 14:26:03.306: D/MyLinearLayout(1561): onInterceptTouchEvent ACTION_DOWN04-01 14:26:03.306: D/MyRelativeLayout(1561): dispatchTouchEvent ACTION_DOWN04-01 14:26:03.306: D/MyRelativeLayout(1561): onInterceptTouchEvent ACTION_DOWN04-01 14:26:03.306: D/MyButton(1561): dispatchTouchEvent ACTION_DOWN04-01 14:26:03.306: D/MyRelativeLayout(1561): onTouchEvent ACTION_DOWN04-01 14:26:03.366: D/Activity(1561): dispatchTouchEvent ACTION_UP04-01 14:26:03.366: D/MyLinearLayout(1561): dispatchTouchEvent ACTION_UP04-01 14:26:03.366: D/MyLinearLayout(1561): onInterceptTouchEvent ACTION_UP04-01 14:26:03.366: D/MyRelativeLayout(1561): dispatchTouchEvent ACTION_UP04-01 14:26:03.366: D/MyRelativeLayout(1561): onTouchEvent ACTION_UP

Note: The onTouchEvent method return true of RelativeLayout consumes the event. The next time an event is generated, it is not passed to the Button and directly handled by the onTouchEvent method of RelativeLayout.

 

Test Solution 6: ADS/ATS, LDS/LIS/LTS, RDS/RIS/RTF, BDF/BTF

 

04-01 14:39:20.794: D/Activity(1644): dispatchTouchEvent ACTION_DOWN04-01 14:39:20.798: D/MyLinearLayout(1644): dispatchTouchEvent ACTION_DOWN04-01 14:39:20.798: D/MyLinearLayout(1644): onInterceptTouchEvent ACTION_DOWN04-01 14:39:20.798: D/MyRelativeLayout(1644): dispatchTouchEvent ACTION_DOWN04-01 14:39:20.798: D/MyRelativeLayout(1644): onInterceptTouchEvent ACTION_DOWN04-01 14:39:20.798: D/MyButton(1644): dispatchTouchEvent ACTION_DOWN04-01 14:39:20.798: D/MyRelativeLayout(1644): onTouchEvent ACTION_DOWN04-01 14:39:20.798: D/MyLinearLayout(1644): onTouchEvent ACTION_DOWN04-01 14:39:20.798: D/Activity(1644): onTouchEvent ACTION_DOWN04-01 14:39:20.874: D/Activity(1644): dispatchTouchEvent ACTION_UP04-01 14:39:20.874: D/Activity(1644): onTouchEvent ACTION_UP

NOTE: If RelativeLayout return false, the event will be transmitted to the previous control for consumption. Among them, RelativeLayout onInterceptTouchEvent return false indicates that the event is also distributed to the Button for processing.

Test Solution 7: ADS/ATS, LDS/LIS/LTS, RDS/RIT/RTF, BDF/BTF

 

04-01 15:02:41.594: D/Activity(1739): dispatchTouchEvent ACTION_DOWN04-01 15:02:41.594: D/MyLinearLayout(1739): dispatchTouchEvent ACTION_DOWN04-01 15:02:41.594: D/MyLinearLayout(1739): onInterceptTouchEvent ACTION_DOWN04-01 15:02:41.594: D/MyRelativeLayout(1739): dispatchTouchEvent ACTION_DOWN04-01 15:02:41.594: D/MyRelativeLayout(1739): onInterceptTouchEvent ACTION_DOWN04-01 15:02:41.594: D/MyRelativeLayout(1739): onTouchEvent ACTION_DOWN04-01 15:02:41.594: D/MyLinearLayout(1739): onTouchEvent ACTION_DOWN04-01 15:02:41.594: D/Activity(1739): onTouchEvent ACTION_DOWN04-01 15:02:41.714: D/Activity(1739): dispatchTouchEvent ACTION_UP04-01 15:02:41.714: D/Activity(1739): onTouchEvent ACTION_UP

Note: The Return Value of RelativeLayout onInterceptTouchEvent is true, indicating that the event information is intercepted by RelativeLayout. The Button does not respond to the event and the event is handled by RelativeLayout onTouchEvent.

 

Test Solution 8: ADS/ATS, LDS/LIS/LTS, RDT/RIT/RTF, BDF/BTF

 

04-01 15:27:17.002: D/Activity(1907): dispatchTouchEvent ACTION_DOWN04-01 15:27:17.002: D/MyLinearLayout(1907): dispatchTouchEvent ACTION_DOWN04-01 15:27:17.002: D/MyLinearLayout(1907): onInterceptTouchEvent ACTION_DOWN04-01 15:27:17.002: D/MyRelativeLayout(1907): dispatchTouchEvent ACTION_DOWN04-01 15:27:17.058: D/Activity(1907): dispatchTouchEvent ACTION_UP04-01 15:27:17.058: D/MyLinearLayout(1907): dispatchTouchEvent ACTION_UP04-01 15:27:17.058: D/MyLinearLayout(1907): onInterceptTouchEvent ACTION_UP04-01 15:27:17.058: D/MyRelativeLayout(1907): dispatchTouchEvent ACTION_UP

Description: RelativeLayout dispatchTouchEvent return true indicates that the event is consumed by itself and is not transmitted.

 

Test Solution 9: ADS/ATS, LDS/LIS/LTS, RDF/RIT/RTF, BDF/BTF

 

04-01 15:32:12.138: D/Activity(1965): dispatchTouchEvent ACTION_DOWN04-01 15:32:12.138: D/MyLinearLayout(1965): dispatchTouchEvent ACTION_DOWN04-01 15:32:12.138: D/MyLinearLayout(1965): onInterceptTouchEvent ACTION_DOWN04-01 15:32:12.138: D/MyRelativeLayout(1965): dispatchTouchEvent ACTION_DOWN04-01 15:32:12.138: D/MyLinearLayout(1965): onTouchEvent ACTION_DOWN04-01 15:32:12.138: D/Activity(1965): onTouchEvent ACTION_DOWN04-01 15:32:12.250: D/Activity(1965): dispatchTouchEvent ACTION_UP04-01 15:32:12.250: D/Activity(1965): onTouchEvent ACTION_UP

Note: RelativeLayout dispatchTouchEvent return false indicates that the event is not processed and is handed over to the upper page for processing.

 

Test Solution 10: ADT/ATS, LDS/LIS/LTS, RDF/RIT/RTF, BDF/BTF

 

04-01 15:43:24.558: D/Activity(2023): dispatchTouchEvent ACTION_DOWN04-01 15:43:24.686: D/Activity(2023): dispatchTouchEvent ACTION_UP

Note: like the dispatchTouchEvent method of ViewGroup and View, return true indicates that you consume events by yourself. The difference is that the dispatchTouchEvent method return false of the Activity also indicates that the event is consumed by itself.

 

Test Solution 11: ADS/ATS, LDS/LIS/LTS, RDF/RIT/RTF, BDF/BTF

 

04-01 15:56:51.486: D/Activity(2149): dispatchTouchEvent ACTION_DOWN04-01 15:56:51.486: D/MyLinearLayout(2149): dispatchTouchEvent ACTION_DOWN04-01 15:56:51.486: D/MyLinearLayout(2149): onInterceptTouchEvent ACTION_DOWN04-01 15:56:51.486: D/MyRelativeLayout(2149): dispatchTouchEvent ACTION_DOWN04-01 15:56:51.486: D/MyLinearLayout(2149): onTouchEvent ACTION_DOWN04-01 15:56:51.486: D/Activity(2149): onTouchEvent ACTION_DOWN04-01 15:56:51.490: D/Activity(2149): dispatchTouchEvent ACTION_UP04-01 15:56:51.490: D/Activity(2149): onTouchEvent ACTION_UP

Note: in fact, the onTouchEvent method passed to the Activity will be handed over to it for event processing.

 

 

The overall improvement is as follows:

Summary:

1. The dispatchTouchEvent method of the Activity. When the dispatchTouchEvent method of the parent class is executed, the event is distributed to the ViewGroup. Otherwise, the event is consumed.

2. When the event is passed to the onTouchEvent method of the Activity again, it means that all the controls on the page do not respond to the event, and the event will be processed by the Activity.

3. The dispatchTouchEvent method of ViewGroup and View. return true indicates that the event is not distributed and is processed by itself. Return false: the event is passed to the onTouchEvent method of the Upper-layer control, and the event passed by the upper-layer control is no longer responded.

4. The onInterceptTouchEvent method return true of ViewGroup indicates to intercept the event and pass the event to its onTouchEvent method for processing.

5. The onTouchEvent method return false of View indicates that the event is not consumed, and the event is passed to the onTouchEvent method of the Upper-layer control. Otherwise, consume the event.

6. The onTouchEvent method return true of ViewGroup indicates that the event is consumed. Otherwise, the event is passed to the upper-Layer Control or the onTouchEvent method of the Activity for processing.

7. When the control does not process events, it no longer receives the next event message, as shown in the dotted line.

Finally, I wish you a happy fool's day.

 

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.