above

The process by which click events are passed from an Activity to a ViewGroup and how they are distributed by the ViewGroup is very important. It is important to understand the distribution of events in a ViewGroup because it involves a problem we often solve: sliding collisions.

# Android event distribution Activity Distributes events

# Android event distribution ViewGroup distribution events

Now let’s review the last part.

The body of the

In our usual development, for custom View, we are generally inherited to the View or ViewGroup has been implemented, for direct inheritance to View is relatively few, just recently did a project, which has a custom View inherited to View, so this aspect of knowledge needs to deepen.

Event distribution to View is also distributed to the end, this part is also relatively easy.

View’s dispatchTouchEvent method

Let’s take a look at the View dispatchTouchEvent code:

//View dispatchTouchEvent method key code
ListenerInfo li = mListenerInfo;
// Determine whether to set TouchListener
if(li ! =null&& li.mOnTouchListener ! =null
        && (mViewFlags & ENABLED_MASK) == ENABLED
        && li.mOnTouchListener.onTouch(this, event)) {
    result = true;
}
// Check the onTouchEvent return value
if(! result && onTouchEvent(event)) { result =true;
}
Copy the code

So this is basically all the code, where the return value of dispatchTouchEvent is whether or not the click event is consumed, This return value directly affects whether the View above it needs to call its own processing logic (super.DispatchTouchEvent for viewGroups, onTouchEvent for activities).

Three judgment conditions

So the first if here has three criteria, and if all of them are true then you don’t execute onTouchEvent anymore, so remember that.

  • mOnTouchListener ! = null, that is, the View sets OnTouchListener.

  • (mViewFlags & ENABLED_MASK) == ENABLED, this View is clickable, for views, it is clickable by default.

  • Montouchlistener. onTouch returns true.

If you set the TouchListener to the View, you must return true in onTouch. If you set the TouchListener to the View, you must return true in onTouch.

When one of the three conditions is not null, the onTouchEvent method is executed.

View’s onTouchEvent method

The onTouchEvent method we use a lot when we’re customizing the View, and this is called inside the dispatchTouchEvent method, which is actually handling the MotionEvent event. Note two things. The first one is handling the UP event. Whether the ClickListener callback is needed, the other is assisted by gesture recognition classes for various complex gestures and actions.

Overall flow chart

View distribution is relatively simple, mainly is used in two places, one is the event has been sent to the uppermost View, then the View instead of the ViewGroup to call dispatchTouchEvent and onTouchEvent to complete the logic; One is that when the ViewGroup dispatches an event to a child View, the event is not consumed, that is, the child View’s dispatchTouchEvent returns false, or it clicks on a blank area of the ViewGroup, which also calls the event distribution of the ViewGroup’s parent class, the View.

Here’s a nice copy from another blog to illustrate the flow:

conclusion

Android event distribution as the basis of a custom View is a must to master, which is very clever design ideas, if not carefully understand clearly, in dealing with event interception and other views will be a little confused.

Here we also focus on the chain of responsibility idea, ViewGroup distribution and interception events, View listener priority order, etc. We will elaborate on the importance of these event distribution principles later.