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

Android onTouch事件传递机制

2016-04-06 10:00 447 查看
在触发OnTouch事件的时候Android的ViewGroup会调用如下三个函数:

- public boolean dispatchTouchEvent(MotionEvent ev) //用于事件的分发

- public boolean onInterceptTouchEvent(MotionEvent ev) // 用于事件的拦截

- public boolean onTouchEvent(MotionEvent ev) //处理事件

(备注:普通View没有onInterceptTouchEvent事件)

测试布局.xml

<com.demo.touch.TestFrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.demo.touch.TestTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="嵌套层级"
android:textColor="#00ff00"/>
</com.demo.touch.TestFrameLayout>

<com.demo.touch.TestTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="平行层级"
android:textColor="#00ff00"/>


事件传递

1.嵌套层级

由外向内传 dispatchTouchEvent

再由内向外传 onTouchEvent

日志

04-05 16:06:58.459 18582-18582/com.demo.touch W/TestFrameLayout: dispatchTouchEvent...ACTION_DOWN
04-05 16:06:58.459 18582-18582/com.demo.touch W/TestFrameLayout: onInterceptTouchEvent...ACTION_DOWN
04-05 16:06:58.459 18582-18582/com.demo.touch D/TestTextView: dispatchTouchEvent...ACTION_DOWN
04-05 16:06:58.459 18582-18582/com.demo.touch D/TestTextView: onTouchEvent...ACTION_DOWN
04-05 16:06:58.459 18582-18582/com.demo.touch W/TestFrameLayout: onTouchEvent...ACTION_DOWN


2.平行层级

每层都先跑完自己的 dispatchTouchEvent和onTouchEvent

然后由上向下传 事件

日志

04-05 16:07:13.269 18582-18582/com.demo.touch D/TestTextView: dispatchTouchEvent...ACTION_DOWN
04-05 16:07:13.269 18582-18582/com.demo.touch D/TestTextView: onTouchEvent...ACTION_DOWN
04-05 16:07:13.279 18582-18582/com.demo.touch W/TestFrameLayout: dispatchTouchEvent...ACTION_DOWN
04-05 16:07:13.279 18582-18582/com.demo.touch W/TestFrameLayout: onInterceptTouchEvent...ACTION_DOWN
04-05 16:07:13.279 18582-18582/com.demo.touch W/TestFrameLayout: onTouchEvent...ACTION_DOWN


事件拦截

1.嵌套层级

TestTextView设置onClick事件

TestFrameLayout的onTouchEvent事件被拦截

日志

04-05 16:41:49.609 17810-17810/com.demo.touch W/TestFrameLayout: dispatchTouchEvent...ACTION_DOWN
04-05 16:41:49.609 17810-17810/com.demo.touch D/TestTextView: dispatchTouchEvent...ACTION_DOWN
04-05 16:41:49.609 17810-17810/com.demo.touch D/TestTextView: onTouchEvent...ACTION_DOWN
...ACTION_MOVE事件可能有多个
04-05 16:41:49.629 17810-17810/com.demo.touch W/TestFrameLayout: dispatchTouchEvent...ACTION_MOVE
04-05 16:41:49.629 17810-17810/com.demo.touch D/TestTextView: dispatchTouchEvent...ACTION_MOVE
04-05 16:41:49.629 17810-17810/com.demo.touch D/TestTextView: onTouchEvent...ACTION_MOVE
...
04-05 16:41:49.649 17810-17810/com.demo.touch W/TestFrameLayout: dispatchTouchEvent...ACTION_UP
04-05 16:41:49.649 17810-17810/com.demo.touch D/TestTextView: dispatchTouchEvent...ACTION_UP
04-05 16:41:49.649 17810-17810/com.demo.touch D/TestTextView: onTouchEvent...ACTION_UP
04-05 16:41:49.649 17810-17810/com.demo.touch D/tv_in: onClick...


2.平行层级

TestTextView设置onClick事件

TestFrameLayout的onTouchEvent和dispatchTouchEvent事件都被拦截

日志

04-05 16:45:14.309 17810-17810/com.demo.touch D/TestTextView: dispatchTouchEvent...ACTION_DOWN
04-05 16:45:14.309 17810-17810/com.demo.touch D/TestTextView: onTouchEvent...ACTION_DOWN
...ACTION_MOVE事件可能有多个
04-05 16:45:14.409 17810-17810/com.demo.touch D/TestTextView: dispatchTouchEvent...ACTION_MOVE
04-05 16:45:14.409 17810-17810/com.demo.touch D/TestTextView: onTouchEvent...ACTION_MOVE
...
04-05 16:45:14.439 17810-17810/com.demo.touch D/TestTextView: dispatchTouchEvent...ACTION_UP
04-05 16:45:14.439 17810-17810/com.demo.touch D/TestTextView: onTouchEvent...ACTION_UP
04-05 16:45:14.439 17810-17810/com.demo.touch D/tv_out: onClick...


测试demo下载

参考:/article/2242084.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: