写这篇博客的缘由。近期因为项目中用到相似一个LinearLayout中水平布局中,有一个TextView和Button,然后对该LinearLayout布局设置点击事件。点击TextView能够触发该点击事件。然而奇怪的是点击Button却不能触发。
然后google到了解决的方法(重写Button,然后重写当中的ontouchEvent方法,且返回值为false)。可是不知道原因,这两天看了几位大神的博客,然后自己总结下。
public class MyButton extends Button { private final static String TAG = "MyButton::zjt"; public MyButton(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } public MyButton(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub } @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub switch (event.getAction()) { case MotionEvent.ACTION_DOWN: Log.e(TAG, "onTouchEvent ACTION_DOWN"); break; case MotionEvent.ACTION_MOVE: Log.e(TAG, "onTouchEvent ACTION_MOVE"); break; case MotionEvent.ACTION_UP: Log.e(TAG, "onTouchEvent ACTION_UP"); break; default: break; } //return super.onTouchEvent(event); return false; } @Override public boolean dispatchTouchEvent(MotionEvent event) { // TODO Auto-generated method stub switch (event.getAction()) { case MotionEvent.ACTION_DOWN: Log.e(TAG, "dispatchTouchEvent ACTION_DOWN"); break; case MotionEvent.ACTION_MOVE: Log.e(TAG, "dispatchTouchEvent ACTION_MOVE"); break; case MotionEvent.ACTION_UP: Log.e(TAG, "dispatchTouchEvent ACTION_UP"); break; default: break; } return super.dispatchTouchEvent(event); }}
MyTextView.java
public class MyTextView extends TextView { private final static String TAG = "MyTextView : "; public MyTextView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } @Override public boolean dispatchTouchEvent(MotionEvent event) { // TODO Auto-generated method stub switch (event.getAction()) { case MotionEvent.ACTION_DOWN: Log.e(TAG, "dispatchTouchEvent ACTION_DOWN"); break; case MotionEvent.ACTION_MOVE: Log.e(TAG, "dispatchTouchEvent ACTION_MOVE"); break; case MotionEvent.ACTION_UP: Log.e(TAG, "dispatchTouchEvent ACTION_UP"); break; default: break; } return super.dispatchTouchEvent(event); } @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub switch (event.getAction()) { case MotionEvent.ACTION_DOWN: Log.e(TAG, "onTouchEvent ACTION_DOWN"); return true 后面的ACTION_MOVE、和ACTION_UP能够得以运行。假设不做不论什么操作,即 break。因为textview默认是不可点击和长点击的,所以return false, //那么 dispatcTouchEvent 会 return false,导致后面的ACTION_MOVE 和 ACTION_UP不能运行 //return true; break; case MotionEvent.ACTION_MOVE: Log.e(TAG, "onTouchEvent ACTION_MOVE"); break; case MotionEvent.ACTION_UP: Log.e(TAG, "onTouchEvent ACTION_UP"); break; default: break; } return super.onTouchEvent(event); }}
MainActivity例如以下:
public class TestTouchActivity extends Activity { private final static String TAG = "TestTouchActivity"; private Button mButton; private TextView mTextView; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.my_button_layout); mButton = (Button) findViewById(R.id.my_btn); mTextView = (TextView) findViewById(R.id.my_textview);// mTextView.setOnClickListener(new OnClickListener() { // // @Override// public void onClick(View v) { // // TODO Auto-generated method stub// Log.e(TAG, "mTextView onClick");// }// }); mButton.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub int action = event.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: Log.e(TAG, "onTouch ACTION_DOWN"); return true; //break; case MotionEvent.ACTION_MOVE: Log.e(TAG, "onTouch ACTION_MOVE"); break; case MotionEvent.ACTION_UP: Log.e(TAG, "onTouch ACTION_UP"); break; default: break; } return false; } }); mTextView.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub int action = event.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: Log.e(TAG, "mTextView onTouch ACTION_DOWN"); break; case MotionEvent.ACTION_MOVE: Log.e(TAG, "mTextView onTouch ACTION_MOVE"); break; case MotionEvent.ACTION_UP: Log.e(TAG, "mTextView onTouch ACTION_UP"); break; default: break; } return false; } }); }}
点击Button和TextView的节目例如以下:
点击Button:
点击TextView:
为什么结果是这种,參考博文已经写得非常精彩了,我就站在巨人的肩膀上,总结下,我们从上面的结果能够看出,当我们点击屏幕上的View的时候首先触发的是View的dispatchTouchEvent事件。源代码例如以下:
/** * Pass the touch screen motion event down to the target view, or this * view if it is the target. * * @param event The motion event to be dispatched. * @return True if the event was handled by the view, false otherwise. */ public boolean dispatchTouchEvent (MotionEvent event) { if (mOnTouchListener != null && ( mViewFlags & ENABLED_MASK) == ENABLED && mOnTouchListener.onTouch( this, event)) { return true; } return onTouchEvent(event); }
上面的mOnTouchListener 就是我们在Activity中设置的Touch事件,我们设置的时候在onTouch中返回的是false,所以会接着运行以下的onTouchEvent方法,能够看出onTouchEvent的返回值就是dispatchTouchEvent 的返回值。onTouchEvent这种方法源代码比較长,我截断了。
public boolean onTouchEvent(MotionEvent event) { 。
。。
。。。。。。。。 此处有省略 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) { switch (event.getAction()) { 。。
。。。。
。。
。。
。 此处有省略 } return true; } return false; }
第4行就是推断该View是否是可点击或者可长按的。假设是返回true。在onTouchEvent中先运行ACTION_DOWN(手指按下)。假设返回true,那么dispatchTouchEvent 的返回值也就是true。就能够接着运行后面的ACTION_MOVE和ACTION_UP方法。假设返回false。那么后面的ACTION_MOVE和ACTION_UP就不运行了,这个详细原因我还不知道。假设有知道的能够分享下。
说明 1:长按事件是在onTouchEvent中的ACTION_DOWN中触发的(假设你设置了长按事件)。而点击onclick事件是在ACTION_UP中触发的。
如今分析下前面的样例:
因为button默认是可点击的,所以在onTouchEvent中会返回true,所以dispatchTouchEvent 也会返回true。后面的ACTION_MOVE和ACTION_UP能够接着运行。
而TextView默认是不可点击的所以onTouchEvent中会返回false,那么dispatchTouchEvent 也会返回false,后面的ACTION_MOVE和ACTION_UP就运行不到了,和上面打印的log相符。假设我们在Activity中对TextView设置onTouch事件返回true,结果会怎么样呢,我们先就着dispatchTouchEvent 的源代码分析下:
mTextView.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return true; } });
因为返回true,我们从dispatchTouchEvent 源代码的第10行能够看出mOnTouchListener.onTouch( this, event))即返回true。那么if条件就成立了,dispatchTouchEvent 直接返回true。接着运行后面的ACTION_MOVE和ACTION_UP,(ACTION_MOVE假设你点击的时候滑动了才会运行)。可是后面的onTouchEvent就运行不到了。
log例如以下:
没有运行ACTION_MOVE是因为我高速点击且没有滑动,从log能够看出运行完dispatchTouchEvent 的ACTION_DOWN之后又运行了dispatchTouchEvent 的ACTION_UP。但并没有运行onTouchEvent。
以下開始讲主题了,也就是前言交代的问题。以下是我自己定义的ViewGroup:
public class MyLinearLayout extends LinearLayout { private final static String TAG = "MyLinearLayout :"; public MyLinearLayout(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } @Override public boolean dispatchTouchEvent(MotionEvent ev) { // TODO Auto-generated method stub int action = ev.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: Log.e(TAG, "dispatchTouchEvent , ACTION_DOWN"); break; case MotionEvent.ACTION_MOVE: Log.e(TAG, "dispatchTouchEvent , ACTION_MOVE"); break; case MotionEvent.ACTION_UP: Log.e(TAG, "dispatchTouchEvent , ACTION_UP"); break; default: break; } return super.dispatchTouchEvent(ev); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { // TODO Auto-generated method stub int action = ev.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: Log.e(TAG, "onInterceptTouchEvent , ACTION_DOWN"); //return true; break; case MotionEvent.ACTION_MOVE: Log.e(TAG, "onInterceptTouchEvent , ACTION_MOVE"); //return true; break; case MotionEvent.ACTION_UP: Log.e(TAG, "onInterceptTouchEvent , ACTION_UP"); break; default: break; } return super.onInterceptTouchEvent(ev); //return true; } @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub int action = event.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: Log.e(TAG, "onTouchEvent , ACTION_DOWN"); //return true; break; case MotionEvent.ACTION_MOVE: Log.e(TAG, "onTouchEvent , ACTION_MOVE"); break; case MotionEvent.ACTION_UP: Log.e(TAG, "onTouchEvent , ACTION_UP"); break; default: break; } return super.onTouchEvent(event); } @Override public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) { // TODO Auto-generated method stub Log.e(TAG, "enter requestDisallowInterceptTouchEvent"); super.requestDisallowInterceptTouchEvent(disallowIntercept); }
xml例如以下:
MainActivity例如以下:
package com.example.test.view.touch;import com.example.drawview.R;import android.app.Activity;import android.os.Bundle;import android.provider.Telephony.Mms;import android.util.Log;import android.view.MotionEvent;import android.view.View;import android.view.View.OnClickListener;import android.view.View.OnTouchListener;import android.widget.Button;import android.widget.LinearLayout;import android.widget.TextView;public class TestViewGroupeTouchActivity extends Activity { private final static String TAG = "TestViewGroupeTouchActivity : "; private Button mButton ; private TextView mTextView; private LinearLayout mLinearLayout ; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.my_linear_layout); mButton = (Button) findViewById(R.id.btn_click); mTextView = (TextView) findViewById(R.id.my_textview_click); mLinearLayout = (LinearLayout) findViewById(R.id.id_my_linearlayout); mLinearLayout.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Log.e(TAG, "mLinearLayout , onClick"); } }); mLinearLayout.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub int action = event.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: Log.e(TAG, "mLinearLayout , onTouch ACTION_DOWN"); break; case MotionEvent.ACTION_MOVE: Log.e(TAG, "mLinearLayout , onTouch ACTION_MOVE"); break; case MotionEvent.ACTION_UP: Log.e(TAG, "mLinearLayout ,onTouch ACTION_UP"); break; default: break; } return false; } }); mButton.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub int action = event.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: Log.e(TAG, "mButton onTouch ACTION_DOWN"); break; case MotionEvent.ACTION_MOVE: Log.e(TAG, "mButton onTouch ACTION_MOVE"); break; case MotionEvent.ACTION_UP: Log.e(TAG, "mButton onTouch ACTION_UP"); break; default: break; } return false; } }); mTextView.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub switch (event.getAction()) { case MotionEvent.ACTION_DOWN: Log.e(TAG, "mTextView , onTouch ACTION_DOWN"); break; case MotionEvent.ACTION_MOVE: Log.e(TAG, "mTextView , onTouch ACTION_MOVE"); break; case MotionEvent.ACTION_UP: Log.e(TAG, "mTextView , onTouch ACTION_UP"); break; default: break; } return false; } }); }}
说明2: 因为我是为了说明前言里面的问题,所以ViewGroup的touch事件分发。我不作过多的说明。ViewGroup事件分发的流程是:dispatchTouchEvent–>onInterceptTouchEvent—>然后到手指点击View的事件分发(參考上面所说的View的事件分发)。
onInterceptTouchEvent默认返回false,表示是否拦截事件。
ViewGroup的dispatchTouchEvent的源代码例如以下:
[java] view plain copy 在CODE上查看代码片派生到我的代码片/** * {@inheritDoc} */ @Override public boolean dispatchTouchEvent(MotionEvent ev) { final int action = ev.getAction(); final float xf = ev.getX(); final float yf = ev.getY(); final float scrolledXFloat = xf + mScrollX; final float scrolledYFloat = yf + mScrollY; final Rect frame = mTempRect; //这个值默认是false, 然后我们能够通过requestDisallowInterceptTouchEvent(boolean disallowIntercept)方法 //来改变disallowIntercept的值 boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0; //这里是ACTION_DOWN的处理逻辑 if (action == MotionEvent.ACTION_DOWN) { //清除mMotionTarget, 每次ACTION_DOWN都非常设置mMotionTarget为null if (mMotionTarget != null) { mMotionTarget = null; } //disallowIntercept默认是false, 就看ViewGroup的onInterceptTouchEvent()方法 if (disallowIntercept || !onInterceptTouchEvent(ev)) { ev.setAction(MotionEvent.ACTION_DOWN); final int scrolledXInt = (int) scrolledXFloat; final int scrolledYInt = (int) scrolledYFloat; final View[] children = mChildren; final int count = mChildrenCount; //遍历其子View for (int i = count - 1; i >= 0; i--) { final View child = children[i]; //假设该子View是VISIBLE或者该子View正在运行动画, 表示该View才 //能够接受到Touch事件 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) { //获取子View的位置范围 child.getHitRect(frame); //如Touch到屏幕上的点在该子View上面 if (frame.contains(scrolledXInt, scrolledYInt)) { // offset the event to the view's coordinate system final float xc = scrolledXFloat - child.mLeft; final float yc = scrolledYFloat - child.mTop; ev.setLocation(xc, yc); child.mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT; //调用该子View的dispatchTouchEvent()方法 if (child.dispatchTouchEvent(ev)) { // 假设child.dispatchTouchEvent(ev)返回true表示 //该事件被消费了。设置mMotionTarget为该子View mMotionTarget = child; //直接返回true return true; } // The event didn't get handled, try the next view. // Don't reset the event's location, it's not // necessary here. } } } } } //推断是否为ACTION_UP或者ACTION_CANCEL boolean isUpOrCancel = (action == MotionEvent.ACTION_UP) || (action == MotionEvent.ACTION_CANCEL); if (isUpOrCancel) { //假设是ACTION_UP或者ACTION_CANCEL, 将disallowIntercept设置为默认的false //假如我们调用了requestDisallowInterceptTouchEvent()方法来设置disallowIntercept为true //当我们抬起手指或者取消Touch事件的时候要将disallowIntercept重置为false //所以说上面的disallowIntercept默认在我们每次ACTION_DOWN的时候都是false mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT; } // The event wasn't an ACTION_DOWN, dispatch it to our target if // we have one. final View target = mMotionTarget; //mMotionTarget为null意味着没有找到消费Touch事件的View, 所以我们须要调用ViewGroup父类的 //dispatchTouchEvent()方法,也就是View的dispatchTouchEvent()方法 if (target == null) { // We don't have a target, this means we're handling the // event as a regular view. ev.setLocation(xf, yf); if ((mPrivateFlags & CANCEL_NEXT_UP_EVENT) != 0) { ev.setAction(MotionEvent.ACTION_CANCEL); mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT; } return super.dispatchTouchEvent(ev); } //这个if里面的代码ACTION_DOWN不会运行。仅仅有ACTION_MOVE //ACTION_UP才会走到这里, 假如在ACTION_MOVE或者ACTION_UP拦截的 //Touch事件, 将ACTION_CANCEL派发给target,然后直接返回true //表示消费了此Touch事件 if (!disallowIntercept && onInterceptTouchEvent(ev)) { final float xc = scrolledXFloat - (float) target.mLeft; final float yc = scrolledYFloat - (float) target.mTop; mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT; ev.setAction(MotionEvent.ACTION_CANCEL); ev.setLocation(xc, yc); if (!target.dispatchTouchEvent(ev)) { } // clear the target mMotionTarget = null; // Don't dispatch this event to our own view, because we already // saw it when intercepting; we just want to give the following // event to the normal onTouchEvent(). return true; } if (isUpOrCancel) { mMotionTarget = null; } // finally offset the event to the target's coordinate system and // dispatch the event. final float xc = scrolledXFloat - (float) target.mLeft; final float yc = scrolledYFloat - (float) target.mTop; ev.setLocation(xc, yc); if ((target.mPrivateFlags & CANCEL_NEXT_UP_EVENT) != 0) { ev.setAction(MotionEvent.ACTION_CANCEL); target.mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT; mMotionTarget = null; } //假设没有拦截ACTION_MOVE, ACTION_DOWN的话,直接将Touch事件派发给target return target.dispatchTouchEvent(ev); }
当我们点击button的时候,因为我们Activity中重写的onInterceptTouchEvent返回值为super.onInterceptTouchEvent(ev);即默认的false。那么25行的if条件!onInterceptTouchEvent(ev))为true。进入if语句里面,遍历全部的子View,然后运行51行的if (child.dispatchTouchEvent(ev)),上面讲到了Button是可点击的。那么MyButton的onTouchEvent返回值为true,即dispatchTouchEvent返回值为true。消费了该事件。全部不会触发mLinearLayout的点击事件。log例如以下:
那么问题来了。为什么将MyButton 的onTouchEvent返回值设为false,然后点击Button就会触发mLinearLayout的点击事件呢?
我们来分析下:将MyButton 的onTouchEvent返回值设为false,那么51行的if (child.dispatchTouchEvent(ev))的返回值为false,为什么呢?
上面分析View的dispatchTouchEvent源代码时分析过了。
返回了false,那么看ViewGroup的源代码。81行, final View target = mMotionTarget; 因为51行的if (child.dispatchTouchEvent(ev))返回false,所以没有对mMotionTarget进行赋值。 mMotionTarget == null。
所以走到85行:if (target == null) //target = mMotionTarget ,所以该if条件成立。走到93行:return super.dispatchTouchEvent(ev);
运行第9行的super.dispatchTouchEvent(ev)。viewgroup的Super是View,即运行View的dispatchTouchEvent方法。因为我们在Activity中47行设置了ontouch事件。所以先运行Activity 中 mLinearLayout.setOnTouchListener中的onTouch, onTouch返回false ,接着运行 MyLinearLayout 中的onTouchEvent。
说明:
本来因为MyLinearLayout 是继承自LinearLayout。默认和textview一样是没有点击(clickable)或长按(longclickable)的能力的。可是,我们在Activity的38行对他设置了点击事件。mLinearLayout.setOnClickListener,所以MyLinearLayout 获得 点击的能力。所以MyLinearLayout的onTouchEvent返回true,然后运行MyLinearLayout的onTouchEvent的ACTION_UP。而点击事件就是在ACTION_UP中运行的(说明1)。
全部触发了mLinearLayout.setOnClickListener点击事件。log 例如以下:
有什么问题大家能够交流下:
总结:
1.Touch事件是从顶层的View一直往下分发到手指按下的最里面的View,假设这个View的onTouchEvent()返回false,即不消费Touch事件,这个Touch事件就会向上找父布局调用其父布局的onTouchEvent()处理。假设这个View返回true,表示消费了Touch事件,就不调用父布局的onTouchEvent()。2.一个clickable或者longClickable的View会永远消费Touch事件,无论他是enabled还是disabled的。 3.View的长按事件是在ACTION_DOWN中运行,要想运行长按事件该View必须是longClickable的,假设设置的长按事件中返回true,那么clickable事件不会触发。而且不能产生ACTION_MOVE。 4.View的点击事件是在ACTION_UP中运行。想要运行点击事件的前提是消费了ACTION_DOWN和ACTION_MOVE,而且是在没有设置OnLongClickListener的情况下,如设置了OnLongClickListener的情况。则必须使onLongClick()返回false。 5.假设View设置了onTouchListener了,而且onTouch()方法返回true,则不运行View的onTouchEvent()方法,也表示View消费了Touch事件。返回false则继续运行onTouchEvent()。
6.Touch事件是从最顶层的View一直分发到手指touch的最里层的View,假设最里层View消费了ACTION_DOWN事件(设置onTouchListener。而且onTouch()返回true 或者onTouchEvent()方法返回true)才会触发ACTION_MOVE,ACTION_UP的发生,假设某个ViewGroup拦截了Touch事件。则Touch事件交给ViewGroup处理。