Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[问答]Android中touch事件的传递机制是怎样的? #30

Open
chriszeng87 opened this issue Mar 11, 2015 · 5 comments
Open

[问答]Android中touch事件的传递机制是怎样的? #30

chriszeng87 opened this issue Mar 11, 2015 · 5 comments

Comments

@chriszeng87
Copy link

No description provided.

@Rowandjj
Copy link

1.Touch事件传递的相关API有dispatchTouchEvent、onTouchEvent、onInterceptTouchEvent
2.Touch事件相关的类有View、ViewGroup、Activity
3.Touch事件会被封装成MotionEvent对象,该对象封装了手势按下、移动、松开等动作
4.Touch事件通常从Activity#dispatchTouchEvent发出,只要没有被消费,会一直往下传递,到最底层的View。
5.如果Touch事件传递到的每个View都不消费事件,那么Touch事件会反向向上传递,最终交由Activity#onTouchEvent处理.
6.onInterceptTouchEvent为ViewGroup特有,可以拦截事件.
7.Down事件到来时,如果一个View没有消费该事件,那么后续的MOVE/UP事件都不会再给它

更多内容看这里:http://wugengxin.cn/download/pdf/android/PRE_andevcon_mastering-the-android-touch-system.pdf

@mthli
Copy link

mthli commented Mar 28, 2015

@goceancx
Copy link

http://v.youku.com/v_show/id_XODQ1MjI2MDQ0.html?f=23088492 2楼发的PDF的原视频,中英字幕

@FrancisShi
Copy link

1、基础知识
(1) 所有Touch事件都被封装成了MotionEvent对象,包括Touch的位置、时间、历史记录以及第几个手指(多指触摸)等。

(2) 事件类型分为ACTION_DOWN, ACTION_UP, ACTION_MOVE, ACTION_POINTER_DOWN, ACTION_POINTER_UP, ACTION_CANCEL,每个事件都是以ACTION_DOWN开始ACTION_UP结束。

(3) 对事件的处理包括三类,分别为传递——dispatchTouchEvent()函数、拦截——onInterceptTouchEvent()函数、消费——onTouchEvent()函数和OnTouchListener

2、传递流程
(1) 事件从Activity.dispatchTouchEvent()开始传递,只要没有被停止或拦截,从最上层的View(ViewGroup)开始一直往下(子View)传递。子View可以通过onTouchEvent()对事件进行处理。

(2) 事件由父View(ViewGroup)传递给子View,ViewGroup可以通过onInterceptTouchEvent()对事件做拦截,停止其往下传递。

(3) 如果事件从上往下传递过程中一直没有被停止,且最底层子View没有消费事件,事件会反向往上传递,这时父View(ViewGroup)可以进行消费,如果还是没有被消费的话,最后会到Activity的onTouchEvent()函数。

(4) 如果View没有对ACTION_DOWN进行消费,之后的其他事件不会传递过来。

(5) OnTouchListener优先于onTouchEvent()对事件进行消费。
上面的消费即表示相应函数返回值为true。

@tsunhua
Copy link

tsunhua commented Jan 30, 2016

谁知道,Android事件分发为什么要设计成从根View到子View,而不是从子View到根View?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants