Android dispatchtouchevent, onintercepttouchevent, Ontouchevent detailed

Source: Internet
Author: User

Before encountering event distribution, go online to find related articles ... I don't feel like I've made these connections clear. Study a few good articles, today in this tidy up, I hope to be helpful to everyone.
The first thing you need to know is that when you touch a control, you call the Dispatchtouchevent method of the control (or its parent class). Then let's take a look at the source code for the Dispatchtouchevent method:

publicdispatchTouchEventevent) {      ifnull && (mViewFlags & ENABLED_MASK) == ENABLED &&              mOnTouchListener.onTouch(thisevent)) {          returntrue;      }      return onTouchEvent(event);  }  

The first condition is true after you setontouchlistener register the listener.
The second condition is that it is not enable (the XML layout file can be set, the button defaults to True,imageview to false). The key is the third condition:

button.setOnTouchListener(new OnTouchListener() {      @Override      publicbooleanonTouch(View v, MotionEvent event) {         returnfalse//       // return true; //     }  

That is, if we return true in the Ontouch method, the three conditions are all set, and the entire method returns true directly. If we return false in the Ontouch method, we will execute the Ontouchevent method again. (OnClick is a callback in ontouchevent)

So summarize:
The first thing to do in Dispatchtouchevent is the Ontouch method, and if Ontouch returns True and the control is enable and the listener has been registered for it, Dispatchtouchevent returns True. Then the ontouchevent will not execute, and the onclick will not execute. (Return to Ture, which can be understood as being consumed by the dispatchtouchevent itself)
If Ontouch returns false, or if the other two conditions are not true, Ontouchevent can execute, then the return value of Dispatchtouchevent will have to look at the return of Ontouchevent. A bit around (Dispatchtouchevent return false can be understood to be consumed by their own ontouchevent, note that the ontouchevent also returns TRUE or FALSE, If true is given to the parent control's ontouchenent and Dispatchtouchevent also returns True, if returning false,dispatchtouchevent also returns false, the event is returned to the parent View's Ontouchevent for consumption)
In fact, there is a situation, that is, return the system default super.dispatchtouchevent (EV), the event will be automatically distributed to the current View of the Onintercepttouchevent method.

So then, let's take a look at the onintercepttouchevent, which is a better understanding.
Here's a quote from someone else's article:
The Onintercepttouchevent event interception logic is as follows:

1 if Onintercepttouchevent returns True, the event is intercepted and the intercepted event is referred to the current View's ontouchevent for processing;
2 if Onintercepttouchevent returns false, it means that the event is released, the event on the current view is passed to the child view, and then the dispatchtouchevent of the view begins the distribution of the event (repeating the previous logic);
3 if Onintercepttouchevent returns to Super.onintercepttouchevent (EV), the event is intercepted by default and the intercepted event is referred to the current View's ontouchevent for processing. (I think the original text here is not quite right, it should be the same as the return false, the event continues to go down to the sub-view, how do you think, welcome message ... 0-0)

Continue ontouchevent below:
Ontouchevent is used to handle events, and the return value determines whether the current control consumes (consume) the event. In particular, for the Action_down event, returns True, indicating that I want to handle subsequent events; returns false, indicating that the event is not cared for and returned by the parent class's ontouchevent (as distinguished from onintercepttouchevent, The ontouchevent is passed up until it encounters a true to be consumed) for processing.

Let's summarize:
The onintercepttouchevent is responsible for intercepting the event and then handing it to the view that first encountered the Ontouchevent return true. The order of execution of the Onintercepttouchevent event is performed by the parent control to the child control, and the Ontouchevent method takes precedence over its own control, and the Ontouchevent event executes exactly the opposite of the control to the parent control

Reference article:

http://blog.csdn.net/fangchao3652/article/details/43191159
http://blog.csdn.net/guolin_blog/article/details/9097463#
http://blog.csdn.net/fangchao3652/article/details/43190601
http://blog.csdn.net/guitk/article/details/7057155

Android dispatchtouchevent, onintercepttouchevent, Ontouchevent detailed

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.