An explanation of the Android event interception processing mechanism

Source: Internet
Author: User

The previous period of time just contact with the development of Android mobile phone, its event propagation mechanism is not very understanding, although the Internet also looked up the relevant information, but always feel vaguely understood, specious, so I wrote a small demo test a bit. Finally figured out its specific mechanism. Write down your conclusions, share them, and hope to help people who are beginners to Android

Layout effect:

Figure 1

Refer to the first to say the concrete results:

1) Onintercepttouchevent is responsible for blocking the touch event, and the first thing to do with nested view is the Onintercepttouchevent method of the outermost view. Then execute the onintercepttouchevent of the child view in turn, and then execute the event interception method in the child view's child view (assuming, of course, that all onintercepttouchevent of the nested view will be executed, Onintercepttouchevent return FALSE for each view). Reference, so the onintercepttouchevent execution order is a--->b--->c--->d. That is, the parent view is passed to the child view. In summary, the event interception mechanism is initiated by the parent view to intercept the event (something happened to me first, son later). Referring to when a finger touches an event, parent view a first initiates an interception of the event, and if a intercept fails, it is intercepted by its sub-view B, and if B interception fails, it is passed to sub-view C of B and then intercepted. Until a child view has successfully intercepted the event.

2) The success or failure of a view interception event is identified as the return value of the Onintercepttouchevent method, when it returns true to indicate that the interception was successful, and that the current view failed to intercept the event when it returns false.

3) The following is a case of interception success, assuming that the C view intercepts the current touch event successfully. The interception success means that this event will no longer be passed to D view. So at this point the D view of the onintercepttouchevent is not running (the event can not be reached, but also to intercept who?) )。 After the event interception succeeds, the event is then processed and the method of processing is taught to the Ontouchevent method. When the C view intercepts successfully, then the Ontouchevent method of C view is executed, does that mean that the current touch event is handled by the C view's Ontouchevent method? This is determined by the return value of the Ontouchevent method of the C view. When the ontouchevent of the C view returns true, the current event is handled by C in its sole discretion, dealing with the various actions of course, what Motionevent.action_move,action_ The up is handed to the C Ontouchevent method for processing. So at this point in the C ontouchevent method of the switch (event.getaction) to determine the execution of the relevant logic. If the return is false, the C view does not handle this event or can not handle it, what to do? No, my son. Dad came, so the incident was in the Ontouchevent method of the B view. The same B to deal with this event or not to see the ontouchevent return value of B, the specific explanation is the same as C, no more words.

4) in the case where both onintercepttouchevent and ontouchevent of a B C D are returned false, the order in which the methods are executed is a.onintercepttouchevent--> B.onintercepttouchevent-->c.onintercepttouchevent-->d.touchevent (Deepest sub-view not rewritten onintercepttouchevent)-- C.touchevent-->b.touchevent-->a.touchevent. That is, the interception event is that the parent view takes precedence over the child view to intercept, and the processing event is the child view precedence parent view for processing.

Summary: Onintercepttouchevent is responsible for intercepting events, and then handing them over to the view that first encountered Ontouchevent return True.


In conjunction with the event source Analysis event processing mechanism, you can read "from the source point of view to analyze the principles of Android events"

The following will explain in detail how the above conclusions are drawn, and prepare to be divided into two parts for a step-by-step explanation. If the above is clear, the following content will not look, because it will be very verbose.

The layout code for Figure 1 looks like this:

[Java]View PlainCopy 
  1. <com.example.demo.aview xmlns:android="Http://schemas.android.com/apk/res/android"
  2. xmlns:tools="Http://schemas.android.com/tools"
  3. Android:layout_width="Match_parent"
  4. android:layout_height="Match_parent" >
  5. <com.example.demo.bview
  6. Android:layout_width="Match_parent"
  7. android:layout_height="Match_parent" >
  8. <com.example.demo.cview
  9. Android:layout_width="Match_parent"
  10. android:layout_height="Match_parent" >
  11. <com.example.demo.dview
  12. Android:layout_width="Match_parent"
  13. android:layout_height="Match_parent"
  14. android:text="Test Demo"/>
  15. </com.example.demo.CView>
  16. </com.example.demo.BView>
  17. </com.example.demo.AView>

The last D is a custom textview, and the difference between a three view and a B c is that D only overrides the Ontouchevent method, and a B C three custom controls also override the Oninterceptevent method.

D code as follows, a B C code basically except the class name and output log is not the same as the rest of the same, so in order to reduce the only one of the paste.

Dview's Code:

[Java]View PlainCopy  
  1. Public class DView extends textview{
  2. private static String tag = "D";
  3. Public DView (context context, AttributeSet attrs, int defstyle) {
  4. Super (context, attrs, Defstyle);
  5. }
  6. Public DView (context context, AttributeSet attrs) {
  7. Super (context, attrs);
  8. }
  9. Public DView (context context) {
  10. super (context);
  11. }
  12. @Override
  13. Public Boolean ontouchevent (Motionevent event) {
  14. LOG.E (Tag, "--ontouchevent--d");
  15. return false;
  16. }
  17. }


The code of Aview is similar to the whole of C D, and it is posted as one:

[Java]View PlainCopy  
  1. Public class Aview extends relativelayout{
  2. private static String tag = "A";
  3. Public Aview (context context) {
  4. super (context);
  5. }
  6. Public Aview (context context, AttributeSet attrs, int defstyle) {
  7. Super (context, attrs, Defstyle);
  8. }
  9. Public Aview (context context, AttributeSet attrs) {
  10. Super (context, attrs);
  11. }
  12. @Override
  13. Public Boolean onintercepttouchevent (motionevent ev) {
  14. LOG.E (Tag,"--onintercepttouchevent--a");
  15. return false;
  16. }
  17. @Override
  18. Public Boolean ontouchevent (Motionevent event) {
  19. LOG.E (Tag,"--ontouchevent---A");
  20. return false;
  21. }
  22. }

At the beginning of the rewrite method all return False to run the Click Effect Output log is:

Conversion becomes:

As you can see from this diagram, the order of execution of the Onintercepttouchevent event is performed by the parent control to the child control and takes precedence over the Ontouchevent method of its own control, and the Ontouchevent event executes exactly the opposite of the control to the parent control. Note that since false is returned, there is no view to handle each action of the touch event, which is why Ontouchevent has been passed to a. So events such as Action_move and action_up are not dealt with, and in this case you will not be executed even if you write the following code in the Ontouchevent method of D.

[Java]View PlainCopy 
    1. if (Event.getaction () ==motionevent.action_move) {
    2. LOG.E (Tag, "--ontouchevent--*****");
    3. }

1) If the intercepttouchevent of a returns true, the rest still returns false, then the log of the execution output is:

Conversion becomes:

It can be found that at this point a intercepts the touch event, and the event no longer passes to the child control B C D of a. At this point all action events such as the finger Move event Action_move or Action_up event are all given to A's Ontouchevent method (which is, of course, in the case where the Ontouchevent method returns True, Returns false if the action is not appropriate when tested. B,c, the D control is an event-handling interception method and event-handling method that cannot be executed.

2) only the Oninterceptetouchevent event of B returns true in the case of the printed log as

Conversion becomes:

At this point, the touch event is intercepted by B and will not be passed to the C-D child control, and the various actions of the event.getaction () of this event will not be processed because the Ontouchevent event returns false.

4) in the same way, the C control's Onintercept method returns True if the remainder of the case still returns false, and the output log is

Converted Into

Let's talk about the ontouchevent of each view returning true.

Since the Ontouchevent event is passed from the child control to the parent control, when the ontouchevent of D returns True, the test output is as follows

Conversion becomes:

The test found that at this point D had handled the various action,c B D of the Touch event, and Ontouchevent was not executed.

Similarly, when the Ontouchevent method of C returns true, the output log is as follows

Converted to the following:

It has been tested that each ACTION of the event is responded to in the CView Ontouchevent method, and the ontouchevent of D is not motionevent.action_xx accordingly. The rest of the time, and so on, is not wordy. After a step-by-step test to get the article at the beginning of the knot is a bit verbose, I hope it can be helpful to people who read this article.


In conjunction with the event source Analysis event processing mechanism, you can read "from the source point of view to analyze the principles of Android events"

An explanation of the Android event interception processing mechanism

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.