首页 > 学院 > 开发设计 > 正文

事件传递

2019-11-09 18:50:17
字体:
来源:转载
供稿:网友
//ViewGroup的事件处理 @Override public boolean dispatchTouchEvent(MotionEvent event) { boolean consume = false; if (onInterceptTouchEvent(event)) {//ViewGroup拦截(viewgroup默认不拦截) consume = onTouchEvent(event);//则ViewGroup处理事件 } else {//ViewGroup不拦截 consume = child.dispatchTouchEvent(event);//则子view派发事件 } return consume; }

事件的传递:隧道式派发

先执行dispatchTouchEvent方法 再执行onInterceptTouchEvent方法(view无此方法) true,拦截,不用派发给下级 false,不拦截,派发给下级

事件的处理:冒泡式消费

只执行onTouchEvent方法(view没有拦截方法,事件传递过来时一定会执行这个方法,当clickable和longclickable都为true时如button默认消费事件) true,处理了,不用处理了 false,没处理,给上级处理

dispatchTouchEvent方法:事件传递到当前view,此方法一定会被调用,返回值受当前view的onTouchEvent和下级view的dispatchTouchEvent返回值影响。

最后一级view一定会调用onTouchEvent方法。

例子如下:

Markdown

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>

点击ViewGroupA

02-06 11:48:55.295 10298-10298/? E/TouchEvent: ViewGroupA==dispatchTouchEvent==false==0==148635293529502-06 11:48:55.295 10298-10298/? E/TouchEvent: ViewGroupA==onInterceptTouchEvent==false==0==148635293529502-06 11:48:55.295 10298-10298/? E/TouchEvent: ViewGroupA==onTouchEvent==false==0==1486352935295

点击ViewGroupB

02-06 11:47:40.835 10298-10298/? E/TouchEvent: ViewGroupA==dispatchTouchEvent==false==0==148635286083402-06 11:47:40.835 10298-10298/? E/TouchEvent: ViewGroupA==onInterceptTouchEvent==false==0==148635286083502-06 11:47:40.835 10298-10298/? E/TouchEvent: ViewGroupB==dispatchTouchEvent==false==0==148635286083502-06 11:47:40.835 10298-10298/? E/TouchEvent: ViewGroupB==onInterceptTouchEvent==false==0==148635286083502-06 11:47:40.835 10298-10298/? E/TouchEvent: ViewGroupB==onTouchEvent==false==0==148635286083502-06 11:47:40.835 10298-10298/? E/TouchEvent: ViewGroupA==onTouchEvent==false==0==1486352860835

点击MyView

02-06 11:45:01.557 10298-10298/? E/TouchEvent: ViewGroupA==dispatchTouchEvent==false==0==148635270155602-06 11:45:01.557 10298-10298/? E/TouchEvent: ViewGroupA==onInterceptTouchEvent==false==0==148635270155702-06 11:45:01.557 10298-10298/? E/TouchEvent: ViewGroupB==dispatchTouchEvent==false==0==148635270155702-06 11:45:01.557 10298-10298/? E/TouchEvent: ViewGroupB==onInterceptTouchEvent==false==0==148635270155702-06 11:45:01.557 10298-10298/? E/TouchEvent:MyView==dispatchTouchEvent==false==0==148635270155702-06 11:45:01.557 10298-10298/? E/TouchEvent: MyView==onTouchEvent==false==0==148635270155702-06 11:45:01.562 10298-10298/? E/TouchEvent:ViewGroupB==onTouchEvent==false==0==148635270156202-06 11:45:01.562 10298-10298/? E/TouchEvent: ViewGroupA==onTouchEvent==false==0==1486352701562

参考:

Android艺术探索 Android群英传


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表