1,传递机制主要原理

 onTouchevent(处理事件) ,dispatchTouchEvent(分发),onInterceptTouchEvent(拦截事件)

 

2,实现效果

【1】正常状态孩子不消费事件,会返回给父容器。

MyViewGroup, dispatchTouchEvent(ture),onInterceptTouchEvent(false), onTouchevent(ture)

MyView   dispatchTouchEvent(ture), onTouchevent(false)

父容器先获取事件,不拦截,分发给孩子,孩子接收到事件.孩子也不消费事件.把事件又返回给父容器消费,默认父容器也不消费

Android View的事件传递机制

【2】孩子消费事件,传递结束

MyViewGroup, dispatchTouchEvent(ture),onInterceptTouchEvent(false), onTouchevent(ture)

MyView   dispatchTouchEvent(ture), onTouchevent(ture)

父容器先获取事件.不拦截.分发给孩子.孩子接收到事件.让孩子消费事件.如果事件消费了,事件传递结束

Android View的事件传递机制

 

【3】父容器先获取事件.拦截.事件不会分发给孩子.让父容器自己消费

MyViewGroup, dispatchTouchEvent(ture),onInterceptTouchEvent(ture), onTouchevent(ture)

MyView   dispatchTouchEvent(ture), onTouchevent(ture)

 

 

Android View的事件传递机制

 

【4】父容器先获取事件.点击绿色区域.不会把事件分发给孩子.父容器拦截

MyViewGroup, dispatchTouchEvent(ture),onInterceptTouchEvent(ture), onTouchevent(ture)

MyView   dispatchTouchEvent(ture), onTouchevent(ture)

Android View的事件传递机制

 

【5】父容器先获取事件.点击绿色区域.不会把事件分发给孩子.父容器不拦截

MyViewGroup, dispatchTouchEvent(ture),onInterceptTouchEvent(true), onTouchevent(ture)

MyView   dispatchTouchEvent(ture), onTouchevent(ture)Android View的事件传递机制

 

【6】

相关文章:

  • 2021-06-03
  • 2021-04-06
  • 2021-05-20
猜你喜欢
  • 2022-01-20
  • 2022-12-23
  • 2021-05-07
  • 2021-05-28
  • 2021-11-09
  • 2021-07-28
  • 2021-06-01
相关资源
相似解决方案