先执行dispatchTouchEvent方法 再执行onInterceptTouchEvent方法(view无此方法) true,拦截,不用派发给下级 false,不拦截,派发给下级
只执行onTouchEvent方法(view没有拦截方法,事件传递过来时一定会执行这个方法,当clickable和longclickable都为true时如button默认消费事件) true,处理了,不用处理了 false,没处理,给上级处理
dispatchTouchEvent方法:事件传递到当前view,此方法一定会被调用,返回值受当前view的onTouchEvent和下级view的dispatchTouchEvent返回值影响。
最后一级view一定会调用onTouchEvent方法。
MyView
public class MyView extends View { //处理事件 @Override public boolean onTouchEvent(MotionEvent event) { Log.e("TouchEvent","MyView==onTouchEvent=="+super.onTouchEvent(event)+"==" +event.getAction()+"=="+System.currentTimeMillis()); return super.onTouchEvent(event); } //派发事件 @Override public boolean dispatchTouchEvent(MotionEvent ev) { Log.e("TouchEvent","MyView==dispatchTouchEvent=="+super.onTouchEvent(ev)+"==" +ev.getAction()+"=="+System.currentTimeMillis()); return super.dispatchTouchEvent(ev); } public MyView(Context context) { super(context); } public MyView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public MyView(Context context, AttributeSet attrs) { super(context, attrs); }}ViewGroupA
public class ViewGroupA extends LinearLayout { //处理事件 @Override public boolean onTouchEvent(MotionEvent event) { Log.e("TouchEvent","ViewGroupA==onTouchEvent=="+super.onTouchEvent(event)+"==" +event.getAction()+"=="+System.currentTimeMillis()); return super.onTouchEvent(event); } //派发事件 @Override public boolean dispatchTouchEvent(MotionEvent ev) { Log.e("TouchEvent","ViewGroupA==dispatchTouchEvent=="+super.onTouchEvent(ev)+"==" +ev.getAction()+"=="+System.currentTimeMillis()); return super.dispatchTouchEvent(ev); } //拦截事件 @Override public boolean onInterceptTouchEvent(MotionEvent ev) { Log.e("TouchEvent","ViewGroupA==onInterceptTouchEvent=="+super.onTouchEvent(ev)+"==" +ev.getAction()+"=="+System.currentTimeMillis()); return super.onInterceptTouchEvent(ev); } //构造方法 public ViewGroupA(Context context) { super(context); } public ViewGroupA(Context context, AttributeSet attrs) { super(context, attrs); } public ViewGroupA(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); }}ViewGroupB
public class ViewGroupB extends LinearLayout { //处理事件 @Override public boolean onTouchEvent(MotionEvent event) { Log.e("TouchEvent","ViewGroupB==onTouchEvent=="+super.onTouchEvent(event)+"==" +event.getAction()+"=="+System.currentTimeMillis()); return super.onTouchEvent(event); } //派发事件 @Override public boolean dispatchTouchEvent(MotionEvent ev) { Log.e("TouchEvent","ViewGroupB==dispatchTouchEvent=="+super.onTouchEvent(ev)+"==" +ev.getAction()+"=="+System.currentTimeMillis()); return super.dispatchTouchEvent(ev); } //拦截事件 @Override public boolean onInterceptTouchEvent(MotionEvent ev) { Log.e("TouchEvent","ViewGroupB==onInterceptTouchEvent=="+super.onTouchEvent(ev)+"==" +ev.getAction()+"=="+System.currentTimeMillis()); return super.onInterceptTouchEvent(ev); } public ViewGroupB(Context context) { super(context); } public ViewGroupB(Context context, AttributeSet attrs) { super(context, attrs); } public ViewGroupB(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); }}布局:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.administrator.myapplication.MainActivity"> <com.example.administrator.myapplication.ViewGroupA android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/holo_blue_bright"> <com.example.administrator.myapplication.ViewGroupB android:layout_width="300dp" android:layout_height="300dp" android:background="@android:color/holo_green_dark"> <com.example.administrator.myapplication.MyView android:layout_width="150dp" android:layout_height="150dp" android:background="@android:color/darker_gray" /> </com.example.administrator.myapplication.ViewGroupB> </com.example.administrator.myapplication.ViewGroupA></RelativeLayout>Android艺术探索 Android群英传
新闻热点
疑难解答