Examples and summary of Android onInterceptTouchEvent () and onTouchEvent () -- Preparations for Launcher

Source: Internet
Author: User

If you have read some of the following articles and are familiar with them, you don't have to think about them. I have intercepted ^ 0 ^, And then I have made up some details to facilitate understanding. (Note: The following is a combination of two articles on the Internet. I just added some changes)

Concepts

1. onintercepttouchevent () is used to process events (the important onintercepttouchevent event is transmitted from the parent control to the Child control until there is an interception or a view without this event, then, we will move back from the child to the parent control, which is ontouch this time) (similar to preprocessing, but we can also not handle it) and change the event transmission direction, that is to say, determine whether to allow the touch event to be passed down (sub-control). 1. If true is returned (the event will be processed in the current viewgroup ), the next pass-down path is truncated (all child controls will not be able to participate in the touch event), and the event is passed to the ontouchevent () of the current control for processing; false is returned, the event is handed over to the onintercepttouchevent () of the sub-control ()

 

2. ontouchevent () is used to process events (the important ontouch event is uploaded from the Child control back to the parent control, which is passed down layer by layer). The returned value determines whether the current control consumes the event (consume, that is to say, whether the touch event can be passed up (parent Control) after the current control finishes processing the touch event. If false is returned, it is passed up to the parent control. For details, the touch event is sent to the parent control, and the up event is triggered by touch here, it will not be passed to its child control. If the parent control is still false, touch's processing will be sent to the parent control of the parent control, so the up event processing will be in the parent control of the parent control, and will not trigger the following.

Returns true. If the child control returns true, all its touch events are processed here. The parent control cannot handle this because it cannot receive the touch that the child control sends to it, the quilt control is blocked. (This is to deepen the memory. These two events are so troublesome to understand. Let alone remember, I must have forgotten them all at once. ^ 0 ^)

(Note: Do you think it is related to consumption? Anyway, I have compiled the processing code for the event? The answer is yes! For example, the premise that ACTION_MOVE or ACTION_UP occurs is that ACTION_DOWN has occurred. If you have not consumed ACTION_DOWN, the system will think that ACTION_DOWN has not happened, so ACTION_MOVE or ACTION_UP cannot be captured .)

Details

OnInterceptTouchEvent () is a method of ViewGroup. The purpose is to intercept related events before the system triggers onTouchEvent () to the ViewGroup and its various childView. The idea of Android design is also very understandable, because ViewGroup contains several childView S, you need to be able to monitor various touch events in a unified manner. Therefore, this method is not available for controls that cannot contain child views, such as LinearLayout, textView does not exist.

OnInterceptTouchEvent () is also very easy to use. If you overwrite this method in ViewGroup, You can intercept various touch events. However, it is complicated to intercept all touch events. The transfer mechanism of touch events in onInterceptTouchEvent () and onTouchEvent and between various childView depend entirely on onInterceptTouchEvent () and onTouchEvent. In addition, the returned values for down event processing directly affect the receipt and transmission of subsequent move and up events.

The basic rule for the return value is clear. If return is true, the method consumes the event. If return is false, the method is not completely processed, the event still needs to be passed in some way and continues waiting for processing.

 

The onInterceptTouchEvent () mechanism is complex and the description above is also complex. To sum up, the basic rules are as follows:

1. The down event is first passed to the onInterceptTouchEvent () method.

2. if the onintercepttouchevent () of the viewgroup returns false after the down event is processed, the subsequent move, up, and other events will be passed to the viewgroup first, the ontouchevent () process is passed to the final target view just like the down event.

3. if the onintercepttouchevent () of the viewgroup returns true after it receives the down event processing, the subsequent move, up and other events will not be passed to onintercepttouchevent (), the ontouchevent () that is passed to the viewgroup like the down event. Note that the target view will not receive any event.

4. If the ontouchevent () of the view that finally needs to process the event returns false, the event will be passed to the ontouchevent () of the view at the previous level for processing.

5. If the ontouchevent () of the view that finally needs to process the event returns true, subsequent events can be passed to the ontouchevent () of the view for processing.

 

Example

The following uses a simple experiment to describe the above complex rules. There are three layers from the bottom to the top of the view. layoutview1 and layoutview2 are linearlayout, and mytextview is textview:

The XML layout file is as follows:

<? XML version ="1.0"Encoding ="UTF-8"?>

<Com. touchstudy. layoutview1 xmlns: Android =Http://schemas.android.com/apk/res/android"

Android: Orientation ="Vertical"

Android: layout_width ="Fill_parent"

Android: layout_height ="Fill_parent">

<Com. touchstudy. LayoutView2

Android: orientation ="Vertical"

Android: layout_width ="Fill_parent"

Android: layout_height ="Fill_parent"

Android: gravity ="Center">

<Com. touchstudy. MyTextView

Android: layout_width ="Wrap_content"

Android: layout_height ="Wrap_content"

Android: id ="@ + Id/TV"

Android: text ="AB"

Android: textSize ="40sp"

Android: textStyle ="Bold"

Android: background ="# FFFFFF"

Android: textColor ="# 0000FF"/>

</Com. touchstudy. LayoutView2>

</Com. touchstudy. LayoutView1>

The details are as follows:

1.OnInterceptTouchEvent ()ProcessingDownAll events are returned.False,OnTouchEvent ()All events are returned.True

Bytes ------------------------------------------------------------------------------------------------------------------------------

04-11 03:58:42. 620: DEBUG/LayoutView1 (614): onInterceptTouchEvent action: ACTION_DOWN

04-11 03:58:42. 620: DEBUG/LayoutView2 (614): onInterceptTouchEvent action: ACTION_DOWN

04-11 03:58:42. 620: DEBUG/MyTextView (614): onTouchEvent action: ACTION_DOWN

04-11 03:58:42. 800: DEBUG/LayoutView1 (614): onInterceptTouchEvent action: ACTION_MOVE

04-11 03:58:42. 800: DEBUG/LayoutView2 (614): onInterceptTouchEvent action: ACTION_MOVE

04-11 03:58:42. 800: DEBUG/MyTextView (614): onTouchEvent action: ACTION_MOVE

...... // Omit excessive ACTION_MOVE

04-11 03:58:43. 130: DEBUG/LayoutView1 (614): onInterceptTouchEvent action: ACTION_UP

04-11 03:58:43. 130: DEBUG/LayoutView2 (614): onInterceptTouchEvent action: ACTION_UP

04-11 03:58:43. 150: DEBUG/MyTextView (614): onTouchEvent action: ACTION_UP

Bytes ------------------------------------------------------------------------------------------------------------------------------

This is the most common situation. onInterceptTouchEvent does not perform any operations to change the time sequence of event transmission, and the effect is the same as that of not overwriting this method. We can see that the transmission of various events is from bottom to top, and the order is: LayoutView1-> LayoutView2-> MyTextView.Note thatOnInterceptTouchEventReturnsFalse,LayoutView1AndLayoutView2OfOnTouchEventNo event will be received, But finally passed to MyTextView.

2.LayoutView1OfOnInterceptTouchEvent ()ProcessingDownEvent returnTrue,

MyTextViewOfOnTouchEvent ()Handle event returnTrue

Bytes ------------------------------------------------------------------------------------------------------------------------------

04-11 03:09:27. 589: DEBUG/LayoutView1 (446): onInterceptTouchEvent action: ACTION_DOWN

04-11 03:09:27. 589: DEBUG/LayoutView1 (446): onTouchEvent action: ACTION_DOWN

04-11 03:09:27. 629: DEBUG/LayoutView1 (446): onTouchEvent action: ACTION_MOVE

04-11 03:09:27. 689: DEBUG/LayoutView1 (446): onTouchEvent action: ACTION_MOVE

...... // Omit excessive ACTION_MOVE

04-11 03:09:27. 959: DEBUG/LayoutView1 (446): onTouchEvent action: ACTION_UP

Bytes ------------------------------------------------------------------------------------------------------------------------------

As you can see from the log, layoutview1 returns true when intercepting the first down event, so subsequent events (including the first down event) will be handled by layoutview1 itself, and the events will not be transmitted.

3.LayoutView1,LayoutView2OfOnInterceptTouchEvent ()ProcessingDownEvent returnFalse,

MyTextViewOfOnTouchEvent ()Handle event returnFalse

LayoutView2OfOnTouchEvent ()Handle event returnTrue

Bytes ----------------------------------------------------------------------------------------------------------------------------

04-11 09:50:21. 147: Debug/layoutview1 (301): onintercepttouchevent action: action_down

04-11 09:50:21. 147: Debug/layoutview2 (301): onintercepttouchevent action: action_down

04-11 09:50:21. 147: Debug/mytextview (301): ontouchevent action: action_down

04-11 09:50:21. 147: Debug/layoutview2 (301): ontouchevent action: action_down

04-11 09:50:21. 176: Debug/layoutview1 (301): onintercepttouchevent action: action_move

04-11 09:50:21. 176: Debug/layoutview2 (301): ontouchevent action: action_move

04-11 09:50:21. 206: Debug/layoutview1 (301): onintercepttouchevent action: action_move

04-11 09:50:21. 217: Debug/layoutview2 (301): ontouchevent action: action_move

...... // Omit excessive action_move

04-11 09:50:21. 486: Debug/layoutview1 (301): onintercepttouchevent action: action_up

04-11 09:50:21. 486: Debug/layoutview2 (301): ontouchevent action: action_up

Bytes ----------------------------------------------------------------------------------------------------------------------------

As you can see, because mytextview returns false in ontouchevent (), the down event is passed to its parent view, that is, the ontouchevent () method of layoutview2 is processed () return true, so the down event is not uploaded to layoutview1.Note:MoveAndUpAll events are passedLayoutView2OfOnTouchEvent ()Not passedMyTextView.

Project source code

Http://files.cnblogs.com/not-code/testtouchstudy.zip

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.