Android dispatchTouchEvent, onInterceptTouchEvent, onTouchEvent Detailed

来源:互联网 发布:通达信软件公式函数 编辑:程序博客网 时间:2024/04/30 11:19

First processing documentation on these three events:

The first one is: of ViewGroup that dispathTouchEvent (MotionEvent ev): pass the Touch event to the target view ().

The second is: of ViewGroup that onInterceptTouchEvent (MotionEvent ev): ViewGroup defined, used to intercept the transfer of Touch events.

Third: View onTouchEvent (the MotionEvent event): the Touch event handlers.

Translation: the ViewGroup The second ViewGroup onInterceptTouchEvent (MotionEvent ev)method

Implement this method to intercept all touch screen motion events. This allows you to watchevents as they are dispatched to your children, and take ownership of the current gesture at any point.

Implement this method to intercept all touch screen events, allows each node to view the event when the event is distributed to sub-view, and have the current gesture ownership

 

Using this function takes some care, as it has a fairly complicated interaction with   and Using it requires IMPLEMENTING that method as well as this one in the correct way. Events will be received in the Following order:

Use this feature to be very careful, because this method and rather complex, using this method requires the correct implementation of these two methods. The event is received in accordance with the following process:

 

1.You will receive the down event here.

First received down event here

 

2.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 should 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.

A down event can view group sub-control processing, or view group to deal with their own.Mean, if you achieve a onTouchEvent () and returns a true, then you will see the following gestures (rather than continue to look for a parent control to handle this event). Also, if you onTouchEvent () to return a true, onInterceptTouchEvent () will no longer receive any of the following events, all touch screen handling in the onTouchEvent ().

 

3.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 all events in this method returns false, the next (containing the until up) will be transmitted to the first and then on to the target onTouchEvent ().

 

4.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   , and all further events will be delivered to your onTouchEvent () METHOD and no longer appear here.

If this returns a ture, you will not receive any of the next event: the target view will receive the same event, action but , and all subsequent events will be passed to the view onTouchEvent () method to deal with, no longer appear here.

 

 

Parameters parameter
ev The motion event being dispatched down the hierarchy.

ev level motion passed down event

 

Returns the return value

Return true to steal motion events from the children and have them dispatched to this ViewGroup through onTouchEvent (). The current target will receive an ACTION_CANCEL event, and no further messages will be delivered here.

Returns a true value to action events from child controls through onTouchEvent () method to delivery to the group view. This the target receives a ACTION_CANCEL event, and there is no more information will be passed to here.

 

To talk about the event passed in two ways: 
Tunnel: from the root element in turn passed down until the innermost sub-elements or an element in the middle due to a condition to stop the pass. 
Bubble: in order from the innermost sub-elements pass out until the root element or as a condition to stop the pass in the middle of an element.

 

Following a simple experiment to illustrate the complex rules. View from the bottom up of 3 layers, where LayoutView1 and LayoutView2 LinearLayout MyTextView TextView: corresponding xml layout file is as follows:

view plain
  1. <span style="font-size:16px;"><com.touchstudy.LayoutView1 xmlns:android="  
  2.     android:layout_width="fill_parent"  
  3.     android:layout_height="fill_parent"  
  4.     android:orientation="vertical" >  
  5.     <com.touchstudy.LayoutView2  
  6.         android:layout_width="fill_parent"  
  7.         android:layout_height="fill_parent"  
  8.         android:gravity="center"  
  9.         android:orientation="vertical" >  
  10.         <com.touchstudy.MyTextView  
  11.             android:id="@+id/tv"  
  12.             android:layout_width="wrap_content"  
  13.             android:layout_height="wrap_content"  
  14.             android:background="#FFFFFF"  
  15.             android:text="AB"  
  16.             android:textColor="#0000FF"  
  17.             android:textSize="40sp"  
  18.             android:textStyle="bold" />  
  19.     </com.touchstudy.LayoutView2>  
  20. </com.touchstudy.LayoutView1></span>  

Main Activity: MainActivity.java

view plain
  1. <span style="font-size:16px;">package com.touchstudy;  
  2.   
  3.   
  4. import android.os.Bundle;  
  5. import android.app.Activity;  
  6. import android.view.Window;  
  7. public class MainActivity extends Activity {  
  8.     @Override  
  9.     public void onCreate(Bundle savedInstanceState) {  
  10.         super.onCreate(savedInstanceState);  
  11.         this.requestWindowFeature(Window.FEATURE_NO_TITLE);  
  12.         setContentView(R.layout.activity_main);  
  13.     }  
  14. }  
  15. </span>  

