Delivery rules for the distribution of Android click events

We analyzed the source code of the click event distribution mechanism earlier. Got it, the relationship between 3 important methods. The following is a simple representation with pseudocode:

public boolean dispatchTouchEvent(MotionEvent ev) {
	boolean result = false;
	if(onInterceptTouchEvent(ev)) {
		result = onTouchEvent(ev);
	} else {
		result = child.dispatchTouchEvent(ev);
	}
	return result;
}

Both the onInterceptTouchEvent and onTouchEvent methods are called in the dispatchTouchEvent method. Now let's analyze the delivery rules of click event distribution based on this pseudo code.

First of all, let’s talk about the top-down transmission rules of the click event. When the click event is generated, it will be processed by the Activity, passed to PhoneWindow, then passed to DecorView, and finally passed to the top-level ViewGroup. Generally, in event delivery, only the onInterceptTouchEvent method of ViewGroup is considered, because in general, we will not rewrite the dispatchTouchEvent method. For the root ViewGroup, the tap event is first passed to its dispatchTouchEvent method. If the ViewGroup's onInterceptTouchEvent method returns true, it means that the event is to be intercepted, and the event will be handed over to its onTouchEvent method for processing. If the onIntercepterTouchEvent method returns false, it means that the event will not be intercepted, and the event will be handed over to the dispatchTouchEvent of its child elements for processing. And so on and on. If it is passed to the bottom view, which has no sub-views, then the dispatchTouchEvent method of the View will be called. In general, the View's onTouchEvent method will eventually be called.

Liu Wangshu used the example of martial arts, we can refer to:

Assuming someone comes to Wudang to make trouble, the news is first reported to the head Zhang Sanfeng, Zhang Sanfeng will not go out in person, so he handed over the task to Song Yuanqiao, one of the Seven Heroes of Wudang, that is to say, Zhang Sanfeng's onInterceptTouchEvent() returns false, Song Yuanqiao He felt that no matter what he was, he was still the Seven Heroes of Wudang, and he didn't need to do it himself, so he handed over the task to Wudang disciple Song Qingshu. That is, Song Yuanqiao's onInterceptTouchEvent returns false, Song Qingshu has no men, and he can only fight by himself. Here, we compare Zhang Zhenren to the top-level ViewGroup, and Song Yuanqiao, one of the Wudang Seven Heroes, to the middle-level ViewGroup. Song Qingshu, a disciple of Wudang, compares the underlying View. Therefore, we come to the conclusion that the rules for passing the return value of events from top to bottom:

If the onInterceptTouchEvent method returns true, it will intercept and not continue to pass down; if the onInterceptTouchEvent method returns false, it will not intercept and continue to pass down.

Let's talk about the bottom-up transmission process of the click event. When the click event is passed to the underlying View, if its onTouchEvent returns true, the event is consumed and processed by the underlying View. If onTouchEvent returns false, it means that the view will not be processed. And pass it to the onTouchEvent method of the parent View for processing. If the onTouchEvent method of the parent View still returns false, it will continue to be passed to the View processing of the parent View. And so on and on.

Continuing with the classic example of martial arts: When Song Qingshu found out that the invading enemy was Cheng Kun, he was not Cheng Kun's opponent at all. He couldn't handle it, that is, his onTouchEvent returned false, so he ran to find Song Yuanqiao. When Song Yuanqiao heard that it was Cheng Kun, he knew in his heart that he was not an opponent, that is, onTouchEvent returned false, so he went to find Zhang Zhenren, Zhang Zhenren easily picked Cheng Kun, and onTouchEvent returned true. Therefore, he concluded that if onTouchEvent returned true, If it is processed, it will not be passed upwards. If the onTouchEvent method returns false, it will not be processed and will continue to pass upwards.

Guess you like

Origin blog.csdn.net/howlaa/article/details/128540365