Android _ Events

Source: Internet
Author: User

Android _ Events

What you should know about the event is
When an event is generated, its transmission process follows the following sequence: Activity> Window> View
The event comes from the activity. If you haven't rewritten any method about the event, the activity will pass the event to the window, and the window will pass the event to the decorView.

Now let's take a look at the situation where we re-wrote the dispatchEvent method in the activity.

We will return the dispatchTouchEvent in the activity to true.

The event is consumed by the dispatchTouchEvent of the activity.
The onTouchEvent method does not receive a response. Let's take a look at the log output.

Obviously, the log only prints the related methods in the dispatchTouchEvent, and the entire event is consumed by the dispatchTouchEvent. It is not passed to our window> decorView. All the return keys on my interface cannot accept the event, therefore, the Click Event of the return button does not take effect either,

We are returning the dispatchTouchEvent in the activity to false to see what the situation is ,,This is also a question for me.That is, if the dispatchTouchEvent in my activity returns false, the event indicates that no one handles the event. At this time, the root cause of the event comes from the activity, this event cannot return its parent view, and the onTouchEvent () of the current activity has no log output. Where is the event going ????

Then let's take a look at the log output that returns false, for example:

Log outputs a method that executes the dispatchTouchEvent in the activity,
TouchEvent does not have any output, indicating that the onTouchEvent of the activity does not receive this event. At this time, I click the return key, and The onclick is still invalid,
So, please help me to answer this question. When the dispatchtouchevent in the activity returns false, what is the event process like ,???

Finally, let the dispatchtouchevent in the activity return the super check.

Then, let's take a look at the log output and the log sequence,

Pay attention to the log order. dispatchTouchEvent down> onTouchEvent down
DispatchTouchEvent move> onTouchEvent move
DispatchTouchEvent up> onTouchEvent up

Pay attention to the questions asked by the interviewer and the execution sequence during the interview.

Then we add a linearlayout to the activity. We will overwrite the three methods related to the event in linearlayout, print the log, and check that no subview is added to linearlayout, simply look at the effect of passing an event from activity to viewgroup.

First, we are writing a TESTLinearlayout, as shown below:

