美文网首页
Android View 的事件拦截机制

Android View 的事件拦截机制

作者: 小柏不是大白 | 来源:发表于2017-04-20 10:45 被阅读37次

场景:

View A 放在viewGroupB中,viewGroupB又放在viewGroup C中。

对于viewGroup中,总共有三个方法:dispatchTouchEvent(MotionEvent e);onInterceptTouchEvent(MotionEvent e);onTouchEvent(MotionEvent e)。

对于view中,只有两个方法:dispatchTouchEvent(MotionEvent e);;onTouchEvent(MotionEvent e);

当点击事件触发时,事件的传递顺序为:Activity--------Window------顶级view

,顶级view就按照事件分发机制:C------B-------A;

事件的处理顺序为:A------B------C;

事件传递的返回值:true :表示拦截,不继续; false :表示不拦截,继续流程;

事件的处理机制为:true :表示处理了,不用审核了,false:表示交给上级处理;

可用伪代码表示:

public boolean  dispatchTouchEvent(MotionEvent e){

       boolean consume=false;

      if(onInterceptTouchevent()){

               consume=onTouchEvent();     }

   else{

         consume=child.dispatchTouchevent(ev);         

}

     return consume;

}

相关文章

网友评论

      本文标题:Android View 的事件拦截机制

      本文链接:https://www.haomeiwen.com/subject/crwsjttx.html