当前位置: 移动技术网 > IT编程>移动开发>Android > 详解Android事件的分发、拦截和执行

详解Android事件的分发、拦截和执行

2019年07月24日  | 移动技术网IT编程  | 我要评论

tokyo hot n0250,都市极品风水师下载,艾捷克

在平常的开发中,我们经常会遇到点击,滑动之类的事件。有时候不同的view之间也存在各种滑动冲突。比如布局的内外两层都能滑动的话,那么就会出现冲突了。这个时候我们就需要了解android的事件分发机制。
android的触摸事件分发过程由三个很重要的方法来共同完成:dispatchtouchevent、onintercepttouchevent、ontouchevent。我先将这三个方法大体的介绍一下。

 •public boolean dispatchtouchevent(motionevent ev) 

用来进行事件的分发。如果事件能够传递给当前view,那么此方法一定会被调用,返回结果受当前view的ontouchevent和下级view的dispatchtouchevent方法的影响,表示是否消耗当前事件。action_down的dispatchtouchevent()返回true,后续事件(action_move、action_up)会再传递,如果返回false,dispatchtouchevent()就接收不到action_up、action_move。简单的说,就是当dispatchtouchevent在进行事件分发的时候,只有前一个action返回true,才会触发后一个action。

 •public boolean onintercepttouchevent(motionevent event) 

这个方法是在dispatchtouchevent方法中调用的,用来拦截某个事件的。如果当前view拦截了某个事件,那么在同一个事件序列中,此方法不会被再次调用,返回的结果表示是否拦截当前事件。它是viewgroup提供的方法,默认返回false。

 •public boolean ontouchevent(motionevent event) 

在dispatchtouchevent方法中调用,用来处理点击事件,返回结果表示是否消耗掉当前事件(true表示消耗,false表示不消耗),如果不消耗,则在同一个事件序列中,当前view无法再次接收到事件。view和viewgroup都有该方法,view默认返回true,表示消费了这个事件。

view里,有两个回调函数 :

public boolean dispatchtouchevent(motionevent ev);   
public boolean ontouchevent(motionevent ev);

viewgroup里,有三个回调函数 :

public boolean dispatchtouchevent(motionevent ev);   
public boolean onintercepttouchevent(motionevent ev);   
public boolean ontouchevent(motionevent ev);

上述三个方法中有什么区别和关系呢?下面用一段伪代码表示:

public boolean dispatchtouchevent(motionevent ev) { 
 boolean consume = false; 
 if(onintercepttouchevent(ev)){ 
  consume = ontouchevent(ev); 
 } else { 
  consume = child.dispatchtouchevent(ev); 
 } 
 return consume; 
} 

 通过上面的伪代码大家可能对点击事件的传递规则有了更清楚的认识,即:对于一个根viewgroup来说,点击事件产生后,首先会传递给它,这时它的dispatchtouchevent就会被调用,如果这个viewgroup的onintercepttouchevent方法返回true表示它要拦截此事件,接着这个事件就会交给这个viewgroup处理,即它的ontouchevent方法就会被调用;如果这个viewgroup的onintercepttouchevent方法返回false,就表示它不拦截此事件,这是当前事件就会继续传递给它的子元素,接着子元素的dispatchtouchevent方法就会被调用,如此反复直到事件被最终处理。

下面的几张图参考自[eoe]:

 •图一:action_down都没被消费

 

•图二(一):action_down被view消费了


•图二(二):后续action_move和up在不被拦截的情况下都会去找view


•图三:后续的被拦截了


•图四:action_down一开始就被拦截

view事件分发源码分析:
 •dispatchtouchevent方法: 

public boolean dispatchtouchevent(motionevent event) { 
 if (montouchlistener != null && (mviewflags & enabled_mask) == enabled && montouchlistener.ontouch(this, event)) { 
  return true; 
 } 
 return ontouchevent(event); 
}

 如果montouchlistener != null,(mviewflags&enabled_mask)==enabled和montouchlistener.ontouch(this, event)这三个条件都为真,就返回true,否则就去执行ontouchevent(event)方法并返回。