LayoutView1.java

view plain
  1. <span style="font-size:16px;">package com.touchstudy;  
  2.   
  3. import android.content.Context;  
  4. import android.util.AttributeSet;  
  5. import android.util.Log;  
  6. import android.view.MotionEvent;  
  7. import android.widget.LinearLayout;  
  8.   
  9. public class LayoutView1 extends LinearLayout {  
  10.   
  11.     private final String TAG = "LayoutView1";  
  12.   
  13.     public LayoutView1(Context context, AttributeSet attrs) {  
  14.         super(context, attrs);  
  15.         Log.d(TAG, TAG);  
  16.     }  
  17.   
  18.     @Override  
  19.     public boolean onInterceptTouchEvent(MotionEvent ev) {  
  20.         int action = ev.getAction();  
  21.         switch (action) {  
  22.         case MotionEvent.ACTION_DOWN:  
  23.             Log.d(TAG, "onInterceptTouchEvent action:ACTION_DOWN");  
  24.             break;  
  25.         case MotionEvent.ACTION_MOVE:  
  26.             Log.d(TAG, "onInterceptTouchEvent action:ACTION_MOVE");  
  27.             break;  
  28.         case MotionEvent.ACTION_UP:  
  29.             Log.d(TAG, "onInterceptTouchEvent action:ACTION_UP");  
  30.             break;  
  31.         case MotionEvent.ACTION_CANCEL:  
  32.             Log.d(TAG, "onInterceptTouchEvent action:ACTION_CANCEL");  
  33.             break;  
  34.         }  
  35.         return false;  
  36.     }  
  37.   
  38.     @Override  
  39.     public boolean onTouchEvent(MotionEvent ev) {  
  40.         int action = ev.getAction();  
  41.         switch (action) {  
  42.         case MotionEvent.ACTION_DOWN:  
  43.             Log.d(TAG, "onTouchEvent action:ACTION_DOWN");  
  44.             break;  
  45.         case MotionEvent.ACTION_MOVE:  
  46.             Log.d(TAG, "onTouchEvent action:ACTION_MOVE");  
  47.             break;  
  48.         case MotionEvent.ACTION_UP:  
  49.             Log.d(TAG, "onTouchEvent action:ACTION_UP");  
  50.             break;  
  51.         case MotionEvent.ACTION_CANCEL:  
  52.             Log.d(TAG, "onTouchEvent action:ACTION_CANCEL");  
  53.             break;  
  54.         }  
  55.         return false;  
  56.     }  
  57.   
  58.     @Override  
  59.     public boolean dispatchTouchEvent(MotionEvent ev) {  
  60.         int action = ev.getAction();  
  61.         switch (action) {  
  62.         case MotionEvent.ACTION_DOWN:  
  63.             Log.d(TAG, "dispatchTouchEvent action:ACTION_DOWN");  
  64.             break;  
  65.         case MotionEvent.ACTION_MOVE:  
  66.             Log.d(TAG, "dispatchTouchEvent action:ACTION_MOVE");  
  67.             break;  
  68.         case MotionEvent.ACTION_UP:  
  69.             Log.d(TAG, "dispatchTouchEvent action:ACTION_UP");  
  70.             break;  
  71.         case MotionEvent.ACTION_CANCEL:  
  72.             Log.d(TAG, "dispatchTouchEvent action:ACTION_CANCEL");  
  73.             break;  
  74.         }  
  75.         return false;  
  76.     }  
  77.   
  78. }  
  79. </span>  

LayoutView2.java

view plain
  1. <span style="font-size:16px;">package com.touchstudy;  
  2.   
  3. import android.content.Context;  
  4. import android.util.AttributeSet;  
  5. import android.util.Log;  
  6. import android.view.MotionEvent;  
  7. import android.widget.LinearLayout;  
  8.   
  9. public class LayoutView2 extends LinearLayout {  
  10.   
  11.     private final String TAG = "LayoutView2";  
  12.   
  13.     public LayoutView2(Context context, AttributeSet attrs) {  
  14.         super(context, attrs);  
  15.         Log.d(TAG, TAG);  
  16.     }  
  17.   
  18.     @Override  
  19.     public boolean onInterceptTouchEvent(MotionEvent ev) {  
  20.         int action = ev.getAction();  
  21.         switch (action) {  
  22.         case MotionEvent.ACTION_DOWN:  
  23.             Log.d(TAG, "onInterceptTouchEvent action:ACTION_DOWN");  
  24.             break;  
  25.         case MotionEvent.ACTION_MOVE:  
  26.             Log.d(TAG, "onInterceptTouchEvent action:ACTION_MOVE");  
  27.             break;  
  28.         case MotionEvent.ACTION_UP:  
  29.             Log.d(TAG, "onInterceptTouchEvent action:ACTION_UP");  
  30.             break;  
  31.         case MotionEvent.ACTION_CANCEL:  
  32.             Log.d(TAG, "onInterceptTouchEvent action:ACTION_CANCEL");  
  33.             break;  
  34.         }  
  35.         return false;  
  36.     }  
  37.   
  38.     @Override  
  39.     public boolean onTouchEvent(MotionEvent ev) {  
  40.         int action = ev.getAction();  
  41.         switch (action) {  
  42.         case MotionEvent.ACTION_DOWN:  
  43.             Log.d(TAG, "onTouchEvent action:ACTION_DOWN");  
  44.             break;  
  45.         case MotionEvent.ACTION_MOVE:  
  46.             Log.d(TAG, "onTouchEvent action:ACTION_MOVE");  
  47.             break;  
  48.         case MotionEvent.ACTION_UP:  
  49.             Log.d(TAG, "onTouchEvent action:ACTION_UP");  
  50.             break;  
  51.         case MotionEvent.ACTION_CANCEL:  
  52.             Log.d(TAG, "onTouchEvent action:ACTION_CANCEL");  
  53.             break;  
  54.         }  
  55.         return false;  
  56.     }  
  57.   
  58.     @Override  
  59.     public boolean dispatchTouchEvent(MotionEvent ev) {  
  60.         int action = ev.getAction();  
  61.         switch (action) {  
  62.         case MotionEvent.ACTION_DOWN:  
  63.             Log.d(TAG, "dispatchTouchEvent action:ACTION_DOWN");  
  64.             break;  
  65.         case MotionEvent.ACTION_MOVE:  
  66.             Log.d(TAG, "dispatchTouchEvent action:ACTION_MOVE");  
  67.             break;  
  68.         case MotionEvent.ACTION_UP:  
  69.             Log.d(TAG, "dispatchTouchEvent action:ACTION_UP");  
  70.             break;  
  71.         case MotionEvent.ACTION_CANCEL:  
  72.             Log.d(TAG, "dispatchTouchEvent action:ACTION_CANCEL");  
  73.             break;  
  74.         }  
  75.         return false;  
  76.     }  
  77. }  
  78. </span>  

 

MyTextView.java

view plain
  1. <span style="font-size:16px;">package com.touchstudy;  
  2.   
  3. import android.content.Context;  
  4. import android.util.AttributeSet;  
  5. import android.util.Log;  
  6. import android.view.MotionEvent;  
  7. import android.widget.TextView;  
  8.   
  9. public class MyTextView extends TextView {  
  10.     private final String TAG = "MyTextView";  
  11.   
  12.     public MyTextView(Context context, AttributeSet attrs) {  
  13.         super(context, attrs);  
  14.         Log.d(TAG, TAG);  
  15.     }  
  16.   
  17.     @Override  
  18.     public boolean onTouchEvent(MotionEvent ev) {  
  19.         int action = ev.getAction();  
  20.         switch (action) {  
  21.         case MotionEvent.ACTION_DOWN:  
  22.             Log.d(TAG, "onTouchEvent action:ACTION_DOWN");  
  23.             break;  
  24.         case MotionEvent.ACTION_MOVE:  
  25.             Log.d(TAG, "onTouchEvent action:ACTION_MOVE");  
  26.             break;  
  27.         case MotionEvent.ACTION_UP:  
  28.             Log.d(TAG, "onTouchEvent action:ACTION_UP");  
  29.             break;  
  30.         case MotionEvent.ACTION_CANCEL:  
  31.             Log.d(TAG, "onTouchEvent action:ACTION_CANCEL");  
  32.             break;  
  33.         }  
  34.         return false;  
  35.     }  
  36.   
  37. }  
  38. </span>  


Prelude to a total of three the view LayoutView1 LayoutView2 MyTextView. They contain relations LayoutView1> LayoutView2> MyTextView. That LayoutView1, LayoutView2 of ViewGroup that, and is View MyTextView. (ViewGroup is a direct subclass of View) three LayoutView1 have dispatchTouchEvent (), onInterceptTouchEvent (), onTouchEvent () method. LayoutView2 dispatchTouchEvent (), onInterceptTouchEvent (), onTouchEvent () three methods. MyTextView onTouchEvent () method. Then, according to the tunneling event pass, so know LayoutView1 dispatchTouchEvent to () is the inlet of the test method (also this is a first response Touch event). We tested variables: 1. Whether to perform the processing method of the parent class. 2 return value of true or false.

