This article is from: 103Style blog

“Android Development art Exploration” learning record

base on Android-29

Validate and analyze Android’s event distribution mechanism — validate the record section


directory

  • Some conclusions
  • To verify
  • Summary of verification results

Some conclusions

Here’s a list of some of the results from The Art of Android Development Quest, and then the next step to verify them:

1. The same event sequence refers to that from the moment the finger touches the screen to the end of leaving the screen, starting with down event, followed by an indefinite number of move events, and finally ending with up event.

2. Normally, a sequence of events can only be intercepted and consumed by one View. For this reason, please refer to number three.

3. Once a View decides to intercept, it can only handle the event sequence (provided the event can be passed to it), and its onInterceptTouchEvent will not be called again. When a View decides to intercept an event, the system assigns all other events in the event sequence to it, so no onInterceptTouchEvent is called to ask if it has been intercepted.

4. Once a View has started processing events, if it does not consume ACTION_DOWN events, all other events in the same sequence will be passed on to it and the event will be redirected to its parent, whose onTouchEvent will be called. Just like an important thing you do not handle well, in the short term the superior will not dare to give you important things to do.

5. If the View consumes no events other than ACTION_DOWN, the click event will disappear, the parent element’s onTouchEvent will not be called, and the current View will receive subsequent events, which will eventually be handed over to the Activity.

6. ViewGroup does not intercept any events by default. OnInterceptTouchEvent returns false by default in ViewGroup source code.

7, The View does not have an onInterceptTouchEvent method. Once an event is passed to it, its onTouchEvent will be called.

A View’s onTouchEvent will consume the event by default (returning true), unless it is unclickable (both Clickable and longClickable are false). The longClickable property of the View defaults to false; Clickable cases, such as Button defaults to true and TextView defaults to false.

9. The Enable property of the View does not affect the default return value of onTouchEvent, even if the View is disabled. OnTouchEvent returns true as long as either Clickable or longClickable is true.

OnClick fires only if the View is clickable and receives down and Up events.

11, transfer process of the event was outside-in, namely event always passed to the parent element, and then distributed to the View, by the parent element through requestDisallowInterceptTouchEvent (Boolean) method can intervene in the parent element distribution process, Except ACTION_DOWN.


Sample validation

Here, you mainly focus on the printed log information. You can take a look at the event distribution process.

Demo source github address

Create a ViewGroup and View that listen to the above three methods, and then rewrite the above methods in the test Activity. Java, TestView. Java, eventhandler. Java, mainActivity_main.xml

Reset Specifies the processing logic after a click.

TestLinearLayout.setIsIntercept(false);
TestLinearLayout.setInterceptEvent(-1024);
testLinearLayout.setOnClickListener(null);
testView.setOnClickListener(null);
testLinearLayout.setClickable(false);
testView.setClickable(false);
Copy the code

We verify this with the following test: setClickable(Fasle) is set to no click when no click event is set except by default

  • Do nothing by default
  • View sets the click event
  • ViewGroup sets click events
  • View and ViewGroup both set click events
  • ViewGroup intercepts only all events, i.eonInterceptTouchEventreturntrue
  • The ViewGroup intercepts all events and sets the ViewGroup’s click events
  • The ViewGroup intercepts all events and sets the View’s click events
  • ViewGroup intercepts all events and sets click events for View and ViewGroup
  • ViewGroup intercepts only DOWN events
  • The ViewGroup intercepts the DOWN event and sets the ViewGroup’s click event
  • The ViewGroup intercepts the DOWN event and sets the View’s click event
  • ViewGroup intercepts DOWN events and sets click events for View and ViewGroup
  • ViewGroup intercepts only MOVE events
  • The ViewGroup intercepts the MOVE event and sets the ViewGroup’s click event
  • The ViewGroup intercepts the MOVE event and sets the View’s click event
  • ViewGroup intercepts MOVE events and sets click events for View and ViewGroup
  • ViewGroup intercepts only UP events
  • The ViewGroup intercepts the UP event and sets the ViewGroup’s click event
  • The ViewGroup intercepts the UP event and sets the View’s click event
  • ViewGroup intercepts UP events and sets click events for View and ViewGroup
  • Make the View clickable only
  • Only set the View enable to false
  • Set View enable to false and clickable

The default state

To run the program, click on the area where TestView is located and you can see the printed log as follows:

MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestLinearLayout: onInterceptTouchEvent: ev.getAction() = ACTION_DOWN, isIntercept = falseTestView: dispatchTouchEvent: ev.getAction() = ACTION_DOWN TestView: onTouchEvent: ev.getAction() = ACTION_DOWN TestLinearLayout: onTouchEvent: ev.getAction() = ACTION_DOWN MainActivity: onTouchEvent: Ev.getaction () = ACTION_DOWN multiple MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_MOVE MainActivity: onTouchEvent: ev.getAction() = ACTION_MOVE MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_UP MainActivity: onTouchEvent: ev.getAction() = ACTION_UPCopy the code

Tips: Multiple means that the following events are repeated multiple times.

Log from the above we can see the DOWN event transmission path from MainActivity. DispatchTouchEvent – TestLinearLayout. DispatchTouchEvent – > TestLinearLayout. OnInterceptTouchEvent – TestView. DispatchTouchEvent – TestView. OnTouchEvent “TAB TestLinearLayout. OnTouchEvent – MainActivity. OnTouchEvent and MOVE and UP event occurs only in the MainActivity. Because the View defaults to both Clickable and longClickable, it does not consume DOWN events.

1, 2, 4, 6, 7, 8, 11 above.


Only the View sets the click event

Click on the View setOnClick button in the example, and then click on the area where TestView is located. You can see the printed log as follows:

MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestLinearLayout: onInterceptTouchEvent: ev.getAction() = ACTION_DOWN, isIntercept = falseTestView: dispatchTouchEvent: ev.getAction() = ACTION_DOWN TestView: onTouchEvent: Ev.getaction () = ACTION_DOWN multiple MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_MOVE TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_MOVE TestLinearLayout: onInterceptTouchEvent: ev.getAction() = ACTION_MOVE, isIntercept =false
TestView: dispatchTouchEvent: ev.getAction() = ACTION_MOVE
TestView: onTouchEvent: ev.getAction() = ACTION_MOVE

MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_UP
TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_UP
TestLinearLayout: onInterceptTouchEvent: ev.getAction() = ACTION_UP, isIntercept = false
TestView: dispatchTouchEvent: ev.getAction() = ACTION_UP
TestView: onTouchEvent: ev.getAction() = ACTION_UP
Copy the code

Since TestView consumes the DOWN event, all subsequent events are passed to TestView. We can see that each event is from MainActivity → TestLinearLayout → TestView. TestView doesn’t handle moves so it disappears.

1, 2, 4, 5, 6, 7, 11 above.


Only ViewGroup sets click events

If you click on the Reset and ViewGroup setOnClick buttons, then click on the TestView area, you can see the printed log as follows:

MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestLinearLayout: onInterceptTouchEvent: ev.getAction() = ACTION_DOWN, isIntercept = falseTestView: dispatchTouchEvent: ev.getAction() = ACTION_DOWN TestView: onTouchEvent: Ev.getaction () = ACTION_DOWN TestLinearLayout: onTouchEvent: ev.getAction() = ACTION_DOWN multiple MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_MOVE TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_MOVE TestLinearLayout: onTouchEvent: ev.getAction() = ACTION_MOVE MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_UP TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_UP TestLinearLayout: onTouchEvent: ev.getAction() = ACTION_UPCopy the code

The DOWN event is from MainActivity → TestLinearLayout → TestView → TestLinearLayout, The MOVE and UP events will start from MainActivity → TestLinearLayout. Because TestView does not consume DOWN events and then pass them to TestLinearLayout, which consumes DOWN events, each subsequent event is passed only to TestLinearLayout.

1, 2, 4, 5, 6, 7, 8, 11 above.


View and ViewGroup both set click events

Click on the Reset, View setOnClick, and ViewGroup setOnClick buttons, and click on the area where TestView is located.

MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestLinearLayout: onInterceptTouchEvent: ev.getAction() = ACTION_DOWN, isIntercept = falseTestView: dispatchTouchEvent: ev.getAction() = ACTION_DOWN TestView: onTouchEvent: Ev.getaction () = ACTION_DOWN MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_MOVE TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_MOVE TestLinearLayout: onInterceptTouchEvent: ev.getAction() = ACTION_MOVE, isIntercept =false
TestView: dispatchTouchEvent: ev.getAction() = ACTION_MOVE
TestView: onTouchEvent: ev.getAction() = ACTION_MOVE

MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_UP
TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_UP
TestLinearLayout: onInterceptTouchEvent: ev.getAction() = ACTION_UP, isIntercept = false
TestView: dispatchTouchEvent: ev.getAction() = ACTION_UP
TestView: onTouchEvent: ev.getAction() = ACTION_UP
Copy the code

This is the same as “View setting click events only”, because TestView consumes DOWN events, TestLinearLayout can no longer consume DOWN events.


ViewGroup is set to intercept all events(Intercepted, not consumed)

Click the Reset and ViewGroup Intercept All buttons in the example successively, and then click the area where TestView is located. You can see the printed log as follows:

MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestLinearLayout: onInterceptTouchEvent: ev.getAction() = ACTION_DOWN, isIntercept = trueTestLinearLayout: onTouchEvent: ev.getAction() = ACTION_DOWN MainActivity: onTouchEvent: Ev.getaction () = ACTION_DOWN multiple MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_MOVE MainActivity: onTouchEvent: ev.getAction() = ACTION_MOVE MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_UP MainActivity: onTouchEvent: ev.getAction() = ACTION_UPCopy the code

We can see that the DOWN event path is MainActivity → TestLinearLayout. Because the DOWN event is intercepted, TestView does not receive the event, while the MOVE and UP events are only in the MainActivity.

1, 3, 4, 5, 6, 7, 11 above.


The ViewGroup setting intercepts all events and sets the ViewGroup’s click events.

Click the Reset, ViewGroup Intercept All, and ViewGroup setOnClick buttons in order, and then click the area where TestView is located. You can see the printed log as follows:

MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_DOWN TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_DOWN TestLinearLayout: onInterceptTouchEvent: Ev.getaction () = ACTION_DOWN, isIntercept TestLinearLayout: onTouchEvent: ev.getAction() = ACTION_DOWN multiple MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_MOVE TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_MOVE TestLinearLayout: onTouchEvent: ev.getAction() = ACTION_MOVE MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_UP TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_UP TestLinearLayout: onTouchEvent: ev.getAction() = ACTION_UPCopy the code

Here we can see that the DOWN event path is MainActivity → TestLinearLayout. Because TestLinearLayout consumes DOWN events, MOVE and UP events are also passed to TestLinearLayout.

1, 2, 3, 4, 5, 6, 7, 8, 11 above.


The ViewGroup setting intercepts all events and sets the View’s click events.

Click the Reset, ViewGroup Intercept All and View setOnClick buttons in the example successively, and then click the area where TestView is located, you can see the printed log as follows:

MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestLinearLayout: onInterceptTouchEvent: ev.getAction() = ACTION_DOWN, isIntercept = trueTestLinearLayout: onTouchEvent: ev.getAction() = ACTION_DOWN MainActivity: onTouchEvent: Ev.getaction () = ACTION_DOWN multiple MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_MOVE MainActivity: onTouchEvent: ev.getAction() = ACTION_MOVE MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_UP MainActivity: onTouchEvent: ev.getAction() = ACTION_UPCopy the code

As with ViewGroup, TestLinearLayout intercepts events, so TestView does not receive them. TestLinearLayout does not consume DOWN events and subsequent events are not transmitted to it.


The ViewGroup setting intercepts all events and sets click events for the View and ViewGroup.

Click the Reset, ViewGroup Intercept All, View setOnClick, and ViewGroup setOnClick buttons in sequence, and then click the area where TestView is located. You can see the printed log as follows:

MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestLinearLayout: onInterceptTouchEvent: ev.getAction() = ACTION_DOWN, isIntercept = trueTestLinearLayout: onTouchEvent: ev.getAction() = ACTION_DOWN multiple MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_MOVE TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_MOVE TestLinearLayout: onTouchEvent: ev.getAction() = ACTION_MOVE MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_UP TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_UP TestLinearLayout: onTouchEvent: ev.getAction() = ACTION_UPCopy the code

TestLinearLayout intercepts events, so TestView does not receive events. TestLinearLayout consumes DOWN events. Subsequent events were also relayed to him.


Only ViewGroup intercepts DOWN events

Click the Reset and ViewGroup Intercept Down buttons in the example, and then click the area where TestView is located. You can see the printed log as follows:

MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestLinearLayout: onInterceptTouchEvent: ev.getAction() = ACTION_DOWN, isIntercept = falseTestLinearLayout: intercept event = ACTION_DOWN TestLinearLayout: onTouchEvent: Ev.getaction () = ACTION_DOWN MainActivity: onTouchEvent: ev.getAction() = ACTION_DOWN multiple MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_MOVE MainActivity: onTouchEvent: ev.getAction() = ACTION_MOVE MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_UP MainActivity: onTouchEvent: ev.getAction() = ACTION_UPCopy the code

As with ViewGroup, TestLinearLayout intercepts events, so TestView does not receive them. TestLinearLayout does not consume DOWN events and subsequent events are not transmitted to it.


The ViewGroup intercepts the DOWN event and sets the ViewGroup’s click event

If you click the Reset, ViewGroup Intercept Down, and ViewGroup setOnClick buttons, and then click the area where TestView is located, you can see the printed log as follows:

MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestLinearLayout: onInterceptTouchEvent: ev.getAction() = ACTION_DOWN, isIntercept = falseTestLinearLayout: intercept event = ACTION_DOWN TestLinearLayout: onTouchEvent: Ev.getaction () = ACTION_DOWN multiple MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_MOVE TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_MOVE TestLinearLayout: onTouchEvent: ev.getAction() = ACTION_MOVE MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_UP TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_UP TestLinearLayout: onTouchEvent: ev.getAction() = ACTION_UPCopy the code

TestLinearLayout intercepts events, so TestView does not receive events. TestLinearLayout consumes DOWN events. Subsequent events were also relayed to him.


The ViewGroup intercepts the DOWN event and sets the View’s click event

Click the Reset, ViewGroup Intercept Down, and View setOnClick buttons in order, and then click the area where TestView is located. You can see the printed log as follows:

MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestLinearLayout: onInterceptTouchEvent: ev.getAction() = ACTION_DOWN, isIntercept = falseTestLinearLayout: intercept event = ACTION_DOWN TestLinearLayout: onTouchEvent: Ev.getaction () = ACTION_DOWN MainActivity: onTouchEvent: ev.getAction() = ACTION_DOWN multiple MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_MOVE MainActivity: onTouchEvent: ev.getAction() = ACTION_MOVE MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_UP MainActivity: onTouchEvent: ev.getAction() = ACTION_UPCopy the code

Same as “ViewGroup is set to intercept all events”, TestLinearLayout intercepts events, so TestView does not receive them. TestLinearLayout does not consume DOWN events, and subsequent events are not transmitted to it.


ViewGroup intercepts DOWN events and sets click events for View and ViewGroup

Click the Reset, ViewGroup Intercept Down, View setOnClick, and ViewGroup setOnClick buttons in sequence, and then click the area where TestView is located. You can see the printed log as follows:

MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestLinearLayout: onInterceptTouchEvent: ev.getAction() = ACTION_DOWN, isIntercept = falseTestLinearLayout: intercept event = ACTION_DOWN TestLinearLayout: onTouchEvent: Ev.getaction () = ACTION_DOWN multiple MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_MOVE TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_MOVE TestLinearLayout: onTouchEvent: ev.getAction() = ACTION_MOVE MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_UP TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_UP TestLinearLayout: onTouchEvent: ev.getAction() = ACTION_UPCopy the code

TestLinearLayout intercepts events, so TestView does not receive events. TestLinearLayout consumes DOWN events. Subsequent events were also relayed to him.


Only ViewGroup intercepts MOVE events

We click the Reset and ViewGroup Intercept Move buttons in the example successively, and then click the area where TestView is located. We can see the printed log as follows:

MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestLinearLayout: onInterceptTouchEvent: ev.getAction() = ACTION_DOWN, isIntercept = falseTestView: dispatchTouchEvent: ev.getAction() = ACTION_DOWN TestView: onTouchEvent: ev.getAction() = ACTION_DOWN TestLinearLayout: onTouchEvent: ev.getAction() = ACTION_DOWN MainActivity: onTouchEvent: Ev.getaction () = ACTION_DOWN multiple MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_MOVE MainActivity: onTouchEvent: ev.getAction() = ACTION_MOVE MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_UP MainActivity: onTouchEvent: ev.getAction() = ACTION_UPCopy the code

Consistent with the “default”, since neither TestLinearLayout nor TestView consume DOWN events, subsequent events cannot be passed to them.


The ViewGroup intercepts the MOVE event and sets the ViewGroup’s click event

Click the Reset, ViewGroup Intercept Move, and ViewGroup setOnClick buttons in order, and then click the area where TestView is located. You can see the printed log as follows:

MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestLinearLayout: onInterceptTouchEvent: ev.getAction() = ACTION_DOWN, isIntercept = falseTestView: dispatchTouchEvent: ev.getAction() = ACTION_DOWN TestView: onTouchEvent: Ev.getaction () = ACTION_DOWN TestLinearLayout: onTouchEvent: ev.getAction() = ACTION_DOWN multiple MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_MOVE TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_MOVE TestLinearLayout: onTouchEvent: ev.getAction() = ACTION_MOVE MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_UP TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_UP TestLinearLayout: onTouchEvent: ev.getAction() = ACTION_UPCopy the code

This is the same as the “Set ViewGroup click event” click event, which is already handled by TestLinearLayout, so there is no onInterceptTouchEvent callback.


The ViewGroup intercepts the MOVE event and sets the View’s click event

Click the Reset, ViewGroup Intercept Move and View setOnClick buttons in the example successively, and then click the area where TestView is located, you can see the printed log as follows:

MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestLinearLayout: onInterceptTouchEvent: ev.getAction() = ACTION_DOWN, isIntercept = false
TestView: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestView: onTouchEvent: ev.getAction() = ACTION_DOWN

MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_MOVE
TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_MOVE
TestLinearLayout: onInterceptTouchEvent: ev.getAction() = ACTION_MOVE, isIntercept = falseTestLinearLayout: intercept event = ACTION_MOVE TestView: dispatchTouchEvent: ev.getAction() = ACTION_CANCEL TestView: OnTouchEvent: ev.getAction() = ACTION_CANCEL multiple MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_MOVE TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_MOVE TestLinearLayout: onTouchEvent: ev.getAction() = ACTION_MOVE MainActivity: onTouchEvent: ev.getAction() = ACTION_MOVE MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_UP TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_UP TestLinearLayout: onTouchEvent: ev.getAction() = ACTION_UP MainActivity: onTouchEvent: ev.getAction() = ACTION_UPCopy the code

As you can see, TestView consumes the DOWN event, but the MOVE event is intercepted by TestLinearLayout, and subsequent moves and UP events are passed to TestLinearLayout. The TestView ACTION_CANCEL event is triggered when a MOVE event is intercepted

1, 2, 3, 4, 5, 6, 7, 8, 11 above.


ViewGroup intercepts Move events and sets click events for View and ViewGroup

Click the Reset, ViewGroup Intercept Move, ViewGroup setOnClick and View setOnClick buttons in order, and then click the area where TestView is located, you can see the printed log as follows:

MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestLinearLayout: onInterceptTouchEvent: ev.getAction() = ACTION_DOWN, isIntercept = false
TestView: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestView: onTouchEvent: ev.getAction() = ACTION_DOWN

MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_MOVE
TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_MOVE
TestLinearLayout: onInterceptTouchEvent: ev.getAction() = ACTION_MOVE, isIntercept = falseTestLinearLayout: intercept event = ACTION_MOVE TestView: dispatchTouchEvent: ev.getAction() = ACTION_CANCEL TestView: OnTouchEvent: ev.getAction() = ACTION_CANCEL multiple MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_MOVE TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_MOVE TestLinearLayout: onTouchEvent: ev.getAction() = ACTION_MOVE MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_UP TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_UP TestLinearLayout: onTouchEvent: ev.getAction() = ACTION_UPCopy the code

Same as “ViewGroup intercepts MOVE event and sets View click event”. Because the DOWN event is passed to TestView first.


Only ViewGroup intercepts UP events

Click the Reset and ViewGroup Intercept Up buttons in the example, and then click the area where TestView is located. You can see the printed log as follows:

MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestLinearLayout: onInterceptTouchEvent: ev.getAction() = ACTION_DOWN, isIntercept = falseTestView: dispatchTouchEvent: ev.getAction() = ACTION_DOWN TestView: onTouchEvent: ev.getAction() = ACTION_DOWN TestLinearLayout: onTouchEvent: ev.getAction() = ACTION_DOWN MainActivity: onTouchEvent: Ev.getaction () = ACTION_DOWN multiple MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_MOVE MainActivity: onTouchEvent: ev.getAction() = ACTION_MOVE MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_UP MainActivity: onTouchEvent: ev.getAction() = ACTION_UPCopy the code

Same as “default”, because TestLinearLayout also does not consume DOWN events, it does not receive MOVE and UP events.


The ViewGroup intercepts the UP event and sets the ViewGroup’s click event

Click the Reset, ViewGroup Intercept Up, and ViewGroup setOnClick buttons in order, and then click the area where TestView is located to see the printed log as follows:

MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestLinearLayout: onInterceptTouchEvent: ev.getAction() = ACTION_DOWN, isIntercept = falseTestView: dispatchTouchEvent: ev.getAction() = ACTION_DOWN TestView: onTouchEvent: Ev.getaction () = ACTION_DOWN TestLinearLayout: onTouchEvent: ev.getAction() = ACTION_DOWN multiple MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_MOVE TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_MOVE TestLinearLayout: onTouchEvent: ev.getAction() = ACTION_MOVE MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_UP TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_UP TestLinearLayout: onTouchEvent: ev.getAction() = ACTION_UPCopy the code

TestLinearLayout consumes the DOWN event because it is already handling the event, and does not call onInterceptTouchEvent to intercept the UP event.

1, 3, 4, 5, 6, 7, 11 above.


The ViewGroup intercepts the UP event and sets the View’s click event

Click the Reset, ViewGroup Intercept Up, and View setOnClick buttons in order, and then click the area where TestView is located. You can see the printed log as follows:

MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestLinearLayout: onInterceptTouchEvent: ev.getAction() = ACTION_DOWN, isIntercept = falseTestView: dispatchTouchEvent: ev.getAction() = ACTION_DOWN TestView: onTouchEvent: Ev.getaction () = ACTION_DOWN multiple MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_MOVE TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_MOVE TestLinearLayout: onInterceptTouchEvent: ev.getAction() = ACTION_MOVE, isIntercept =false
TestView: dispatchTouchEvent: ev.getAction() = ACTION_MOVE
TestView: onTouchEvent: ev.getAction() = ACTION_MOVE

MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_UP
TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_UP
TestLinearLayout: onInterceptTouchEvent: ev.getAction() = ACTION_UP, isIntercept = false
TestLinearLayout: intercept event = ACTION_UP
TestView: dispatchTouchEvent: ev.getAction() = ACTION_CANCEL
TestView: onTouchEvent: ev.getAction() = ACTION_CANCEL
Copy the code

Because TestView consumes DOWN events and TestLinearLayout only intercepts UP events, the previous DOWN and MOVE events are passed to TestView once. Intercepted by TestLinearLayout, which then triggers the ACTION_CANCEL event of TestView.

1, 2, 3, 4, 5, 6, 7, 8, 11 above.


ViewGroup intercepts UP events and sets click events for View and ViewGroup

Click the Reset, ViewGroup Intercept Up, View setOnClick, and ViewGroup setOnClick buttons in sequence, and then click the area where TestView is located. You can see the printed log as follows:

MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestLinearLayout: onInterceptTouchEvent: ev.getAction() = ACTION_DOWN, isIntercept = falseTestView: dispatchTouchEvent: ev.getAction() = ACTION_DOWN TestView: onTouchEvent: Ev.getaction () = ACTION_DOWN multiple MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_MOVE TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_MOVE TestLinearLayout: onInterceptTouchEvent: ev.getAction() = ACTION_MOVE, isIntercept =false
TestView: dispatchTouchEvent: ev.getAction() = ACTION_MOVE
TestView: onTouchEvent: ev.getAction() = ACTION_MOVE

MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_UP
TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_UP
TestLinearLayout: onInterceptTouchEvent: ev.getAction() = ACTION_UP, isIntercept = false
TestLinearLayout: intercept event = ACTION_UP
TestView: dispatchTouchEvent: ev.getAction() = ACTION_CANCEL
TestView: onTouchEvent: ev.getAction() = ACTION_CANCEL
Copy the code

Same as “ViewGroup intercepts UP events and sets View click events”


Make the View clickable only

Click the Reset and Set View Clickable True buttons in the example, and then click the TestView area. You can see the printed log as follows:

MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestLinearLayout: onInterceptTouchEvent: ev.getAction() = ACTION_DOWN, isIntercept = false
TestView: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestView: onTouchEvent: ev.getAction() = ACTION_DOWN

MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_UP
TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_UP
TestLinearLayout: onInterceptTouchEvent: ev.getAction() = ACTION_UP, isIntercept = false
TestView: dispatchTouchEvent: ev.getAction() = ACTION_UP
TestView: onTouchEvent: ev.getAction() = ACTION_UP
Copy the code

Consistent with “setting only View click events”, the main validation point is point 8.


Only set the View enable to false

Click the Reset and Set View Enable false buttons in order, and then click the TestView area. You can see the printed log as follows:

MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestLinearLayout: onInterceptTouchEvent: ev.getAction() = ACTION_DOWN, isIntercept = false
TestView: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestView: onTouchEvent: ev.getAction() = ACTION_DOWN
TestLinearLayout: onTouchEvent: ev.getAction() = ACTION_DOWN
MainActivity: onTouchEvent: ev.getAction() = ACTION_DOWN

MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_UP
MainActivity: onTouchEvent: ev.getAction() = ACTION_UP
Copy the code

Consistent with “Default”, point 9 is the main validation.


Set View enable to false and clickable

Click the Reset, Set View Clickable True, and Set View Enable False buttons, and then click the TestView area to see the printed log as follows:

MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestLinearLayout: onInterceptTouchEvent: ev.getAction() = ACTION_DOWN, isIntercept = false
TestView: dispatchTouchEvent: ev.getAction() = ACTION_DOWN
TestView: onTouchEvent: ev.getAction() = ACTION_DOWN

MainActivity: dispatchTouchEvent: ev.getAction() = ACTION_UP
TestLinearLayout: dispatchTouchEvent: ev.getAction() = ACTION_UP
TestLinearLayout: onInterceptTouchEvent: ev.getAction() = ACTION_UP, isIntercept = false
TestView: dispatchTouchEvent: ev.getAction() = ACTION_UP
TestView: onTouchEvent: ev.getAction() = ACTION_UP
Copy the code

Same as “only set View click events”, mainly verify points 8 and 9.


Summary of log information

Here is a brief summary. From the above log, we can see:

  • By default event order is Activity. The dispatchTouchEvent – ViewGroup. DispatchTouchEvent – ViewGroup. OnInterceptTouchEvent – > View.dispatchtouchevent → view.onTouchEvent → ViewGroup.onTouchEvent → Activity.onTouchEvent, Then subsequent events are only the Activity. The dispatchTouchEvent – > Activity. The onTouchEvent, it won’t continue to put down the again. Like the boss got a task, and then to your supervisor, your supervisor and then handed over to you, you want to for a long time found that won’t solve, will tell the superior won’t solve, then your boss thought for a long time, found that can solve, and then feedback to the boss, and then the boss can only deal with the task, the subsequent similar tasks also won’t give your superior, So you won’t have a similar task.

  • As long as the intermediate ViewGroup or View handles the Down event, subsequent events will be passed to the location where the Down event was handled except for the upper interception. Or the above example, if to complete the task, and subsequent similar task the boss will next to your supervisor and your supervisor also can handle it, he would have to you first, if you can handle, let you deal with, if you can’t handle, subsequent similar task is not assigned to you, he will handle it himself.

  • If a View handles a Down event, but subsequent events are intercepted by the ViewGroup, subsequent events are handed over to the ViewGroup for processing. Again, the boss gets an assignment, gives it to your supervisor, and your supervisor gives it to you, and then you finish the first part, and then you go on sabbatical, so your supervisor takes care of the rest and doesn’t bother you while you’re on vacation.

  • However, the Enable state of the View does not affect its receipt of events. It’s like your supervisor calls you in for a meeting, but you’re in another meeting, and doesn’t let you know just because you’re in another meeting.


If there is a description wrong, please remind me, thank you!

The above

If you like it, please give it a thumbs up.


Scan the qr code below, follow my public account Android1024, click attention, don’t get lost.

`