Android:view Event Distribution mechanism

Source: Internet
Author: User

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

dispatchTouchEventThis 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.

ObservationonTouchEventThe 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, thisonTouchEventWill definitely returntrue, what is the use of this conclusion whendispatchTouchEventWhen the event is distributed, only this method returnstrueTo trigger an event for the next action. So whenonTouchmethod 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, rewritedispatchTouchEventmethod to return the method to thefalseWhen We click on this button and the event finishes executing按下After this operation is consumed, when you抬起Fingers, the system does not call againdispatchTouchEventThe
② whenMotionEvent.ACTION_UPIf the system decides beforeMotionEvent.ACTION_DOWNHave performed长按Operation, which calls the followingperformLongClickMethod (in fact, internal is the callbackOnLongClickListenerOfonLongClickmethod), and returns thefalse, you can triggeronClick。 OtherwiseonLongClickmethod returnstruemHasPerformedLongPresswill becometrue, you cannot perform
performClickMethod.

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,

    1. If this onTouch method returns, true the dispatchTouchEvent event is true not passed to onTouchEvent ;
    2. If the onTouch method returns false , the dispatchTouchEvent method is called and the onTouchEvent various logic is processed within the method.
      1. 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 .
      2. 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 .
    3. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.