Differences between Android onTouchEvent and onInterceptTouchEvent

Source: Internet
Author: User

First, understand the two words

OnTouchEvent: triggers a touch event

OnInterceptTouchEvent: triggers the interception touch event

View Source Code and class inheritance relationships

OnInterceptTouchEvent: A method defined in ViewGroup. This event is used to intercept touch events. ViewGroup (inherited from View), a View Group, that is, a layout of ours, such as LinerLayout, is inherited from ViewGroup;

OnTouchEvent: A method defined in the View to process the gesture touch event passed to the View. The types of gesture events include ACTION_DOWN, ACTION_MOVE, ACTION_UP, and ACTION_CANCEL;

The default value of onInterceptTouchEvent in ViewGroup is false, so that the touch event is passed to the View control. The default value of onTouchEvent in ViewGroup is false;

The default value of onTouchEvent in the View is true. When we click the screen with our fingers, the ACTION_DOWN event is called first. When the return value in onTouchEvent is true, onTouch will continue to call the ACTION_UP event, if the returned value in onTouchEvent is false, onTouchEvent only calls ACTION_DOWN instead of ACTION_UP.

1. Create two classes LLayout. The LView is as follows:

Copy codeThe Code is 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 to the following layout:
Copy codeThe Code is 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) click the button in the interface

(2) Click another area on the page.

Conclusion: In LLayout, the default value of onInterceptTouchEvent is false, and the default value of onTouchEvent is false. Therefore, only ACTION_DOWN events are called;

The default value of onTouchEvent in LView is true. Two events ACTION_DOWN and ACTION_UP are called;

(3) Modify onInterceptTouchEvent in LLayout to return true and run the code again:

Conclusion: In LLayout, onInterceptTouchEvent returns true and intercepts the touch event. Therefore, the event is not passed to the View, but the onTouchEvent event in LLayout is directly executed;

(4) change the returned value of onInterceptTouchEvent in LLayout to false, and then change onTouchEvent in LView to false:

Conclusion: Because the returned value of onTouchEvent in LView is changed to false, only ACTION_DOWN is executed, and then onTouchEvent is executed in LLayout;

The default value of onInterceptTouchEvent in ViewGroup is false to pass the event to onTouchEvent in View.

The default onTouchEvent value in ViewGroup is false.

The default value of onTouchEvent returned in the View is true. In this way, multiple touch events can be executed.

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.