您的位置:首页 > 移动开发 > Android开发

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

2012-03-22 12:50 190 查看
Android中提供了ViewGroup、View、Activity三个等级的Touch事件处理。也就是说,这三个地方都有事件回调方法。

测试DEMO视图结构:

<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, 41 次)

转载注明本文地址: http://orgcent.com/android-touch-event-mechanism/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: