Android event distribution mechanism instance verification
My program has a custom MyLayout layout in MainActivity. There is a custom MyButton under the MyLayout layout.
Case 1
PS: In the table, "super" indicates that the method with the same name in the parent class is returned. "-" indicates that this method does not exist. "No return value indicates that the return type of this method is void.
Click the non-MyButton area. The result is as follows:
Click "MyButton". The result is as follows:
Through these two results, we can find that the event is the Activity distributed from top to bottom-> ViewGroup-> View.
However, there is another difference between the two results, that is, the onInterceptTouchEvent method of MyLayout In the first result is not executed when it is lifted, and the second result is executed. This is because the dispatchTouchEvent method in ViewGroup calls the onInterceptTouchEvent method only when it is pressed or the event continues to be distributed.
Case 2
Click the non-MyButton area. The result is as follows:
Click "MyButton". The result is as follows:
The output result of Case 2 is obviously less than that of Case 1, because the onInterceptTouchEvent method and onTouchEvent method in MyLayout are called in the dispatchTouchEvent method in ViewGroup, in Case 2, the returned value of the dispatchTouchEvent method in MyLayout is false, not super. dispatchTouchEvent (ev) does not call the dispatchTouchEvent method in ViewGroup. Because the returned value is false, the event is returned to the onTouchEvent method of the upper level for consumption instead of further distribution. Therefore, the onTouch and onClick methods in MyLayout are not executed either. In this case, the result of clicking the non-MyButton area under scenario 2 is the same as that of clicking the MyButton area.
Case 3
Click the non-MyButton area. The result is as follows:
Click "MyButton". The result is as follows:
The output result of case 3 is obviously one less than Case 2, because in case 3, the return value of the dispatchTouchEvent method in MyLayout is true, and the event will be consumed by the dispatchTouchEvent method in MyLayout, the upper-level onTouchEvent method will not pass up for consumption, so the output result of case 3 is one fewer than that of Case 2.
Case 4
Click the non-MyButton area. The result is as follows:
Click "MyButton". The result is as follows:
The output result of case 4 is exactly the same as that of Case 1, because the return value of the onInterceptTouchEvent method in MyLayout is false, and the return value is super. dispatchTouchEvent (event), but the dispatchTouchEvent method of ViewGroup has nothing, that is, the return value is false.
Case 5
Click the non-MyButton area. The result is as follows:
Click "MyButton". The result is as follows:
Scenario 5: the result of clicking a non-MyButton area is the same as that of clicking the MyButton area. This is because the onInterceptTouchEvent method in MyLayout returns true, indicating that the event is intercepted, the event is handed over to the onTouchEvent of the current View for processing, and the event will not continue to be distributed.
Case 6
Click the non-MyButton area. The result is as follows:
Click "MyButton". The result is as follows:
In case 6, clicking a non-MyButton area is different from clicking a non-MyButton area in Case 1. This is because the onTouchEvent method in MyLayout in case 6 returns false, indicating that the current View cannot be solved, the event is passed up to the onTouchEvent method of the upper level for processing. We found that MainActivity did not distribute the event to MyLayout when we raised the event, because when we pressed the event, MainActivity distributed the event to MyLayout, but did not process the event successfully before MyLayout, therefore, when an event is raised, MainActivity does not distribute the event to MyLayout. However, when an event is raised next time, MainActivity will still distribute the event to MyLayout because the elements of successful event processing are not stored before MyLayout, it is cleared through the dispatchTouchEvent method of the parent level.
Case 7
Click the non-MyButton area. The result is as follows:
Click "MyButton". The result is as follows:
In case 7, clicking a non-MyButton area is one fewer result than clicking a non-MyButton area in Case 1, because the onTouchEvent method in MyLayout returns true, this indicates that the current View will accept and consume the event. However, the onTouchEvent method in the View is not called at this time. If you do not call this method, the Click event cannot be responded.
Case 8
Click the non-MyButton area. The result is as follows:
Click "MyButton". The result is as follows:
In case 8, clicking a non-MyButton area is less than clicking a non-MyButton area in Case 1, because the onTouch method in MyLayout returns true, if the onTouch method of a View returns true, it ends. If the return value is false, the onTouchEvent method of the View is called.
Case 9
Click the non-MyButton area. The result is as follows:
Click "MyButton". The result is as follows:
Case 10
Click the non-MyButton area. The result is as follows:
Click "MyButton". The result is as follows:
Summary
This example verifies the Touch event analysis in the distribution and consumption mechanism of Touch events in Android programming.
The Touch Event Analysis Section of this blog post is referenced below:
Event distribution: public boolean dispatchTouchEvent (MotionEvent ev)
DispatchTouchEvent (MotionEvent ev) of the Activity when the Touch event occurs) the method will be in the tunnel mode (pass down from the root element until the inmost layer sub-element or stop passing because of a certain condition in the middle of a mona1 element) the event is passed to the dispatchTouchEvent (MotionEvent ev) method of the outermost View, and the dispatchTouchEvent (MotionEvent ev) method of the View distributes the event. The event distribution logic of dispatchTouchEvent is as follows:
If return true, the event is distributed to the current View and consumed by the dispatchTouchEvent method. At the same time, the event stops being passed down. If return false is returned, event distribution is divided into two types:
* If the event retrieved by the current View is directly from the Activity, the event is returned to the onTouchEvent of the Activity for consumption;
* If the event retrieved by the current View comes from the outer parent control, the event is returned to the onTouchEvent of the parent View for consumption. If the system returns the default super. dispatchTouchEvent (ev), the event is automatically distributed to the onInterceptTouchEvent method of the current View.
Event interception: public boolean onInterceptTouchEvent (MotionEvent ev)
When the dispatchTouchEvent (MotionEvent ev) method of the outer View returns the system's default super. dispatchTouchEvent (ev), the event is automatically distributed to the onInterceptTouchEvent method of the current View. The event interception logic of onInterceptTouchEvent is as follows:
If onInterceptTouchEvent returns true, the event is intercepted and the intercepted event is handed over to the onTouchEvent of the current View for processing. If onInterceptTouchEvent returns false, the event is released, the event on the current View will be passed to the sub-View, and then the dispatchTouchEvent of the sub-View will start distributing the event; If onInterceptTouchEvent is returned, super. onInterceptTouchEvent (ev), the event is intercepted by default, and the intercepted event is handed over to the onTouchEvent of the current View for processing.
Event Response: public boolean onTouchEvent (MotionEvent ev)
When dispatchTouchEvent returns super. dispatchTouchEvent (ev) and onInterceptTouchEvent returns true or super. onInterceptTouchEvent (ev), onTouchEvent is called. The Event Response logic of onTouchEvent is as follows:
If the event is passed to the onTouchEvent method of the current View, and the method returns false, the event will be passed up from the current View and received by the onTouchEvent of the upper View, if the onTouchEvent passed to the above method also returns false, the event will "disappear" and the next event will not be received. If true is returned, the event is received and consumed. If super. onTouchEvent (ev) is returned, the default event processing logic is the same as when false is returned.
In situations 6, 7, 8, there are also some knowledge points beyond the summary, please note.