Analysis of Android Event Distribution Mechanism (I)

I haven't had time to write before. Now I'm going to write an article about event distribution mechanism. There must be many and better articles on this kind of network than mine. I just try my best to clarify them and let me reorganize them.

Association and Difference of onTouch, onClick, onTouchEvent

  • onTouch method
  myButton.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.i(TAG, "onTouch" + event.getAction());
                return false;
            }
        });

This is what we usually use. We use event.getAction() to judge user ACTION_DOWN, ACTION_UP, ACTION_MOVE operations. What's the difference between that and onTouchEvent()? Let's look at the onTouchEvent() method.

 @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.i(TAG, "onTouchEvent" + event.getAction());
        return true;
    }

Finding that it can also have event can also judge user operation, so how should we use it and which one to use? Here I want to talk about another method.

   @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        Log.i(TAG, "dispatchTouchEvent" + event.getAction());
        return super.dispatchTouchEvent(event);
    }

What's the use of this method? Well, this method is called first when we touch the control view. It serves as a distribution of touch events. Is that right? Let's look at the log I printed.

09-01 13:22:40.313 2985-2985/com.xu.startservice I/==========>: dispatchTouchEvent0
09-01 13:22:40.313 2985-2985/com.xu.startservice I/==========>: onTouch0
09-01 13:22:40.313 2985-2985/com.xu.startservice I/==========>: onTouchEvent0
09-01 13:22:40.334 2985-2985/com.xu.startservice I/==========>: dispatchTouchEvent2
09-01 13:22:40.334 2985-2985/com.xu.startservice I/==========>: onTouch2
09-01 13:22:40.334 2985-2985/com.xu.startservice I/==========>: onTouchEvent2
09-01 13:22:40.352 2985-2985/com.xu.startservice I/==========>: dispatchTouchEvent2
09-01 13:22:40.352 2985-2985/com.xu.startservice I/==========>: onTouch2
09-01 13:22:40.352 2985-2985/com.xu.startservice I/==========>: onTouchEvent2
09-01 13:22:40.354 2985-2985/com.xu.startservice I/==========>: dispatchTouchEvent1
09-01 13:22:40.354 2985-2985/com.xu.startservice I/==========>: onTouch1
09-01 13:22:40.354 2985-2985/com.xu.startservice I/==========>: onTouchEvent1
09-01 13:22:40.358 2985-2985/com.xu.startservice I/==========>: onClick

0 for Down 2 for Move 3 for UP
Because I shook my hand when I clicked the button, there were two 2. But we can see from the sequence of log events that the order of execution methods is:
dispatchTouchEvent=>onTouch=>onTouchEvent=>onClick
Why? Let's take a look at the source code of the dispatchTouchEvent method, which has a lot of code that we can only see useful:

 ListenerInfo li = mListenerInfo;
            if (li != null && li.mOnTouchListener != null
                    && (mViewFlags & ENABLED_MASK) == ENABLED
                    && li.mOnTouchListener.onTouch(this, event)) {
                result = true;
            }

            if (!result && onTouchEvent(event)) {
                result = true;
            }

mOnTouchListener This is our setOnTouchListener(new View.OnTouchListener()) TouchListener. mViewFlags & ENABLED_MASK== ENABLED This is a description of whether button s can be clicked.
li.mOnTouchListener.onTouch(this, event) This is the value we return in the onTouch() method. So we can see from the parameters that when li.mOnTouchListener.onTouch(this, event) returns true, the onTouchEvent cannot be executed, which becomes so.

09-01 13:22:40.313 2985-2985/com.xu.startservice I/==========>: dispatchTouchEvent0
09-01 13:22:40.313 2985-2985/com.xu.startservice I/==========>: onTouch0
09-01 13:22:40.334 2985-2985/com.xu.startservice I/==========>: dispatchTouchEvent2
09-01 13:22:40.334 2985-2985/com.xu.startservice I/==========>: onTouch2
09-01 13:22:40.354 2985-2985/com.xu.startservice I/==========>: dispatchTouchEvent1
09-01 13:22:40.354 2985-2985/com.xu.startservice I/==========>: onTouch1

So the onTouch() method has a higher level than onTouchEvent(), and its return value determines whether onTouchEvent() is executed.

Keywords: network

Added by iamngk on Tue, 28 May 2019 01:00:18 +0300