The Android Touch event mechanism
Android touch event mechanism is a commonplace in the development, but it is a shame that for so long development, still ignorant of the specific process of Android touch event, taking advantage of the project online gap, to re-study.
To prepare
In order to show the distribution-interception-consumption process of Android touch events as simply and clearly as possible, a bit of preparation has been done based on the characteristics of activities, Viewgroups, and Views.
Touch events:
- actionDown
- actionMove
- actionUp
There are two types of Event handling for Android:
For activities and Views: there are only two types of events:
- Dispatch: dispatchTouchEvent function
- Consumption: onTouchEvent function and OnTouchListener function
For ViewGroup: all three events:
- Dispatch: dispatchTouchEvent function
- Interception: onInterceptTouchEvent function
- Consumption: onTouchEvent function and OnTouchListener function
So, rewrite a typical ViewGroup: RelativeLayout and a typical View: TextView for their touch events to figure out the flow of the touch events.
TouchRelativeLayout
public class TouchRelativeLayout extends RelativeLayout { public TouchRelativeLayout(Context context) { super(context); } public TouchRelativeLayout(Context context, AttributeSet attrs) { super(context, attrs); } public TouchRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override public boolean dispatchTouchEvent(MotionEvent ev) { LogUtils.e("ViewGroup-------dispatchTouchEvent-------", ev.getAction() + ""); return super.dispatchTouchEvent(ev); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { LogUtils.e("ViewGroup-------onInterceptTouchEvent-------", ev.getAction() + ""); return super.onInterceptTouchEvent(ev); } @Override public boolean onTouchEvent(MotionEvent event) { LogUtils.e("ViewGroup-------onTouchEvent-------", event.getAction() + ""); return super.onTouchEvent(event); }}Copy the code
TouchTextView
public class TouchTextView extends TextView { public TouchTextView(Context context) { super(context); } public TouchTextView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); } public TouchTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override public boolean dispatchTouchEvent(MotionEvent ev) { LogUtils.e("View---------dispatchTouchEvent-------", ev.getAction() + ""); return super.dispatchTouchEvent(ev); } @Override public boolean onTouchEvent(MotionEvent event) { LogUtils.e("View---------onTouchEvent-------", event.getAction() + ""); return super.dispatchTouchEvent(event); }}Copy the code
The code does not do anything, just print out the current control touch events. Go ahead and create a new Activity that provides an entry for touch events:
The layout of the Activity
<? The XML version = "1.0" encoding = "utf-8"? > <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" android:orientation="vertical"> <com.ddz.lifestyle.baseview.customview.TouchRelativeLayout android:id="@+id/rl_touch" android:layout_width="200dp" android:layout_height="200dp" android:layout_centerInParent="true" android:background="@android:color/holo_blue_light"> <com.ddz.lifestyle.baseview.customview.TouchTextView android:id="@+id/tv_touch" android:layout_width="100dp" android:layout_height="100dp" android:layout_centerInParent="true" android:background="@color/recy_bg" /> </com.ddz.lifestyle.baseview.customview.TouchRelativeLayout> </RelativeLayout>Copy the code
Ignore the surrounding RelativeLayout, which is essentially an Activity with a ViewGroup wrapped around a View. The Activity does nothing but print touch events:
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
LogUtils.e("activity-------dispatchTouchEvent-------", ev.getAction() + "");
return super.dispatchTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
LogUtils.e("activity-------onTouchEvent-------", event.getAction() + "");
return super.onTouchEvent(event);
}
Copy the code
Run the program, open the Activity you want to touch, and display the effect. The blue area is the rewritten TouchRelativeLayout, and the gray area is the rewritten TouchTextView
image
Start analyzing:
1. Do not overwrite any events by default
- 1. Touch area A (press and lift)
08-10 14:58:08. 131, 11780-11780 / com. The DDZ) lifestyle E/activity -- -- -- -- -- -- -- dispatchTouchEvent -- -- -- -- -- -- - : 008-10 14:58:08.132 11780-11780/com.ddz.lifestyle E/activity-------onTouchEvent-------: 008-10 14:58:08.697 11780-11780/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 1 08-10 14:58:08.697 11780-11780/com.ddz.lifestyle E/activity-------onTouchEvent-------: 1Copy the code
According to log analysis: dispatchTouchEvent and onTouchEvent events of the Activity are recorded. 0 indicates the pressed action and 1 indicates the lifted action.
Conclusion: By default, the Activity distributes touch events and consumes itself without child View consumption.
- 2. Touch area B (press, move, lift)
08-10 15:11:43. 128, 11780-11780 / com. The DDZ) lifestyle E/activity -- -- -- -- -- -- -- dispatchTouchEvent -- -- -- -- -- -- - : 0 08-10 15:11:43.129 11780-11780/com.ddz.lifestyle E/ViewGroup-------dispatchTouchEvent-------: 008-10 15:11:43.129 11780-11780/com.ddz.lifestyle E/ViewGroup-------onInterceptTouchEvent-------: 0 08-10 15:11:43.129 11780-11780/com.ddz.lifestyle E/ViewGroup-------onTouchEvent-------: 008-10 15:11:43.129 11780-11780/com.ddz.lifestyle E/activity-------onTouchEvent-------: 008-10 15:11:43.519 11780-11780/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 15:11:43.519 11780-11780/com.ddz.lifestyle E/activity-------onTouchEvent-------: 2 08-10 15:11:43.536 11780-11780/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 15:11:43.536 11780-11780/com.ddz.lifestyle E/activity-------onTouchEvent-------: 2 08-10 15:11:43.553 11780-11780/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 15:11:43.553 11780-11780/com.ddz.lifestyle E/activity-------onTouchEvent-------: 2 08-10 15:11:43.569 11780-11780/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 15:11:43.570 11780-11780/com.ddz.lifestyle E/activity-------onTouchEvent-------: 2 08-10 15:11:43.875 11780-11780/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 1 08-10 15:11:43.875 11780-11780/com.ddz.lifestyle E/activity-------onTouchEvent-------: 1Copy the code
Analysis:
1. When pressed, the Activity dispatches events to the ViewGroup. The ViewGroup returns the default super.dispatchTouchEvent(EV) that dispatches events and passes them to the onInterceptTouchEvent function of the ViewGroup.
2, onInterceptTouchEvent. Return to the default super onInterceptTouchEvent (ev), events to the ViewGroup onTouchEvent method
3. OnTouchEvent still returns the default super.onTouchEvent(event), which does not consume, and returns the event to the Activity at the next level.
4. With finger movements, the Activity’s dispatchTouchEvent method dispatches events. Because you already know that the ViewGroup in the Activity does nothing with the touch event and returns the event to the Activity, the Activity is smart to consume the event itself
5. The Activity’s onTouchEvent handles the movement event itself
6. Finger lift: Again, the Activity’s dispatchTouchEvent method dispatches events and its own onTouchEvent is consumed
- 3. Touch area C (press, move, lift)
08-10 15:50:20. 933, 20620-20620 / com. The DDZ) lifestyle E/activity -- -- -- -- -- -- -- dispatchTouchEvent -- -- -- -- -- -- - : 008-10 15:50:20.933 20620-20620/com.ddz.lifestyle E/ViewGroup-------dispatchTouchEvent-------: 008-10 15:50:20.934 20620-20620/com.ddz.lifestyle E/ViewGroup-------onInterceptTouchEvent-------: 0 08-10 15:50:20.934 20620-20620/com.ddz.lifestyle E/View---------dispatchTouchEvent-------: 008-10 15:50:20.934 20620-20620/com.ddz.lifestyle E/View---------onTouchEvent-------: 008-10 15:50:20.943 20620-20620/com.ddz.lifestyle E/ViewGroup-------onTouchEvent-------: 008-10 15:50:20.944 20620-20620/com.ddz.lifestyle E/activity-------onTouchEvent-------: 0 07-10 15:50:21.441 20620-20620/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 15:50:21.441 20620-20620/com.ddz.lifestyle E/activity-------onTouchEvent-------: 2 08-10 15:50:21.458 20620-20620/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 15:50:21.458 20620-20620/com.ddz.lifestyle E/activity-------onTouchEvent-------: 2 08-10 15:50:21.475 20620-20620/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 15:50:21.475 20620-20620/com.ddz.lifestyle E/activity-------onTouchEvent-------: 2 08-10 15:50:21.492 20620-20620/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 15:50:21.492 20620-20620/com.ddz.lifestyle E/activity-------onTouchEvent-------: 2 08-10 15:50:21.786 20620-20620/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 15:50:21.786 20620-20620/com.ddz.lifestyle E/activity-------onTouchEvent-------: 2 08-10 15:50:21.787 20620-20620/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 1 08-10 15:50:21.787 20620-20620/com.ddz.lifestyle E/activity-------onTouchEvent-------: 1Copy the code
Analysis: The result is similar to that of touch in region B
1. When pressed, the Activity dispatchTouchEvent method dispatches events to the ViewGroup
2. The ViewGroup dispatchTouchEvent method sends events to its onInterceptTouchEvent method by default. The onInterceptTouchEvent method does not intercept events by default and passes events to its View
The View dispatchTouchEvent method sends events to its own onTouchEvent method by default. OnTouchEvent returns the default super.onTouchEvent(event). Pass the event back to the ViewGroup’s onTouchEvent method
4. The ViewGroup onTouchEvent also passes events to the Activity’s onTouchEvent method by default
5. The pressed event must be handled by the Activity’s onTouchEvent method itself
The Activity already knows that neither the ViewGroup nor the View handles touch events, so it uses its dispatchTouchEvent method to pass movement events to its onTouchEvent method for consumption
7, fingers lifted, the same as the sixth step, their own distribution of their own processing.
2. Overwrite the ViewGroup dispatchTouchEvent method to return false
- 1. Touch area B (press, move, lift)
08-10 16:22:05. 686, 3933-3933 / com. The DDZ) lifestyle E/activity -- -- -- -- -- -- -- dispatchTouchEvent -- -- -- -- -- -- - : 008-10 16:22:05.686 3933-3933/com.ddz.lifestyle E/ViewGroup-------dispatchTouchEvent-------: 008-10 16:22:05.686 3933-3933/com.ddz.lifestyle E/activity-------onTouchEvent-------: 008-10 16:22:07.268 3933-3933/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 16:22:07.268 3933-3933/com.ddz.lifestyle E/activity-------onTouchEvent-------: 2 08-10 16:22:07.352 3933-3933/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 16:22:07.352 3933-3933/com.ddz.lifestyle E/activity-------onTouchEvent-------: 2 08-10 16:22:07.436 3933-3933/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 16:22:07.436 3933-3933/com.ddz.lifestyle E/activity-------onTouchEvent-------: 2 08-10 16:22:07.486 3933-3933/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 16:22:07.486 3933-3933/com.ddz.lifestyle E/activity-------onTouchEvent-------: 2 08-10 16:22:07.873 3933-3933/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 16:22:07.873 3933-3933/com.ddz.lifestyle E/activity-------onTouchEvent-------: 2 08-10 16:22:07.890 3933-3933/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 16:22:07.890 3933-3933/com.ddz.lifestyle E/activity-------onTouchEvent-------: 2 08-10 16:22:07.892 3933-3933/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 1 08-10 16:22:07.892 3933-3933/com.ddz.lifestyle E/activity-------onTouchEvent-------: 1Copy the code
Analysis:
1. When pressed, the Activity dispatchTouchEvent dispatches events to the ViewGroup
2. Since ViewGroup dispatchTouchEvent returns false, that is, the event is not sent further down and is returned to the Activity’s onTouchEvent for consumption
3. Finger movement: The Activity uses dispatchTouchEvent to distribute events, but it already knows from the feedback of the pressed event that the ViewGroup no longer distributes events and will return them, so it consumes the event itself with onTouchEvent
4, finger lift: the same as finger movement, their own distribution of their own consumption
- 2. Touch area C (press, move, lift)
08-10 16:31:17. 409, 3933-3933 / com. The DDZ) lifestyle E/activity -- -- -- -- -- -- -- dispatchTouchEvent -- -- -- -- -- -- - : 008-10 16:31:17.409 3933-3933/com.ddz.lifestyle E/ViewGroup-------dispatchTouchEvent-------: 008-10 16:31:17.409 3933-3933/com.ddz.lifestyle E/activity-------onTouchEvent-------: 0 08-10 16:31:17.912 3933-3933/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 16:31:17.912 3933-3933/com.ddz.lifestyle E/activity-------onTouchEvent-------: 2 08-10 16:31:17.929 3933-3933/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 16:31:17.929 3933-3933/com.ddz.lifestyle E/activity-------onTouchEvent-------: 2 08-10 16:31:17.963 3933-3933/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 16:31:17.963 3933-3933/com.ddz.lifestyle E/activity-------onTouchEvent-------: 2 08-10 16:31:18.194 3933-3933/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 1 08-10 16:31:18.194 3933-3933/com.ddz.lifestyle E/activity-------onTouchEvent-------: 1Copy the code
Analysis:
1. IspatchTouchEvent of ViewGroupd returns false, that is, no more events are distributed and no View is touched
3. Overwrite the ViewGroup dispatchTouchEvent method and return true
- 1. Touch area B (press, move, lift)
08-10 16:43:37. 080, 3206-3206 / com. The DDZ) lifestyle E/activity -- -- -- -- -- -- -- dispatchTouchEvent -- -- -- -- -- -- - : 008-10 16:43:37.080 3206-3206/com.ddz.lifestyle E/ViewGroup-------dispatchTouchEvent-------: 0 08-10 16:43:37.226 3206-3206/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 16:43:37.226 3206-3206/com.ddz.lifestyle E/ViewGroup-------dispatchTouchEvent-------: 2 08-10 16:43:37.243 3206-3206/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 16:43:37.243 3206-3206/com.ddz.lifestyle E/ViewGroup-------dispatchTouchEvent-------: 2 08-10 16:43:37.260 3206-3206/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 16:43:37.411 3206-3206/com.ddz.lifestyle E/ViewGroup-------dispatchTouchEvent-------: 2 08-10 16:43:37.444 3206-3206/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 16:43:37.445 3206-3206/com.ddz.lifestyle E/ViewGroup-------dispatchTouchEvent-------: 2 08-10 16:43:37.464 3206-3206/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 1 08-10 16:43:37.464 3206-3206/com.ddz.lifestyle E/ViewGroup-------dispatchTouchEvent-------: 1Copy the code
Analysis: 1. When pressed, the Activity uses the dispatchTouchEvent method to dispatch events; 2. The ViewGroup dispatchTouchEvent receives the event, but the ViewGroup dispatchTouchEvent returns true. 3. Finger movement, finger lift: The Activity dispatchTouchEvent event is consumed by the ViewGroup dispatchTouchEvent method.
- 2. Touch area C (press, move, lift)
08-10 16:43:37. 080, 3206-3206 / com. The DDZ) lifestyle E/activity -- -- -- -- -- -- -- dispatchTouchEvent -- -- -- -- -- -- - : 008-10 16:43:37.080 3206-3206/com.ddz.lifestyle E/ViewGroup-------dispatchTouchEvent-------: 0 08-10 16:43:37.226 3206-3206/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 16:43:37.226 3206-3206/com.ddz.lifestyle E/ViewGroup-------dispatchTouchEvent-------: 2 08-10 16:43:37.243 3206-3206/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 16:43:37.243 3206-3206/com.ddz.lifestyle E/ViewGroup-------dispatchTouchEvent-------: 2 08-10 16:43:37.260 3206-3206/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 16:43:37.411 3206-3206/com.ddz.lifestyle E/ViewGroup-------dispatchTouchEvent-------: 2 08-10 16:43:37.444 3206-3206/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 16:43:37.445 3206-3206/com.ddz.lifestyle E/ViewGroup-------dispatchTouchEvent-------: 2 08-10 16:43:37.464 3206-3206/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 1 08-10 16:43:37.464 3206-3206/com.ddz.lifestyle E/ViewGroup-------dispatchTouchEvent-------: 1Copy the code
The ViewGroup dispatchTouchEvent method returns true. The event is consumed by the ViewGroup dispatchTouchEvent method and is no longer passed down
4, rewrite the onInterceptTouchEvent method of ViewGroup to return true
- 1. Touch area B (press, move, lift)
08-10 17:01:03. 576, 16009-16009 / com. The DDZ) lifestyle E/activity -- -- -- -- -- -- -- dispatchTouchEvent -- -- -- -- -- -- - : 0 08-10 17:01:03.576 16009-16009/com.ddz.lifestyle E/ViewGroup-------dispatchTouchEvent-------: 008-10 17:01:03.576 16009-16009/com.ddz.lifestyle E/ViewGroup-------onInterceptTouchEvent-------: 008-10 17:01:03.576 16009-16009/com.ddz.lifestyle E/ViewGroup-------onTouchEvent-------: 008-10 17:01:03.576 16009-16009/com.ddz.lifestyle E/activity-------onTouchEvent-------: 0 08-10 17:01:04.148 16009-16009/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 17:01:04.148 16009-16009/com.ddz.lifestyle E/activity-------onTouchEvent-------: 2 08-10 17:01:04.165 16009-16009/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 17:01:04.165 16009-16009/com.ddz.lifestyle E/activity-------onTouchEvent-------: 2 08-10 17:01:04.448 16009-16009/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 17:01:04.448 16009-16009/com.ddz.lifestyle E/activity-------onTouchEvent-------: 2 08-10 17:01:04.448 16009-16009/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 1 08-10 17:01:04.448 16009-16009/com.ddz.lifestyle E/activity-------onTouchEvent-------: 1Copy the code
Analysis: 1, finger press: The Activity dispatchTouchEvent dispatches events to the ViewGroup. The ViewGroup default dispatchTouchEvent dispatches events to the onInterceptTouchEvent method. The onInterceptTouchEvent method returns true to intercept the event and feed it to the onTouchEvent method. Return the event to the Activity’s onTouchEvent method, and the event will be consumed by the Activity’s onTouchEvent. The Activity uses dispatchTouchEvent to send the event to its onTouchEvent and consumes it. Like finger movements, the Activity distributes its own consumption
- 2. Touch area C (press, move, lift)
08-10 17:06:27. 835, 16009-16009 / com. The DDZ) lifestyle E/activity -- -- -- -- -- -- -- dispatchTouchEvent -- -- -- -- -- -- - : 008-10 17:06:27.835 16009-16009/com.ddz.lifestyle E/ViewGroup-------dispatchTouchEvent-------: 008-10 17:06:27.835 16009-16009/com.ddz.lifestyle E/ViewGroup-------onInterceptTouchEvent-------: 008-10 17:06:27.835 16009-16009/com.ddz.lifestyle E/ViewGroup-------onTouchEvent-------: 008-10 17:06:27.835 16009-16009/com.ddz.lifestyle E/activity-------onTouchEvent-------: 008-10 17:06:27.998 16009-16009/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 17:06:27.999 16009-16009/com.ddz.lifestyle E/activity-------onTouchEvent-------: 2 08-10 17:06:28.015 16009-16009/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 17:06:28.015 16009-16009/com.ddz.lifestyle E/activity-------onTouchEvent-------: 2 08-10 17:06:28.300 16009-16009/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 17:06:28.300 16009-16009/com.ddz.lifestyle E/activity-------onTouchEvent-------: 2 08-10 17:06:28.357 16009-16009/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 1 08-10 17:06:28.357 16009-16009/com.ddz.lifestyle E/activity-------onTouchEvent-------: 1Copy the code
The onInterceptTouchEvent in the ViewGroup does not consume the event and is returned to the Activity. The View in the ViewGroup does not receive the event because the ViewGroup has already intercepted the event.
Update the ViewGroup onInterceptTouchEvent method to return false
- 1. Touch area B (press, move, lift)
08-10 17:13:47. 725, 19111-19111 / com. The DDZ) lifestyle E/activity -- -- -- -- -- -- -- dispatchTouchEvent -- -- -- -- -- -- - : 008-10 17:13:47.725 19111-19111/com.ddz.lifestyle E/ViewGroup-------dispatchTouchEvent-------: 008-10 17:13:47.725 19111-19111/com.ddz.lifestyle E/ViewGroup-------onInterceptTouchEvent-------: 008-10 17:13:47.725 19111-19111/com.ddz.lifestyle E/ViewGroup-------onTouchEvent-------: 008-10 17:13:47.725 19111-19111/com.ddz.lifestyle E/activity-------onTouchEvent-------: 007-10 17:13:47.786 19111-19111/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 786 19111-19111/com.ddz.lifestyle E/activity-------onTouchEvent-------: 2 08-10 17:13:47.803 19111-19111/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 17:13:47.803 19111-19111/com.ddz.lifestyle E/activity-------onTouchEvent-------: 2 08-10 17:13:48.005 19111-19111/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 17:13:48.005 19111-19111/com.ddz.lifestyle E/activity-------onTouchEvent-------: 2 08-10 17:13:48.009 19111-19111/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 1 08-10 17:13:48.009 19111-19111/com.ddz.lifestyle E/activity-------onTouchEvent-------: 1Copy the code
Analysis: 1, finger press: The Activity dispatchTouchEvent dispatches events to the ViewGroup. The ViewGroup default dispatchTouchEvent dispatches events to the onInterceptTouchEvent method. The onInterceptTouchEvent method returns false, but the ViewGroup touch area does not have a View, so the event is passed to the onTouchEvent method. Return the event to the Activity’s onTouchEvent method, and the event will be consumed by the Activity’s onTouchEvent. The Activity uses dispatchTouchEvent to send the event to its onTouchEvent and consumes it. Like finger movements, the Activity distributes its own consumption
- 2. Touch area C (press, move, lift)
08-10 17:20:04. 767, 19111-19111 / com. The DDZ) lifestyle E/activity -- -- -- -- -- -- -- dispatchTouchEvent -- -- -- -- -- -- - : 007-10 17:20:04.767 19111-19111/com.ddz.lifestyle E/ViewGroup-------dispatchTouchEvent-------: 008-10 17:20:04.767 19111-19111/com.ddz.lifestyle E/ViewGroup-------onInterceptTouchEvent-------: 007-10 17:20:04.767 19111-19111/com.ddz.lifestyle E/View---------dispatchTouchEvent-------: 008-10 17:20:04.767 19111-19111/com.ddz.lifestyle E/View---------onTouchEvent-------: 008-10 17:20:04.768 19111-19111/com.ddz.lifestyle E/ViewGroup-------onTouchEvent-------: 008-10 17:20:04.768 19111-19111/com.ddz.lifestyle E/activity-------onTouchEvent-------: 008-10 17:20:04.896 19111-19111/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 17:20:04.896 19111-19111/com.ddz.lifestyle E/activity-------onTouchEvent-------: 2 08-10 17:20:05.047 19111-19111/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 17:20:05.047 19111-19111/com.ddz.lifestyle E/activity-------onTouchEvent-------: 2 08-10 17:20:05.064 19111-19111/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 17:20:05.064 19111-19111/com.ddz.lifestyle E/activity-------onTouchEvent-------: 2 08-10 17:20:05.081 19111-19111/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 17:20:05.081 19111-19111/com.ddz.lifestyle E/activity-------onTouchEvent-------: 2 08-10 17:20:05.095 19111-19111/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 17:20:05.095 19111-19111/com.ddz.lifestyle E/activity-------onTouchEvent-------: 2 08-10 17:20:05.095 19111-19111/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 1 08-10 17:20:05.095 19111-19111/com.ddz.lifestyle E/activity-------onTouchEvent-------: 1Copy the code
Analysis: 1, finger press: The Activity dispatchTouchEvent sends the event to the ViewGroup. The ViewGroup dispatchTouchEvent method returns the default super.dispatchTouchEvent(EV). So the event continues to be distributed, passing onInterceptTouchEvent; OnInterceptTouchEvent returns false. The event is passed to the View in the ViewGroup. 5. The View’s onTouchEvent method does not consume the event. The View’s onTouchEvent method does not consume the event. The event is returned to the ViewGroup’s onTouchEvent method; 7. The Activity’s onTouchEvent method consumes the pressed event itself (Activity: it doesn’t consume either, so it won’t be passed to you next time). 8, The Activity does what it says: finger movement and finger lift events The Activity uses the dispatchTouchEvent method to send events to its own onTouchEvent method for consumption, and to send events to its own consumption.
Overwrite the ViewGroup onTouchEvent method and return true
- 1. Touch area B (press, move, lift)
08-10 17:32:42. 158, 12826-12826 / com. The DDZ) lifestyle E/activity -- -- -- -- -- -- -- dispatchTouchEvent -- -- -- -- -- -- - : 0 08-10 17:32:42.158 12826-12826/com.ddz.lifestyle E/ViewGroup-------dispatchTouchEvent-------: 0 08-10 17:32:42.158 12826-12826/com.ddz.lifestyle E/ViewGroup-------onInterceptTouchEvent-------: 0 08-10 17:32:42.158 12826-12826/com.ddz.lifestyle E/ViewGroup-------onTouchEvent-------: 0 08-10 17:32:42.194 12826-12826/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 17:32:42.194 12826-12826/com.ddz.lifestyle E/ViewGroup-------dispatchTouchEvent-------: 2 08-10 17:32:42.194 12826-12826/com.ddz.lifestyle E/ViewGroup-------onTouchEvent-------: 2 08-10 17:32:42.227 12826-12826/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 17:32:42.227 12826-12826/com.ddz.lifestyle E/ViewGroup-------dispatchTouchEvent-------: 2 08-10 17:32:42.227 12826-12826/com.ddz.lifestyle E/ViewGroup-------onTouchEvent-------: 2 08-10 17:32:42.428 12826-12826/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 17:32:42.428 12826-12826/com.ddz.lifestyle E/ViewGroup-------dispatchTouchEvent-------: 2 08-10 17:32:42.428 12826-12826/com.ddz.lifestyle E/ViewGroup-------onTouchEvent-------: 2 08-10 17:32:42.442 12826-12826/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 1 08-10 17:32:42.442 12826-12826/com.ddz.lifestyle E/ViewGroup-------dispatchTouchEvent-------: 1 08-10 17:32:42.442 12826-12826/com.ddz.lifestyle E/ViewGroup-------onTouchEvent-------: 1Copy the code
Analysis: 1, finger press: The Activity dispatchTouchEvent dispatches events to the ViewGroup. 2. The ViewGroup’s default dispatchTouchEvent method dispatches events to the ViewGroup. Passed to onInterceptTouchEvent method 3, ViewGroup onInterceptTouchEvent method returned by default super. OnInterceptTouchEvent (ev), not intercept, But the touch area of the ViewGroup does not have a View, so the event is passed to the onTouchEvent method of the ViewGroup. The Activity’s dispatchTouchEvent method still dispatches events to the ViewGroup; The onInterceptTouchEvent method is not intercepted by default, so the onInterceptTouchEvent method is not asked by the ViewGroup. 7. Finger lift: The same process as finger movement, and the final event is consumed by the onTouchEvent method of the ViewGroup
- 2. Touch area C (press, move, lift)
08-10 17:39:21. 134, 12826-12826 / com. The DDZ) lifestyle E/activity -- -- -- -- -- -- -- dispatchTouchEvent -- -- -- -- -- -- - : 0 08-10 17:39:21.135 12826-12826/com.ddz.lifestyle E/ViewGroup-------dispatchTouchEvent-------: 0 08-10 17:39:21.135 12826-12826/com.ddz.lifestyle E/ViewGroup-------onInterceptTouchEvent-------: 0 08-10 17:39:21.135 12826-12826/com.ddz.lifestyle E/View---------dispatchTouchEvent-------: 0 08-10 17:39:21.135 12826-12826/com.ddz.lifestyle E/View---------onTouchEvent-------: 0 08-10 17:39:21.140 12826-12826/com.ddz.lifestyle E/ViewGroup-------onTouchEvent-------: 008-10 17:39:21.261 12826-12826/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 17:39:21.261 12826-12826/com.ddz.lifestyle E/ViewGroup-------dispatchTouchEvent-------: 2 08-10 17:39:21.261 12826-12826/com.ddz.lifestyle E/ViewGroup-------onTouchEvent-------: 2 08-10 17:39:21.277 12826-12826/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 17:39:21.278 12826-12826/com.ddz.lifestyle E/ViewGroup-------dispatchTouchEvent-------: 2 08-10 17:39:21.278 12826-12826/com.ddz.lifestyle E/ViewGroup-------onTouchEvent-------: 2 08-10 17:39:21.428 12826-12826/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 17:39:21.428 12826-12826/com.ddz.lifestyle E/ViewGroup-------dispatchTouchEvent-------: 2 08-10 17:39:21.428 12826-12826/com.ddz.lifestyle E/ViewGroup-------onTouchEvent-------: 2 08-10 17:39:21.435 12826-12826/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 1 08-10 17:39:21.435 12826-12826/com.ddz.lifestyle E/ViewGroup-------dispatchTouchEvent-------: 1 08-10 17:39:21.435 12826-12826/com.ddz.lifestyle E/ViewGroup-------onTouchEvent-------: 1Copy the code
Analysis: 1, finger press: The Activity dispatchTouchEvent dispatches events to the ViewGroup. 2. The ViewGroup’s default dispatchTouchEvent method dispatches events to the ViewGroup. Passed to onInterceptTouchEvent method 3, ViewGroup onInterceptTouchEvent method returned by default super. OnInterceptTouchEvent (ev), not intercept, Now I have a View in my ViewGroup, The View dispatchTouchEvent sends the event to its own onTouchEvent method. The View’s onTouchEvent does not want to consume the event. The onTouchEvent method of the ViewGroup returns true and consumes the event itself. Press the event button to end the event. The Activity’s dispatchTouchEvent method still dispatches events to the ViewGroup; The ViewGroup already knows that its onInterceptTouchEvent method is not intercepted by default, so the onInterceptTouchEvent method is not asked when the onInterceptTouchEvent method is moved. The View in the ViewGroup does not consume events, so it will not be passed to the View this time. The onTouchEvent method passed directly to the ViewGroup returns true. 10. Finger lift is the same as finger movement
7, overwrite the ViewGroup onTouchEvent method to return false
- 1. Touch area B (press, move, lift)
08-10 17:49:17. 141, 29978-29978 / com. The DDZ) lifestyle E/activity -- -- -- -- -- -- -- dispatchTouchEvent -- -- -- -- -- -- - : 0 08-10 17:49:17.142 29978-29978/com.ddz.lifestyle E/ViewGroup-------dispatchTouchEvent-------: 0 08-10 17:49:17.142 29978-29978/com.ddz.lifestyle E/ViewGroup-------onInterceptTouchEvent-------: 0 08-10 17:49:17.142 29978-29978/com.ddz.lifestyle E/ViewGroup-------onTouchEvent-------: 008-10 17:49:17.142 29978-29978/com.ddz.lifestyle E/activity-------onTouchEvent-------: 008-10 17:49:17.245 29978-29978/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 17:49:17.245 29978-29978/com.ddz.lifestyle E/activity-------onTouchEvent-------: 2 08-10 17:49:17.295 29978-29978/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 17:49:17.295 29978-29978/com.ddz.lifestyle E/activity-------onTouchEvent-------: 2 08-10 17:49:17.409 29978-29978/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 17:49:17.409 29978-29978/com.ddz.lifestyle E/activity-------onTouchEvent-------: 2 08-10 17:49:17.409 29978-29978/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 1 08-10 17:49:17.409 29978-29978/com.ddz.lifestyle E/activity-------onTouchEvent-------: 1Copy the code
Analysis: 1, finger press: The Activity dispatchTouchEvent dispatches events to ViewGroup 2. The ViewGroup’s default dispatchTouchEvent method dispatches events to the onInterceptTouchEvent method. 3, ViewGroup onInterceptTouchEvent method returns the default super. OnInterceptTouchEvent (ev), i.e. not intercept, but not the View ViewGroup touch area, 3. The onTouchEvent method of ViewGroup returns false, does not consume the event, and returns the event to the Activity’s onTouchEvent method. The final pressed event is consumed by the Activity’s onTouchEvent itself. The Activity uses dispatchTouchEvent to send the event to its onTouchEvent and consumes it. Like finger movements, the Activity distributes its own consumption
- 2. Touch area C (press, move, lift)
08-10 17:52:32. 229, 29978-29978 / com. The DDZ) lifestyle E/activity -- -- -- -- -- -- -- dispatchTouchEvent -- -- -- -- -- -- - : 0 08-10 17:52:32.230 29978-29978/com.ddz.lifestyle E/ViewGroup-------dispatchTouchEvent-------: 0 08-10 17:52:32.230 29978-29978/com.ddz.lifestyle E/ViewGroup-------onInterceptTouchEvent-------: 0 08-10 17:52:32.230 29978-29978/com.ddz.lifestyle E/View---------dispatchTouchEvent-------: 0-08-10 17:52:32.230 29978-29978/com.ddz.lifestyle E/View---------onTouchEvent-------: 0 08-10 17:52:32.230 29978-29978/com.ddz.lifestyle E/ViewGroup-------onTouchEvent-------: 0 08-10 17:52:32.230 29978-29978/com.ddz.lifestyle E/activity-------onTouchEvent-------: 0 08-10 17:52:32.331 29978-29978/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 17:52:32.331 29978-29978/com.ddz.lifestyle E/activity-------onTouchEvent-------: 2 08-10 17:52:32.348 29978-29978/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 17:52:32.348 29978-29978/com.ddz.lifestyle E/activity-------onTouchEvent-------: 2 08-10 17:52:32.735 29978-29978/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 17:52:32.735 29978-29978/com.ddz.lifestyle E/activity-------onTouchEvent-------: 2 08-10 17:52:32.740 29978-29978/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 1 08-10 17:52:32.741 29978-29978/com.ddz.lifestyle E/activity-------onTouchEvent-------: 1Copy the code
Analysis: 1, finger press: The Activity dispatchTouchEvent sends the event to the ViewGroup. The ViewGroup dispatchTouchEvent method returns the default super.dispatchTouchEvent(EV). So the event continues to be distributed, passing onInterceptTouchEvent; The onInterceptTouchEvent is not intercepted by default. The event is passed to the View in the ViewGroup. 5. The View’s onTouchEvent method does not consume the event by default. The event is returned to the ViewGroup’s onTouchEvent method; But the onTouchEvent method of the ViewGroup returns false, and the event is returned to the Activity’s onTouchEvent method. Do not consume, next time will not give you); 8, The Activity does what it says: finger movement and finger lift events The Activity uses the dispatchTouchEvent method to send events to its own onTouchEvent method for consumption, and to send events to its own consumption.
8. Override the View dispatchTouchEvent method to return false
- 1. Touch area C (press, move, lift)
08-10 19:20:06. 394, 29197-29197 / com. The DDZ) lifestyle E/activity -- -- -- -- -- -- -- dispatchTouchEvent -- -- -- -- -- -- - : 008-10 19:20:06.394 29197-29197/com.ddz.lifestyle E/ViewGroup-------dispatchTouchEvent-------: 008-10 19:20:06.394 29197-29197/com.ddz.lifestyle E/ViewGroup-------onInterceptTouchEvent-------: 008-10 19:20:06.394 29197-29197/com.ddz.lifestyle E/View---------dispatchTouchEvent-------: 008-10 19:20:06.394 29197-29197/com.ddz.lifestyle E/ViewGroup-------onTouchEvent-------: 0 08-10 19:20:06.395 29197-29197/com.ddz.lifestyle E/activity-------onTouchEvent-------: 008-10 19:20:06.529 29197-29197/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 19:20:06.529 29197-29197/com.ddz.lifestyle E/activity-------onTouchEvent-------: 2 08-10 19:20:06.563 29197-29197/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 19:20:06.563 29197-29197/com.ddz.lifestyle E/activity-------onTouchEvent-------: 2 08-10 19:20:06.579 29197-29197/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 19:20:06.580 29197-29197/com.ddz.lifestyle E/activity-------onTouchEvent-------: 2 08-10 19:20:06.634 29197-29197/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 1 08-10 19:20:06.634 29197-29197/com.ddz.lifestyle E/activity-------onTouchEvent-------: 1Copy the code
1. Press the Activity dispatchTouchEvent button to send events to the ViewGroup. 2. The VIewGroup dispatchTouchEvent method returns the default super.dispatchTouchEvent(EV), so events continue to be distributed and passed to the onInterceptTouchEvent; The onInterceptTouchEvent of the ViewGroup is not intercepted by default. The event is passed to the View in the ViewGroup. When the View receives the event, the dispatchTouchEvent method returns false, the event is no longer distributed, and the onTouchEvent is returned to the ViewGroup. 6. Finally, the pressed event is consumed by the Activity’s onTouchEvent method. 7. Press the event to know: From View–>ViewGroup–>View, the event is finally returned to the Activity, so the finger moves, raises the event, no further distribution, directly has the Activity’s dispatchTouchEvent to its own onTouchEvent consumption
9. Overwrite the View dispatchTouchEvent method and return true
- 1. Touch area C (press, move, lift)
08-10 19:29:34. 275, 22834-22834 / com. The DDZ) lifestyle E/activity -- -- -- -- -- -- -- dispatchTouchEvent -- -- -- -- -- -- - : 0 08-10 19:29:34.275 22834-22834/com.ddz.lifestyle E/ViewGroup-------dispatchTouchEvent-------: 0 08-10 19:29:34.275 22834-22834/com.ddz.lifestyle E/ViewGroup-------onInterceptTouchEvent-------: 0 08-10 19:29:34.275 22834-22834/com.ddz.lifestyle E/View---------dispatchTouchEvent-------: 0 08-10 19:29:34.621 22834-22834/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 19:29:34.621 22834-22834/com.ddz.lifestyle E/ViewGroup-------dispatchTouchEvent-------: 2 08-10 19:29:34.621 22834-22834/com.ddz.lifestyle E/ViewGroup-------onInterceptTouchEvent-------: 2 08-10 19:29:34.621 22834-22834/com.ddz.lifestyle E/View---------dispatchTouchEvent-------: 2 08-10 19:29:34.637 22834-22834/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 19:29:34.637 22834-22834/com.ddz.lifestyle E/ViewGroup-------dispatchTouchEvent-------: 2 08-10 19:29:34.637 22834-22834/com.ddz.lifestyle E/ViewGroup-------onInterceptTouchEvent-------: 2 08-10 19:29:34.638 22834-22834/com.ddz.lifestyle E/View---------dispatchTouchEvent-------: 2 08-10 19:29:34.642 22834-22834/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 1 08-10 19:29:34.642 22834-22834/com.ddz.lifestyle E/ViewGroup-------dispatchTouchEvent-------: 1 08-10 19:29:34.642 22834-22834/com.ddz.lifestyle E/ViewGroup-------onInterceptTouchEvent-------: 1 08-10 19:29:34.642 22834-22834/com.ddz.lifestyle E/View---------dispatchTouchEvent-------: 1Copy the code
1. Press the Activity dispatchTouchEvent button to send events to the ViewGroup. 2. The VIewGroup dispatchTouchEvent method returns the default super.dispatchTouchEvent(EV), so events continue to be distributed and passed to the onInterceptTouchEvent; 3, The onInterceptTouchEvent of the ViewGroup is not intercepted by default. The event is passed to the View in the ViewGroup. When the View receives an event, the dispatchTouchEvent method returns true, the event is not sent and is consumed by the View’s dispatchTouchEvent. All are consumed by the View’s dispatchTouchEvent method
Overwrite the View onTouchEvent method to return false
- 1. Touch area C (press, move, lift)
08-10 21:18:33. 082, 8039-8039 / com. The DDZ) lifestyle E/activity -- -- -- -- -- -- -- dispatchTouchEvent -- -- -- -- -- -- - : 0 08-10 21:18:33.082 8039-8039/com.ddz.lifestyle E/ViewGroup-------dispatchTouchEvent-------: 008-10 21:18:33.082 8039-8039/com.ddz.lifestyle E/ViewGroup-------onInterceptTouchEvent-------: 0 08-10 21:18:33.082 8039-8039/com.ddz.lifestyle E/View---------dispatchTouchEvent-------: 0 8-10 21:18:33. 082, 8039-8039 / com. The DDZ) lifestyle E/View -- -- -- -- -- -- -- -- -- onTouchEvent -- -- -- -- -- - : 0 08-10 21:18:33.082 8039-8039/com.ddz.lifestyle E/ViewGroup-------onTouchEvent-------: 008-10 21:18:33.082 8039-8039/com.ddz.lifestyle E/activity-------onTouchEvent-------: 0 08-10 21:18:33.108 8039-8039/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 21:18:33.108 8039-8039/com.ddz.lifestyle E/activity-------onTouchEvent-------: 2 08-10 21:18:33.125 8039-8039/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 21:18:33.125 8039-8039/com.ddz.lifestyle E/activity-------onTouchEvent-------: 2 08-10 21:18:33.141 8039-8039/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-10 21:18:33.141 8039-8039/com.ddz.lifestyle E/activity-------onTouchEvent-------: 2 08-10 21:18:33.248 8039-8039/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 1 08-10 21:18:33.248 8039-8039/com.ddz.lifestyle E/activity-------onTouchEvent-------: 1Copy the code
Analysis: 1, finger press: The Activity dispatchTouchEvent sends the event to the ViewGroup. The ViewGroup dispatchTouchEvent method returns the default super.dispatchTouchEvent(EV). So the event continues to be distributed, passing onInterceptTouchEvent; The onInterceptTouchEvent is not intercepted by default. The event is passed to the View in the ViewGroup. 4. After the View receives the event, the dispatchTouchEvent method returns the default super.dispatchTouchEvent(EV), 5. The View onTouchEvent method returns false, does not consume the event, and the event is returned to the ViewGroup onTouchEvent method. 6. By default, the onTouchEvent method of the ViewGroup does not consume the event, which is returned to the Activity’s onTouchEvent method. Do not consume, next time will not give you); Finger movement: The Activity already knows that neither the ViewGroup nor the View consumes events, so the finger movement event consumes itself using the onTouchEvent method that dispatchTouchEvent passes to it
Rewrite the View onTouchEvent method and return true
- 1. Touch area C (press, move, lift)
08-11 13:33:27. 684, 11900-11900 / com. The DDZ) lifestyle E/activity -- -- -- -- -- -- -- dispatchTouchEvent -- -- -- -- -- -- - : 008-11 13:33:27.684 11900-11900/com.ddz.lifestyle E/ViewGroup-------dispatchTouchEvent-------: 008-11 13:33:27.684 11900-11900/com.ddz.lifestyle E/ViewGroup-------onInterceptTouchEvent-------: 008-11 13:33:27.684 11900-11900/com.ddz.lifestyle E/View---------dispatchTouchEvent-------: 008-11 13:33:27.685 11900-11900/com.ddz.lifestyle E/View---------onTouchEvent-------: 008-11 13:33:27.761 11900-11900/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-11 13:33:27.761 11900-11900/com.ddz.lifestyle E/ViewGroup-------dispatchTouchEvent-------: 2 08-11 13:33:27.761 11900-11900/com.ddz.lifestyle E/ViewGroup-------onInterceptTouchEvent-------: 2 08-11 13:33:27.761 11900-11900/com.ddz.lifestyle E/View---------dispatchTouchEvent-------: 2 08-11 13:33:27.761 11900-11900/com.ddz.lifestyle E/View---------onTouchEvent-------: 2 08-11 13:33:27.962 11900-11900/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 2 08-11 13:33:27.962 11900-11900/com.ddz.lifestyle E/ViewGroup-------dispatchTouchEvent-------: 2 08-11 13:33:27.962 11900-11900/com.ddz.lifestyle E/ViewGroup-------onInterceptTouchEvent-------: 2 08-11 13:33:27.962 11900-11900/com.ddz.lifestyle E/View---------dispatchTouchEvent-------: 2 08-11 13:33:27.962 11900-11900/com.ddz.lifestyle E/View---------onTouchEvent-------: 2 08-11 13:33:27.993 11900-11900/com.ddz.lifestyle E/activity-------dispatchTouchEvent-------: 1 08-11 13:33:27.994 11900-11900/com.ddz.lifestyle E/ViewGroup-------dispatchTouchEvent-------: 1 08-11 13:33:27.994 11900-11900/com.ddz.lifestyle E/ViewGroup-------onInterceptTouchEvent-------: 1 08-11 13:33:27.994 11900-11900/com.ddz.lifestyle E/View---------dispatchTouchEvent-------: 1 08-11 13:33:27.994 11900-11900/com.ddz.lifestyle E/View---------onTouchEvent-------: 1Copy the code
Analysis: 1, finger press: The Activity dispatchTouchEvent sends the event to the ViewGroup. The ViewGroup dispatchTouchEvent method returns the default super.dispatchTouchEvent(EV). So the event continues to be distributed, passing onInterceptTouchEvent; The onInterceptTouchEvent is not intercepted by default. The event is passed to the View in the ViewGroup. When the View receives an event, the dispatchTouchEvent method returns the default super.dispatchTouchEvent(EV), and the event is passed to its onTouchEvent method. 6. Finger movement: The Activity already knows that the onTouchEvent method of the View in the ViewGroup needs to consume the event. The finger movement event repeats the process of pressing the event until the event is consumed by the View onTouchEvent method. 7, finger lifting: the same as finger movement
conclusion
ViewGroup:
DispatchTouchEvent dispatchTouchEvent The ViewGroup receives the event and decides whether to dispatch it
- 1. The super-.dispatchTouchEvent (EV) method is returned by default, which is the default dispatch event
- 2, if return false; Events will not be distributed and will be returned to the onTouch method, and subsequent events will not be distributed to the current ViewGroup and will be distributed and consumed by the upper layer itself
- 3. If true is returned, the event is no longer dispatched and consumed by the ViewGroup dispatchTouchEvent. Subsequent touch events are also consumed by the ViewGroup dispatchTouchEvent
OnInterceptTouchEvent Intercepts ViewGroup’s dispatchTouchEvent. If the event is distributed by default, pass it to the onInterceptTouchEvent method, The onInterceptTouchEvent method determines whether to intercept
- 1, the default return super. OnInterceptTouchEvent (ev) is not intercept, events continue to pass
- 2, return true, the event is passed directly to the ViewGroup onTouchEvent method consumption, no longer passed to the View in the ViewGroup
- 3. If false is returned, the event is passed to the View in the ViewGroup for processing
OnTouchEvent consumptionIf the onInterceptTouchEvent in the ViewGroup does not intercept events by default, use the onTouchEvent return value of the ViewGroup to determine
- Super.ontouchevent (Event) is returned by default. The event will be passed to the View in the ViewGroup for processing. If the View in the ViewGroup does not consume the event, the event will be returned to the onTouchEvent method of the ViewGroup for processing. The ViewGroup onTouchEvent method does not process by default and returns to the upper-level onTouchEvent method
- If true is returned, the event will also be passed to the View in the ViewGroup for processing. If the View in the ViewGroup does not consume the event, the event will be returned to the onTouchEvent method in the ViewGroup. The onTouchEvent of the ViewGroup will consume the event without returning the onTouchEvent method
- 3. If false is returned, the event will be passed to the View by default from the ViewGroup. The View does not process the onTouchEvent returned to the ViewGroup. So the event is returned to the upper onTouchEvent method
The View:
DispatchTouchEvent After receiving the event, the View determines whether to continue sending the event according to the value of dispatchTouchEvent
- Super.dispatchtouchevent (EV) is returned by default. The event will be sent down to the View’s own onTouchEvent for processing.
- 2. If false is returned, the event is not distributed and is returned directly to the onTouchEvent method of the upper ViewGroup for processing
- 3. If true is returned, events are not distributed and are consumed directly by the View’s dispatchTouchEvent method, and subsequent events are consumed directly by the View’s dispatchTouchEvent method
OnTouchEvent consumptionIf the View’s dispatchTouchEvent passes the event to the onTouchEvent method, it will decide whether to consume the event based on the value returned by the onTouchEvent method
- Super.ontouchevent (event) is returned by default. Events are not consumed by default and will be returned to the onTouchEvent method of the upper ViewGroup for processing
- 2. If set to true, the event will be consumed and will not be returned. Other events will also be consumed by the onTouchEvent method
- 3. If the value is set to false, it will not be consumed and will be returned to higher processing as the default method
So, the whole flow of the touch event is clear
For event distribution: (dispatchTouchEvent) If you want the event not to be sent down, consume it yourself: return true for the current dispatchTouchEvent; If you want the event not to be passed down, return to the upper layer: return false for the current dispatchTouchEvent; For event intercepting: (onInterceptTouchEvent) If you want to intercept an event, consume it in your onTouchEvent method: Return true for onInterceptTouchEvent; if you don’t intercept an event, pass it down by default: For event consumption: (onTouchEvent) If you do not want to consume, return to the upper layer: Return onTouchEvent as default or return false; If you want to consume, do not return: Return onTouchEvent true;