There have been many articles on the view event distribution mechanism, recommending two articles from Guo Lin and Yang,
http://blog.csdn.net/guolin_blog/article/details/9097463
http://blog.csdn.net/lmj623565791/article/details/38960443
In combination with what they write, they simply summarize themselves and may only apply to individuals.
Process
As long as you touch any of the controls, you will definitely call the control's Dispatchtouchevent method, the source code is as follows (the latest API source code is not the case, but the analysis is still feasible)
publicdispatchTouchEventevent) { ``` ifnull && (mViewFlags & ENABLED_MASK) == ENABLED && mOnTouchListener.onTouch(thisevent)) { returntrue; } return onTouchEvent(event); }
As you can see from this approach, the setOnTouchListener
process of distributing the entire event after the control is as follows:
View.dispatchEvent -> OnTouchListener.onTouch -> View.onTouchEvent
dispatchTouchEvent
This is what the system calls when we perform actions on the view, such as pressing, moving, lifting, and so on, which onTouch
is the event listener written by the programmer.setOnTouchListener(new OnTouchListener(){...})
When we return in a method, the onTouch
true
dispatchTouchEvent
method returns true
without distributing the event onTouchEvent
, and the method and method are called within the method onClick
onLongClick
onTouchEvent
, so only the onTouch
method is returned false
, it is possible to trigger onClick
, onLongClick
method.
ObservationonTouchEvent
The source of the method (in order not to occupy space, directly from the above article) we can know:
① If a view is enable and clickable, thisonTouchEvent
Will definitely returntrue
, what is the use of this conclusion whendispatchTouchEvent
When the event is distributed, only this method returnstrue
To trigger an event for the next action. So whenonTouch
method returnsfalse
, events are distributed toonTouchEvent
, if view is enable and clickable, it will inevitably returntrue
, so that events of other actions can continue to triggerdispatchTouchEvent
。 If we write ourselves a custom control to inherit the button, rewritedispatchTouchEvent
method to return the method to thefalse
When We click on this button and the event finishes executing按下
After this operation is consumed, when you抬起
Fingers, the system does not call againdispatchTouchEvent
The
② whenMotionEvent.ACTION_UP
If the system decides beforeMotionEvent.ACTION_DOWN
Have performed长按
Operation, which calls the followingperformLongClick
Method (in fact, internal is the callbackOnLongClickListener
OfonLongClick
method), and returns thefalse
, you can triggeronClick
。 OtherwiseonLongClick
method returnstrue
,mHasPerformedLongPress
will becometrue
, you cannot perform
performClick
Method.
if (performLongClick()) { true; }
if(!mhasperformedlongpress) {//This was a tap, so remove the Longpress checkRemovelongpresscallback ();//Only perform take click on actions if we were in the pressed state if(!focustaken) {//Use a Runnable and post this rather than calling //PerformClick directly. This lets other visual state //Of the view update before click Actions Start. if(Mperformclick = =NULL) {Mperformclick =NewPerformClick (); }if(!Post(Mperformclick)) {PerformClick (); } } }
Summary
When you manipulate a control, whether it is pressed, moved, lifted, or not consumed in the middle of the event, the system will follow View.dispatchEvent -> OnTouchListener.onTouch -> View.onTouchEvent
the distribution.
For example, 按下
when a control produces an MotionEvent.ACTION_DOWN
event, the system first distributes the event to dispatchTouchEvent
, and then distributes the event to the programmer's written onTouch
method,
- If this
onTouch
method returns, true
the dispatchTouchEvent
event is true
not passed to onTouchEvent
;
- If the
onTouch
method returns false
, the dispatchTouchEvent
method is called and the onTouchEvent
various logic is processed within the method.
- When processing Action_down, the system determines this is the
长按
operation, the callback programmer writes the long press method, if the programmer writes the onLongClick
method returns true
, will make the long press the flag bit true
.
- The callback method is only used when the action_up is processed,
false
onClick
and the flag bit is long by default if the flag bit is long pressed false
.
- If the
dispatchTouchEvent
method returns false
, it is not possible to trigger events for other actions, which distinguishes whether the event will fire and whether the event will be distributed.
After executing the action 按下
抬起
, the system generates the MotionEvent.ACTION_UP
event, and then follow the above event distribution process to go through.
Android:view Event Distribution mechanism