The first part, dispatchTouchEvent () method:

1 dispatchTouchEvent () method, the return value to decide whether to block subsequent events.

The false shielded the subsequent events ACTION_MOVE ACTION_UP.

The true, shielded the subsequent events ACTION_MOVE ACTION_UP.

dispatchTouchEvent () method, whether to perform super.dispatchTouchEvent (ev).

Implementation, to call onInterceptTouchEvent () method onTouchEvent () method.

Does not perform, do not call onInterceptTouchEvent () method onTouchEvent () method.

 

The second part of the analysis onInterceptTouchEvent () method:

onInterceptTouchEvent () method, the return value

true, intercept, not down to a level dispatchTouchEvent () method to pass

false, blocking, the down level dispatchTouchEvent () method to pass

Both cases the method of the parent class has nothing to do whether to perform.

The third part, onTouchEvent () method of analysis:

1.onTouchEvent () method, the return value

The false, shielded the subsequent events ACTION_MOVE ACTION_UP.

true, and not to shield the subsequent events ACTION_MOVE ACTION_UP.

Both cases the method of the parent class has nothing to do whether to perform.

Touch event by dispatchTouchEvent the tunnel passed from the top down. The onTouchEvent a View returns true, the next event (After the ACTION_DOWN the ACTION_UP, and that may be contained in the middle certain ACTION_MOVE of, from ACTION_DOWN to ACTION_UP a continuous event, which is they want, and not know the exact N) will still be passed to the View onTouchEvent returns false, the next event will not be passed to the View, but the implementation of the Parent View's onTouchEvent return false onTouchEvent event whenever a View Next event (if there are any) will stop at this View of Parent View, each up a level, bubble-like way.

 

Touch events are passed through the elements in the process are a View, event handling outermost element is not the view is lowered with Window-related events, when a Touch incident, first call the current activity dispatchTouchEvent function , before the event is passed to the View of the lower element. When dispatchTouchEvent passed down through a View, The View is a ViewGroup to call its onInterceptTouchEvent function, this function indicates whether to intercept Touch event, if the function returns true, the said this ViewGroup intercepted the pass of the event, Touch events are not then passed down to its sub-View, but dealt with it, it will call it onTouchEvent function, in the process of passing ViewGroup intercept events, that is, after all ViewGroup returns false, the event will eventually pass to the innermost View, ships is a widget, of course, also be a ViewGroup (its interior does not contain any element) if the last event is passed to the View (non ViewGroup), then first call this View onTouchListener (if set onTouchEvent (a), If onTouchListener returns false then continue to call View, default true), if the last event is passed to a ViewGroup (no sub-View), will call it onTouchEvent function, the default return false.


If you call a the onTouchEvent function of View when returns true, then the next Touch Event event (the the ACTION_DOWN ACTION_UP and may be included in the middle of a number of ACTION_MOVE, from ACTION_DOWN to ACTION_UP as a continuous event, this is what they want, not know the exact N) will continue to be passed to the View and call it the onTouchEvent function onTouchEvent function can conditionally return a different value if a time in this function returns false then the Touch Event event would not and then passed to the View, and in the Parent View terminated call the Parent View onTouchEvent. If all View onTouchEvent function returns false, then the Touch Event event will be handled by the Activity calling Activity onTouchEvent function.

 

When the when ViewGroup dispatchTouchEvent function is called, will be the first call onInterceptTouchEvent function to determine not intercept events, no interceptions (returns false), it will in turn call ViewGroup the child View dispatchTouchEvent function. For example, a FrameLayout is stacked on three of ViewGroup that, then turn in this FrameLayout dispatchTouchEvent of call the these three ViewGroup dispatchTouchEvent of function, and in these three ViewGroup dispatchTouchEvent of will in turn call their the child View dispatchTouchEvent function until The one View dispatchTouchEvent returns true, said it had dealt with the Touch event, do not need to call this View Slibling Views. These three stacked ViewGroup dispatchTouchEvent of functions such as call, if the first ViewGroup dispatchTouchEvent of function returns true (has consumed the event), then the other two ViewGroup dispatchTouchEvent of will no longer be called. Can customize a ViewGroup subclass to reload his dispatchTouchEvent function it treated Touch event still returns false, then it also calls the other brothers View dispatchTouchEvent function.

0 0
原创粉丝点击