public class AAATestLinearlayout extends LinearLayout {    private static final String TAG = "AAATestLinearlayout";    public AAATestLinearlayout(Context context) {        super(context);    }    public AAATestLinearlayout(Context context, AttributeSet attrs) {        super(context, attrs);    }    public AAATestLinearlayout(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    @Override    public boolean dispatchTouchEvent(MotionEvent ev) {        switch (ev.getAction()) {            case MotionEvent.ACTION_DOWN:                Logger.e(TAG, TAG + ">>>>dispatchTouchEvent.ACTION_DOWN");                break;            case MotionEvent.ACTION_MOVE:                Logger.d(TAG, TAG + ">>>>dispatchTouchEvent.ACTION_MOVE");                break;            case MotionEvent.ACTION_UP:                Logger.w(TAG, TAG + ">>>>dispatchTouchEvent.ACTION_UP");                break;        }//        return true;        return super.dispatchTouchEvent(ev);    }    @Override    public boolean onInterceptTouchEvent(MotionEvent ev) {        switch (ev.getAction()) {            case MotionEvent.ACTION_DOWN:                Logger.e(TAG, TAG + ">>>>onInterceptTouchEvent.ACTION_DOWN");                break;            case MotionEvent.ACTION_MOVE:                Logger.d(TAG, TAG + ">>>>onInterceptTouchEvent.ACTION_MOVE");                break;            case MotionEvent.ACTION_UP:                Logger.w(TAG, TAG + ">>>>onInterceptTouchEvent.ACTION_UP");                break;        }//        return true;        return super.onInterceptTouchEvent(ev);    }    @Override    public boolean onTouchEvent(MotionEvent event) {        switch (event.getAction()) {            case MotionEvent.ACTION_DOWN:                Logger.e(TAG, TAG + ">>>>onTouchEvent.ACTION_DOWN");                break;            case MotionEvent.ACTION_MOVE:                Logger.d(TAG, TAG + ">>>>onTouchEvent.ACTION_MOVE");                break;            case MotionEvent.ACTION_UP:                Logger.w(TAG, TAG + ">>>>onTouchEvent.ACTION_UP");                break;        }        return super.onTouchEvent(event);    }}

We will first let all the linearlayout tests return the super value by default.

Let's take a look at the event passing process from activity> window> decorView> our own linearlayout;

In addition, we set an additional onTouchLinstener in the Construction of linearlayout,
Let's look at the results;

Here is a brief description. We have added a linearlayout to the activity, and added a touchlistener to the linearlayoutset.
We can see from the log that the event is transmitted from the activity, and then all the methods related to the event in linearlayout return super.
OnInterceptTouchEvent
I would like to say two sentences. The returned values of this sentence affect the event transfer of the neutron view in the viewgroup container, and will not affect the onTouchEvent of the current viewgroup or the touchlistener of the set, we can see from the log that the onTouchEvent of linearlayout. both down and touchliste are executed.

If the value of onInterceptTouchEvent returns super, we can see the source code of viewgroup.
The default value is false, that is, do not intercept... What's important is not to intercept ....

    * appear here.     *      *     * @param ev The motion event being dispatched down the hierarchy.     * @return Return true to steal motion events from the children and have     * them dispatched to this ViewGroup through onTouchEvent().     * The current target will receive an ACTION_CANCEL event, and no further     * messages will be delivered here.     */    public boolean onInterceptTouchEvent(MotionEvent ev) {        return false;    }

This is the source code of viewgroup, Which is exactly consistent with the sixth article on page 6 of Android art exploration,
There is another blog, which should be classic. The explanation of onInterceptTouchEvent is as follows;

Here, if the blogger says "super" is returned, it is intercepted by default, and the intercepted event is handed over to the current view for processing,
The current view here is our linearlayoutlayout. From the log, the onTouchEvent is indeed executed. however, this onTouchEvent is not returned because super is not intercepted and executed. because I tried to return true, false, super, which has no effect on the down events of touchlistener and onTouchEvent we set,

So I am also confused for a long time. I read books and consulted the nil in the group. I guess it may be that the blogger understands that there are discrepancies or misoperations, and left the blog address again: please review and correct the original article links as follows:

Next let's talk about our scenario. Currently, the event processing methods in linearlayout do not process the event, so that the subview in linearlayout, for example, our return button, can indeed receive the event, then click the return arrow and the activity is closed. The first scenario is completed.

In the second scenario, we return true for the dispatchTouchEvent in our liearlayout,
We suppose this is the case. If true is returned, the event is terminated in the current method, that is, in liearlayout. Therefore, other methods will not receive this event, so let's take a look at the log output,

Struct + tdq2/rj2s6G + Serial/srCvP69q8rH0ru49tT1w7TR + weight/rSmwO0syOe5 + weight + Weight = "here write picture description" src = "http://www.bkjia.com/uploads/allimg/160403/041R053C-12.png" title = "\"/>

! [Write picture description here] (http://img.blog.csdn.net/20160331173347685)

When we click our return arrow, because the event is returned to activity consumption from liearlayout, The subcontrol cannot receive the event, so clicking the returned arrow does not take effect;

In the third scenario, the onInterceptTouchEvent in linearlayout returns true, and the others return super, conjecture, the interception event returns true, so the event is not passed to the subview,
The return button does not take effect. because all our other methods return to super, the event is returned to its parent view or activity in onTouchEvent. Because onTouchEvent in activity also returns to super, the event disappears ,.. specific process and explanation of log output

The return button is invalid because the event is also in the activity and is not passed to the Child view.

In the next scenario, let onInterceptTouchEvent in our liearlayout return false without interception. The event will be passed to the sub-view, and the sub-view will call its own dispatch method to send a message, in this way, the event can be accepted by the return arrow and the activity can be closed normally.

Let's take a look at the log output,

Because onInterceptTouchevent returns false, the event is passed to the sub-view after the method in the event is executed. Therefore, the event takes effect from the dispatch and intercept in the source activity and linearlayout, there is no onTouchEvent. For details, see the log output description. The returned arrow takes effect because the event is passed to the subview,This method returns false and returns super to produce the same effect. If you are not interested, you can output the result.

So far, the scenario of only one viewgroup in our activity has been basically analyzed. Then we want to add a textview in liearlayout, such

Because here we add the smallest unit of view, that is, the view can no longer add additional content. We touch the screen event transmission process with our fingers, and there is activity to liearlayout. In the view, linearlayout is returned, return activity until it disappears. The specific process is as follows;

In another scenario, this time we asked the dispatch event in childview to return false,
I was a bit vague about the transfer. I can guess the print result when I write this blog today. Because textviewdispatch returns false, therefore, after the dispatch is executed, the false information is directly returned to the onTouchEvent of the parent view linearlayout. The onTouchEvent is executed after touchlistener is set, and then the parent view returns the super, therefore, the onTouchEvent passed to the activity returns super because the onTouchEvent of the activity. so the event disappears and finally the move and up operations are executed in the activity.

Then let's take a look at the printed results;

Here, the fuzzy scenario of the events to be understood has been a little transparent, and more practices are required. If you do not understand the situation in this article, please approve it, all the events I understand end in viewgroup and view. Thanks for the nil in the group. Thank you!

If you still don't quite understand this process after reading it, please simulate the scenario and print the log by yourself, because I don't feel any worse than the author, when you look at log simulation, you may even be able to guess the transfer scenarios of different return values, then you must have a better understanding of the event transfer process than before! Thank you ....

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.