Android time Interception Mechanism

Source: Internet
Author: User

Android time Interception Mechanism

For Android event interception mechanisms, it is believed that most Android beginners are hard to understand. In fact, it is not difficult to understand this problem.

First, you understand what the event interception mechanism is like? The event interception mechanism mentioned here refers to the interception mechanism for touch events. What is a touch event? The so-called touch event refers to the event generated by the touch screen captured by the system. When we click the button, three events are generated. Press the button. This is one of the events. If you slide a little, this is the second event. If you lift it up, this is the third event. Android encapsulates a class named MotionEvent for this touch event. In OnTouchEvent events, you can easily listen to these three events.

Now that you can listen to this touch event, let's talk about how to intercept it. So, can you imagine such a scenario? A View is placed in a ViewGroup, and the parent ViewGroup control is placed in another VIewGroup, which can even be nested, so that the child sun will never go through. So what's the problem? There is only one event that can be touched. Who should I give it. Both the ViewGroup and the Child View want to handle this touch event, so the domineering name "event interception" came into being.

To understand the event interception mechanism, I need to place myself in a situation where I put myself in place. Just like a company where you are, there is a CEO, there is a director under the CEO, the director is a manager below, and the manager has a hard nut to crack. Now, after a task is completed, the CEO assigns him to the Director and the Director to the manager, and the manager gives him to you. In this way, the task upload and release process is exactly like the event distribution and interception process.

To better understand the case of events, I will use the control structure to simulate such an organizational structure to explain the event Interception Mechanism in depth.

Let's take a look at the UI architecture of this control:

How can I detect that an event is intercepted between the parent container and the Child control, and listen to OnTouchEvent (), DispatchEvent (), and OninterceptEvent () Events in the parent container (ViewGroup, listen to OnTouchEvent () Events and DispatchEvent () Events in the Child View. We can see how these events are called. We can print the corresponding logs for each event. Well, with such a general idea, we can go to the source code.

The source code of the sub-View is as follows:

public class MyView extends View {    public MyView(Context context) {        super(context);    }    public MyView(Context context, AttributeSet attrs) {        super(context, attrs);    }    public MyView(Context context, AttributeSet attrs,                  int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    @Override    public boolean onTouchEvent(MotionEvent event) {        Log.d("zcw", "View onTouchEvent" + event.getAction());        return super.onTouchEvent(event);    }    @Override    public boolean dispatchTouchEvent(MotionEvent event) {        Log.d("zcw", "View dispatchTouchEvent" + event.getAction());        return super.dispatchTouchEvent(event);    }}

The source code of the parent ViewGroup is as follows:

public class MyViewGroupC extends LinearLayout {    public MyViewGroupC(Context context) {        super(context);    }    public MyViewGroupC(Context context, AttributeSet attrs) {        super(context, attrs);    }    public MyViewGroupC(Context context, AttributeSet attrs,                        int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    @Override    public boolean dispatchTouchEvent(MotionEvent ev) {        Log.d("zcw", "ViewGroupC dispatchTouchEvent" + ev.getAction());        return super.dispatchTouchEvent(ev);    }    @Override    public boolean onInterceptTouchEvent(MotionEvent ev) {        Log.d("zcw", "ViewGroupC onInterceptTouchEvent" + ev.getAction());        return super.onInterceptTouchEvent(ev);    }    @Override    public boolean onTouchEvent(MotionEvent event) {        Log.d("zcw", "ViewGroupC onTouchEvent" + event.getAction());        return super.onTouchEvent(event);    }}

The source code of the parent ViewGroup is as follows:

public class MyViewGroupB extends LinearLayout {    public MyViewGroupB(Context context) {        super(context);    }    public MyViewGroupB(Context context, AttributeSet attrs) {        super(context, attrs);    }    public MyViewGroupB(Context context, AttributeSet attrs,                        int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    @Override    public boolean dispatchTouchEvent(MotionEvent ev) {        Log.d("zcw", "ViewGroupB dispatchTouchEvent" + ev.getAction());        return super.dispatchTouchEvent(ev);    }    @Override    public boolean onInterceptTouchEvent(MotionEvent ev) {        Log.d("zcw", "ViewGroupB onInterceptTouchEvent" + ev.getAction());        return super.onInterceptTouchEvent(ev);    }    @Override    public boolean onTouchEvent(MotionEvent event) {        Log.d("zcw", "ViewGroupB onTouchEvent" + event.getAction());        return super.onTouchEvent(event);    }}

The source code of the Zeng zu ViewGroup control is as follows:

public class MyViewGroupA extends LinearLayout {    public MyViewGroupA(Context context) {        super(context);    }    public MyViewGroupA(Context context, AttributeSet attrs) {        super(context, attrs);    }    public MyViewGroupA(Context context, AttributeSet attrs,                        int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    @Override    public boolean dispatchTouchEvent(MotionEvent ev) {        Log.d("zcw", "ViewGroupA dispatchTouchEvent" + ev.getAction());        return super.dispatchTouchEvent(ev);    }    @Override    public boolean onInterceptTouchEvent(MotionEvent ev) {        Log.d("zcw", "ViewGroupA onInterceptTouchEvent" + ev.getAction());        return super.onInterceptTouchEvent(ev);    }    @Override    public boolean onTouchEvent(MotionEvent event) {        Log.d("zcw", "ViewGroupA onTouchEvent" + event.getAction());        return super.onTouchEvent(event);    }}

We have buried points in every event we want to listen.

We run the program to see the sequence of event execution for each control:

 

When we touch the child View, we find that the parent Control DispatchEvent on the top layer is executed, and the onInterceptTouchEvent event is executed first. When the OnTouchEvent method in the layer is fully executed, the OnTouchEvent method is executed from the inside out. The execution flow chart is as follows:

Let's slightly modify the control code. If the onTouchEvent method returns true, the running effect is like this.

 

We can come up with the conclusion that, after return true, We will intercept the event and do not pass down the event. This is the essence of android event interception.

Return false -- if the event is laissez-faire, the event will be transmitted, and return true -- if you do it yourself, you will not bother others, the event will be intercepted, and the event will not be transmitted.

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.