总结下来ontouch能够得到执行需要两个前提条件(都满足):
 1.设置了ontouchlistener
 2.控件是enable状态

 而ontouchevent能够得到执行满足以下三个条件任意一个即可:
 1.没有设置ontouchlistener
 2.控件不是enable状态
 3.ontouch返回false

 再来看一下dispatchtouchevent的返回值,它其实受ontouch和ontouchevent函数的返回值控制,也就是说touch事件被成功消费返回true,它也就返回true,说明分发成功,此后的事件序列也会在此被分发,而如果返回false,则认为分发失败,此后的事件序列就不再分发下去了。
 •ontouchevent方法:

 if (((viewflags & clickable) == clickable || (viewflags & long_clickable) == long_clickable)) {
  ...
  return true;
 }

view的ontouchevent默认都会消耗掉事件(该方法返回true),除非它是不可点击的(clickable和longclickable同时为false)。并且view的longclickable默认为false,clickable属性要分情况,比如button默认为true,textview、imageview默认为false。

public boolean performclick() { 
 sendaccessibilityevent(accessibilityevent.type_view_clicked); 
 if (monclicklistener != null) { 
  playsoundeffect(soundeffectconstants.click); 
  monclicklistener.onclick(this); 
  return true; 
 } 
 return false; 
}

 这不就是我们熟悉的onclicklistener吗,它原来是在ontouchevent中被调用的。只要monclicklistener不是null,就会去调用它的onclick方法。

总结下来onclick能够得到执行需要两个前提条件(都满足):
 1.可以执行到ontouchevent
 2.设置了onclicklistener

 整个view的事件转发流程是:
dispatchevent->setontouchlistener->ontouchevent->setonclicklistener

最后还有一个问题,setonlongclicklistener和setonclicklistener是否只能执行一个?
答:不是的,只要setonlongclicklistener中的onclick返回false,则两个都会执行;返回true则会屏蔽setonclicklistener。

viewgroup事件分发源码分析:
 •dispatchtouchevent方法:

 ...
   if (disallowintercept || !onintercepttouchevent(ev)) { 
    ev.setaction(motionevent.action_down); 
    final int scrolledxint = (int) scrolledxfloat; 
    final int scrolledyint = (int) scrolledyfloat; 
    final view[] children = mchildren; 
    final int count = mchildrencount; 

    for (int i = count - 1; i >= 0; i--) { 
     final view child = children[i]; 
     if ((child.mviewflags & visibility_mask) == visible 
       || child.getanimation() != null) { 
      child.gethitrect(frame); 
      if (frame.contains(scrolledxint, scrolledyint)) { 
       final float xc = scrolledxfloat - child.mleft; 
       final float yc = scrolledyfloat - child.mtop; 
       ev.setlocation(xc, yc); 
       child.mprivateflags &= ~cancel_next_up_event; 
       if (child.dispatchtouchevent(ev)) { 
        // event handled, we have a target now. 
        mmotiontarget = child; 
        return true; 
       } 
      } 
     } 
    } 
   }

 两种可能会进入if代码段(即事件被分发给子view):
1、当前不允许拦截,即disallowintercept = true.
2、当前没有拦截,即onintercepttouchevent(ev)返回false.

 注:disallowintercept是指是否禁用掉事件拦截的功能,默认是false,可以通过viewgroup.requestdisallowintercepttouchevent(boolean)进行设置;而onintercepttouchevent(ev)可以进行复写。

进入if代码段后,通过一个for循环,遍历当前viewgroup下的所有子view,判断当前遍历的view是不是正在点击的view,如果是的话就会调用该view的dispatchtouchevent,就进入了view的事件分发流程了,上面有讲。当child.dispatchtouchevent(ev)返回true,则为mmotiontarget=child;然后return true,说明viewgroup的dispatchtouchevent返回值受childview的dispatchtouchevent返回值影响,子view事件分发成功,viewgroup的事件分发才成功,此后的事件序列也会在此分发(从上面知:子view的clickable或longclickable为true都能分发成功),而如果viewgroup事件分发失败或者没有找到子view(点击空白位置),则会走到它的ontouchevent,以后的事件序列也不会分发下去,直接走ontouchevent。

整个viewgroup的事件转发流程是:
dispatchevent->onintercepttouchevent->child.dispatchevent->(setontouchlistener->ontouchevent)

上面的总结都是基于:如果没有拦截;那么如何拦截呢?
 •onintercepttouchevent

 public boolean onintercepttouchevent(motionevent ev) { 
 return false; 
}

 代码很简单,只有一句,即返回false,viewgroup默认是不拦截的。如果你需要拦截,只要return true就行了,这样该事件就不会往子view传递了,并且如果你在down return true ,则down,move,up子view都不会捕获到事件;如果你在move return true , 则子view在move和up都不会捕获到事件。

如何不被拦截:
如果viewgroup的onintercepttouchevent(ev) 当action_move时return true ,即拦截了子view的move以及up事件;此时子view希望依然能够响应move和up时该咋办呢?
答:onintercepttouchevent是定义在viewgroup中的,子view无法修改。android给我们提供了一个方法:requestdisallowintercepttouchevent(boolean) 用于设置是否允许拦截,我们在子view的dispatchtouchevent中直接这么写:

 @override 
  public boolean dispatchtouchevent(motionevent event) 
  { 
   getparent().requestdisallowintercepttouchevent(true); 
   int action = event.getaction();  
   switch (action) { 
   case motionevent.action_down: 
    log.e(tag, "dispatchtouchevent action_down"); 
    break; 
   case motionevent.action_move: 
    log.e(tag, "dispatchtouchevent action_move"); 
    break; 
   case motionevent.action_up: 
    log.e(tag, "dispatchtouchevent action_up"); 
    break;  
   default: 
    break; 
   } 
   return super.dispatchtouchevent(event); 
  } 

 getparent().requestdisallowintercepttouchevent(true); 这样即使viewgroup在move的时候return true,子view依然可以捕获到move以及up事件。
注:如果viewgroup在onintercepttouchevent(ev) action_down里面直接return true了,那么子view是没有办法的捕获事件的!

总结
关于代码流程上面已经总结过了~
1、如果viewgroup找到了能够处理该事件的view,则直接交给子view处理,自己的ontouchevent不会被触发;
2、可以通过复写onintercepttouchevent(ev)方法,拦截子view的事件(即return true),把事件交给自己处理,则会执行自己对应的ontouchevent方法
3、子view可以通过调用getparent().requestdisallowintercepttouchevent(true); 阻止viewgroup对其move或者up事件进行拦截;
好了,那么实际应用中能解决哪些问题呢?
比如你在scrollview中嵌套了一个edittext,当edittext中文字内容太多超出范围时,你想上下滑动使edittext中文字滚动出来,却发现滚动的是scrollview。这时我们设置edittext的ontouch事件,在ontouch中设置不让scrollview拦截我的事件,最后在up时把状态改回去。

@override
public boolean ontouch(view view, motionevent motionevent) {
  if ((view.getid() == r.id.tousucontentedittext && canverticalscroll(tousucontentedittext))) {
   view.getparent().requestdisallowintercepttouchevent(true);
   if (motionevent.getaction() == motionevent.action_up) {
    view.getparent().requestdisallowintercepttouchevent(false);
   }
  }
  return false;
 }

private boolean canverticalscroll(edittext edittext) {
  int scrolly = edittext.getscrolly();
  int scrollrange = edittext.getlayout().getheight();
  int scrollextent = edittext.getheight() - edittext.getcompoundpaddingtop() - edittext.getcompoundpaddingbottom();
  int scrolldifference = scrollrange - scrollextent;
  if (scrolldifference == 0) {
   return false;
  }
  return (scrolly > 0) || (scrolly < scrolldifference - 1);
 }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网