作者:lenomon 发布:2011-11-25 13:54 分类:Android

Android中提供了ViewGroup、View、Activity三个等级的Touch事件处理。也就是说,这三个地方都有事件回调方法。

测试DEMO视图结构:

 1 <com .orgcent.eventtest.EventLinearLayout
2 xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:background="#032d3d"
6 android:orientation="vertical" >
7 <com .orgcent.eventtest.EventTextView
8 android:id="@+id/tvEvent"
9 android:layout_width="fill_parent"
10 android:layout_height="100dp"
11 android:gravity="center"
12 android:textColor="@android:color/black"
13 android:background="@android:color/white"
14 android:text="Activity - ViewGroup - View Event http://orgcent.com dispatch Test"/>
15 </com>
Nov 25 2011

Android事件传递机制【Touch事件】

作者:lenomon 发布:2011-11-25 13:54 分类: Android 阅读: 抢沙发

Android中提供了ViewGroup、View、Activity三个等级的Touch事件处理。也就是说,这三个地方都有事件回调方法。

测试DEMO视图结构:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<com .orgcent.eventtest.EventLinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#032d3d"
android:orientation="vertical" >
<com .orgcent.eventtest.EventTextView
android:id="@+id/tvEvent"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:gravity="center"
android:textColor="@android:color/black"
android:background="@android:color/white"
android:text="Activity - ViewGroup - View Event http://orgcent.com dispatch Test"/>
</com>

至于三者之间的执行顺序,将在下面详细阐述:

整体上看,事件传递顺序为ViewGroup::onInterceptTouchEvent() –> ViewGroup或View的onTouchEvent() –> Activity::onTouchEvent()

由于上面每个事件回调方法的返回值不同,导致事件本身及顺序发生微妙变化。下面以返回值为主线来详细阐述:

需要注意以下两点:
1、onInterceptTouchEvent()返回true,那么这个方法只会拦截动作ACTION_DOWN。
2、onInterceptTouchEvent()负责事件分发(事件传递方向),onTouchEvent()负责事件处理(消费)。

1、ViewGroup的onInterceptTouchEvent()
返回false:
默认实现方式。事件(按下、移动、抬起等)将直接传递给目标view(用户触摸的view)。
在ViewGroup触发,调用ViewGroup::onTouchEvent(),在View触发,调用View::onTouchEvent()。


PS:这里发现ViewGroup::onTouchEvent()也被调用了,原因是View::onTouchEvent()没有处理该事件(返回false),事件将交给父容器处理。

返回true:
表示ViewGroup将拦截子View的Touch事件。事件会直接传递到ViewGroup::onTouchEvent()处理。
也就是说,事件后面的移动、抬起动作不会经过onInterceptTouchEvent(),而是直接传到onTouchEvent()。

2、ViewGroup/View的onTouchEvent()
返回true:
表示事件按下动作被处理,意味着事件的移动、抬起等后续动作将会传到此方法。
如果是View处理,那么ViewGroup的onTouchEvent()将不会获得该事件。


PS:只要onInterceptTouchEvent()返回false,而且目标控件View::onTouchEvent()返回true,那么事件的每一个动作(按下、移动、抬起等)会都会首先传递到onInterceptTouchEvent()中。

如果是ViewGroup处理,那么Activity不会获得事件。

返回false:
表示View或ViewGroup不处理事件,系统将把事件传递给其父级处理。

如果View返回false,那么将由其父容器ViewGroup处理。如果ViewGroup不处理,最终将交给Activity来处理。

如果ViewGroup返回false,将交给最后一级Activity来处理。

3、Activity的onTouchEvent()
这个方法是事件最后被处理的地方。如果不处理,系统将抛弃这个事件。暂时没有发现这个方法的返回值对程序有什么意义。也许返回true能告诉系统事件被处理了。

小提示:
不要直接在程序中调用事件的回调方法。可以使用dispatchTouchEvent(MotionEvent)来分发事件。


DEMO:android_event_test.zip (56.8 KB)

转载注明本文地址: http://orgcent.com/android-touch-event-mechanism/

本文固定链接: http://orgcent.com/android-touch-event-mechanism/ | 萝卜白菜的博客

更多相关文章

  1. android 获得焦点(View get focus)
  2. Android中ExpandableListView的使用
  3. Android(安卓)SDK安装失败处理办法
  4. 面试题八:事件分发机制
  5. android SIM卡状态
  6. Android(安卓)Intent和按钮响应事件的几种方式
  7. Android(安卓)图像处理(类型转换,比例缩放,倒影,圆角)
  8. Android内容提供者源码
  9. Android实验法分析Touch事件传递

随机推荐

  1. Android(安卓)如何自定义共享库
  2. 写给Android(安卓)App开发人员看的Androi
  3. PPT:解析 Android(安卓)用户的内容消费行
  4. 树莓派遇见Android(安卓)Things(一):Raspber
  5. 刚进入Android终端即可使用busybox的命令
  6. Android(安卓)中的微型云
  7. Android使用第三方SDK——百度地图
  8. android 面试题三
  9. Android(安卓)7.1.1系统源码下载、编译、
  10. windows 下adb不能找到Android设备