Onintercepttouchevent and ontouchevent call time series)

Source: Internet
Author: User

Address: http://blog.csdn.net/ddna/article/details/5473293

Onintercepttouchevent and ontouchevent Call Sequence

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 SDK provides the following instructions:

· You will receive the down event here.

· The down event will be handled either by a child of this view group, or given to your own ontouchevent () method to handle; this means you shoshould implement ontouchevent () to return true, so you will continue to see the rest of the gesture
(Instead of looking for a parent view to handle it ). also, by returning true from ontouchevent (), you will not receive any following events in onintercepttouchevent () and all touch processing must happen in ontouchevent () like normal.

· For as long as you return false from this function, each following event (up to and including the final up) will be delivered first here and then to the target's ontouchevent ().

· If you return true from here, you will not receive any following events: the target view will receive the same event but with the action action_cancel, and all further events will be delivered to your ontouchevent () method and no longer appear here.

 

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.

 

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. When onintercepttouchevent () is used to process down events, false is returned, and when ontouchevent () is used to process events, true is returned.

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: When both onintercepttouchevent return false, the ontouchevent of layoutview1 and layoutview2 will not receive the event, But finally passed to mytextview.

 

2. The onintercepttouchevent () of layoutview1 processes the down event and returns true,

Mytextview ontouchevent () Processing Event returns true

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, layoutview2 onintercepttouchevent () processing down event returns false,

Mytextview ontouchevent () Processing Event returns false

The ontouchevent () Processing Event of layoutview2 returns true.

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 that subsequent move and up events are passed to ontouchevent () of layoutview2, but not to mytextview.

 

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

In response to the requirements of everyone, I pasted the source code, which is actually very simple. It is the basic file, mainly used to observe the transmission of events.

 

Main activity: intercepttouchstudyactivity. Java:

 

Public classIntercepttouchstudyactivityExtendsActivity {

Static finalStringTag= "Itsactivity ";

Textview TV;

/** Called when the activity is first created .*/

@ Override

Public voidOncreate (bundle savedinstancestate ){

Super. Oncreate (savedinstancestate );

Setcontentview (R. layout.Layers_touch_pass_test);

}

}

 

Layoutview1.java:

Public classLayoutview1ExtendsLinearlayout {

Private FinalString tag = "layoutview1 ";

PublicLayoutview1 (context, attributeset attrs ){

Super(Context, attrs );

Log.D(TAG, tag );

}

 

@ Override

Public BooleanOnintercepttouchevent (motionevent eV ){

IntAction = eV. getaction ();

Switch(Action ){

CaseMotionevent.Action_down:

Log.D(TAG, "onintercepttouchevent action: action_down ");

// Return true;

Break;

CaseMotionevent.Action_move:

Log.D(TAG, "onintercepttouchevent action: action_move ");

Break;

CaseMotionevent.Action_up:

Log.D(TAG, "onintercepttouchevent action: action_up ");

Break;

CaseMotionevent.Action_cancel:

Log.D(TAG, "onintercepttouchevent action: action_cancel ");

Break;

}

Return false;

}

 

@ Override

Public BooleanOntouchevent (motionevent eV ){

IntAction = eV. getaction ();

Switch(Action ){

CaseMotionevent.Action_down:

Log.D(TAG, "ontouchevent action: action_down ");

Break;

CaseMotionevent.Action_move:

Log.D(TAG, "ontouchevent action: action_move ");

Break;

CaseMotionevent.Action_up:

Log.D(TAG, "ontouchevent action: action_up ");

Break;

CaseMotionevent.Action_cancel:

Log.D(TAG, "ontouchevent action: action_cancel ");

Break;

}

Return true;

}

 

@ Override

Protected voidOnlayout (BooleanChanged,IntL,IntT,IntR,IntB ){

//TodoAuto-generated method stub

Super. Onlayout (changed, L, t, R, B );

}

 

@ Override

Protected voidOnmeasure (IntWidthmeasurespec,IntHeightmeasurespec ){

//TodoAuto-generated method stub

Super. Onmeasure (widthmeasurespec, heightmeasurespec );

}

}

Layoutview2.java:

 

Public classLayoutview2ExtendsLinearlayout {

Private FinalString tag = "layoutview2 ";

PublicLayoutview2 (context, attributeset attrs ){

Super(Context, attrs );

Log.D(TAG, tag );

}

 

@ Override

Public BooleanOnintercepttouchevent (motionevent eV ){

IntAction = eV. getaction ();

Switch(Action ){

CaseMotionevent.Action_down:

Log.D(TAG, "onintercepttouchevent action: action_down ");

Break;

CaseMotionevent.Action_move:

Log.D(TAG, "onintercepttouchevent action: action_move ");

Break;

CaseMotionevent.Action_up:

Log.D(TAG, "onintercepttouchevent action: action_up ");

Break;

CaseMotionevent.Action_cancel:

Log.D(TAG, "onintercepttouchevent action: action_cancel ");

Break;

}

Return false;

}

 

@ Override

Public BooleanOntouchevent (motionevent eV ){

IntAction = eV. getaction ();

Switch(Action ){

CaseMotionevent.Action_down:

Log.D(TAG, "ontouchevent action: action_down ");

Break;

CaseMotionevent.Action_move:

Log.D(TAG, "ontouchevent action: action_move ");

Break;

CaseMotionevent.Action_up:

Log.D(TAG, "ontouchevent action: action_up ");

Break;

CaseMotionevent.Action_cancel:

Log.D(TAG, "ontouchevent action: action_cancel ");

Break;

}

Return true;

}

}

Mytextview. Java:

Public classMytextviewExtendsTextview {

Private FinalString tag = "mytextview ";

PublicMytextview (context, attributeset attrs ){

Super(Context, attrs );

Log.D(TAG, tag );

}

 

@ Override

Public BooleanOntouchevent (motionevent eV ){

IntAction = eV. getaction ();

Switch(Action ){

CaseMotionevent.Action_down:

Log.D(TAG, "ontouchevent action: action_down ");

Break;

CaseMotionevent.Action_move:

Log.D(TAG, "ontouchevent action: action_move ");

Break;

CaseMotionevent.Action_up:

Log.D(TAG, "ontouchevent action: action_up ");

Break;

CaseMotionevent.Action_cancel:

Log.D(TAG, "ontouchevent action: action_cancel ");

Break;

}

Return false;

}

Public voidOnclick (view v ){

Log.D(TAG, "onclick ");

}

Public BooleanOnlongclick (view v ){

Log.D(TAG, "onlongclick ");

Return false;

}

}

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.