Discussion on the difference between Android ontouchevent and Onintercepttouchevent _android

Source: Internet
Author: User

First of all, two words in the literal sense

Ontouchevent: Triggering touch events

Onintercepttouchevent: Triggering intercept touch events

By viewing source code and class inheritance relationships

Onintercepttouchevent: is a method defined in ViewGroup that is used to intercept touch events, ViewGroup (inherited from view), a view group, That is, we have a layout such as Linerlayout, each layout class inherits from ViewGroup;

Ontouchevent: is a method defined in view that handles gesture-touching events passed to view. Gesture event types include action_down,action_move,action_up,action_cancel and other events;

The default return value of the onintercepttouchevent in ViewGroup is false so that the touch event is passed to the view control, and the ontouchevent default return value in ViewGroup is false;

The ontouchevent default return value in view is true, and when we click on the screen, the Action_down event is invoked, and when the Ontouchevent return value is true, Ontouch continues to invoke the Action_up event. If the return value in Ontouchevent is false, then ontouchevent will only invoke Action_down without invoking action_up.

1, the new two class Llayout, LView as follows

Copy Code code as follows:

public class Llayout extends Framelayout {
ViewGroup
@Override
public boolean onintercepttouchevent (Motionevent ev) {
LOG.I ("Ltag", "Llayout onintercepttouchevent");
LOG.I ("Ltag", "Llayout onintercepttouchevent default return" + super.onintercepttouchevent (EV));
return super.onintercepttouchevent (EV);
}
View
@Override
public boolean ontouchevent (Motionevent event) {
LOG.I ("Ltag", "Llayout ontouchevent");
LOG.I ("Ltag", "Llayout ontouchevent default return" + Super.ontouchevent (event));
Return Super.ontouchevent (event);
}
}
public class LView extends Button {
TextView <--View
@Override
public boolean ontouchevent (Motionevent event) {
LOG.I ("Ltag", "ontouchevent");
LOG.I ("Ltag", "ontouchevent default return" + Super.ontouchevent (event));
Return Super.ontouchevent (event);
}
}

2, modify the layout file for the following layout
Copy Code code as follows:

<com.touchpro.llayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent" >
<com.touchpro.lview
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "@string/app_name"/>
</com.touchpro.LLayout>

(1) First click on the button in the interface

(2) Click on other areas in the interface

Conclusion: Onintercepttouchevent default return value in Llayout is false,ontouchevent the default return value is False, so only the Action_down event is invoked;

The default return value of Ontouchevent in LView is true; action_down,action_up two events were invoked;

(3) Modify the Onintercepttouchevent return value of Llayout to true, and run the code again:

Conclusion: In Llayout, Onintercepttouchevent returns True and intercepts the touch event, so the event is not passed to view, and the Ontouchevent event in Llayout is executed directly.

(4) Change the Onintercepttouchevent return value in Llayout to False, and then change ontouchevent in LView to return false:

Conclusion: Because the Ontouchevent return value in LView is modified to false, only the Action_down is executed, and then the Ontouchevent event is executed in Llayout;

The onintercepttouchevent default value in ViewGroup is false so that the event can be passed to the ontouchevent in the view.

The ontouchevent default value in ViewGroup is false.

Ontouchevent in view returns the default value is True. This allows multiple touch events to be performed.

Related Article

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.