android事件分发机制

来源:互联网 发布:文华期货仿真交易软件 编辑:程序博客网 时间:2024/06/17 17:40

View分发机制:

触摸任何一个控件,如Button→调用dispatchTouchEvent→判断touchlistener是否注册、判断当前控件是否enable、判断注册touch事件的ontouch回调方法返回值,如果同时为true,则不执行ontouchevent(onclick也不会执行),如果任意一个为false→调用ontouchevent→在actionup里判断是否注册onclicklistener,如果不为空则执行onclick(进入ontouchevent事件的action判断后,每次都会返回true,所以actiondown、actionup、actionmove能得到执行,才可能会出现判断是否有onclick事件)

注:imageview与button的不同,imageview默认是不能点击的,所以imageview注册了ontouch回调事件后,再执行ontouchevent事件时不会进入到action的代码,另触摸任何一个控件时,都会先从控件中找是否有dispatchTouchEvent事件,如没有就会向父类找,直到View父类

 

ViewGroup分发机制:

触摸任何一个控件,如Button→先调用layout的dispatchTouchEvent方法,如果没有向上父类找,直到viewgroup→调用ontouchevent→onintercepttouchevent判断,如果返回false,则继续判断child,否则屏蔽一切子控件的触摸事件→判断点击子view的dispatchtouchevent事件→子View的dispatchtouchevent事件返回 true(子View Button因为默认是可点击的),则layout的touch 事件给拦截掉

注:如点击layout的空白区域呢,首先在判断子view的dispatchtouchevent事件里就不会返回true(原因是子View里的ontouchevent事件由于没有被点击,不会进入action代码),所以会调用父类View的dispatchTouchevent事件,处理父类View的touch事件

0 0