1. DOWN Event flow direction
【 Key points 】
dispatchTouchEvent
,onTouchEvent
returntrue
: The event is stopped (no one can receive the event again).dispatchTouchEvent
returnfalse
: Events stop passing and distributing to the child View and start backtracking to the parent control, just like recursive stops and backtracking begins.dispatchTouchEvent
,onTouchEvent
,onInterceptTouchEvent
returnsuper.xxxxxx()
: Events follow a U-shaped path through the entire event.
2. Flow direction of MOVE and UP events
2.1. DispatchTouchEvent Returns true
ACTION_MOVE and ACTION_UP events are not passed down the View and are consumed immediately.
2.2. OnTouchEvent Returns true
ACTION_MOVE and ACTION_UP events are not passed down the View and will be consumed by onTouchEvent.
2.3. Summarize
ACTION_DOWN
In which control is the event consumed (return true
), thenACTION_MOVE
和ACTION_UP
It goes from the top downdispatchTouchEvent
Event distribution is passed down to the control, not down.- if
ACTION_DOWN
Event is indispatchTouchEvent
Consume, then the event stops passing. - if
ACTION_DOWN
Event is inonTouchEvent
Consumer, then will putACTION_MOVE
或ACTION_UP
Event to the controlonTouchEvent
Process and end the pass.
3. setOnTouchListener
public boolean dispatchTouchEvent(MotionEvent event) {
// ...
boolean result = false;
// ...
if (onFilterTouchEventForSecurity(event)) {
// ...
if(li ! =null&& li.mOnTouchListener ! =null
&& (mViewFlags & ENABLED_MASK) == ENABLED
&& li.mOnTouchListener.onTouch(this, event)) {
// Handle OnTouchListener first
// If the return value is true, its own onTouchEvent will not be executed and consumed
result = true;
}
// Pass onTouchEvent to yourself
if(! result && onTouchEvent(event)) { result =true; }}// ...
return result;
}
Copy the code
【 conclusion 】
- Is not set
OnTouchListener
Is executed by defaultonTouchEvent(event)
Methods. - Set up
OnTouchListener
, the system will execute it first and decide whether to execute it based on its return valueonTouchEvent(event)
. - see
OnTouchListener
Priority ratio ofonTouchEvent
High.