前言:
一个touch事件序列包括:down、move、up(其中move事件会多次触发,就是说如果手指在屏幕上多次滑动的时候会多次触发move事件,可以利用这一点实现view 的移动)
viewgroup:用来进行事件分发 view:用来对事件的处理
分发流程: activity#dispatchtouchevent -> phonewindow#superdispatchtouchevent -> decorview#superdispatchtouchevent ->viewgroup#dispatchtouchevent -> view#dispatchtouchevent ->view#ontouchevent
从下往上看,先看事件如何被处理的,先看一个例子
btn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { log.e("hover","onclick"); } }); btn.setontouchlistener(new view.ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { log.e("hover", "ontouch:" + event.getaction()); //return false;//return false的话两个打印日志都有 return true;//只有ontouch日志会打印 } });
对同一个组件设置两个监听,此处有三个面试题:
- 1、ontouch的返回值有什么用
- 2、ontouch和onclick哪个先调用(还有一个ontouchevent)
- 3、在哪里调用的
带着问题看看view的dispatchtouchevent
从代码中可以看出,ontouch的优先级要高于ontouchevent,而onclick是在ontouchevent中调用的,因此onclick的优先级最低
注意以上三个方法可能都不会执行,因为三个方法都是在view的dispatchtouchevent的执行的,如果连dispatchtouchevent都不执行的话,那么三个方法就都不会执行了
什么情况下view的dispatchtouchevent会不执行呢:父容器不分发事件给view,就不会执行,即父容器不会调用子view的dispatchtouchevent方法
那什么时候父容器不会分发事件给view呢?这就需要看看事件分发的过程了: activity#dispatchtouchevent -> phonewindow#superdispatchtouchevent -> decorview#superdispatchtouchevent -> viewgroup#dispatchtouchevent
viewgroup中的dispatchtouchevent中的核心地方可以用两句伪代码来阐述(摘自android开发艺术探索):
如果,viewgroup的onintercepttouchevent方法执行了,则表示viewgroup拦截了当前事件,去执行自己的 ontouchevent逻辑,否则将事件分发给子view去执行
viewgroup的分发逻辑主要有三个部分:
第一部分:判断是否拦截该事件:
第二部分:分发事件给view,看哪个子view处理事件(源码太多了,只粘了后半部分)
注意:当move事件来的时候不会走第二部分代码!!!
第三部分:执行事件:单指操作还是多指操作
- 1、上下滑动的时候,此时viewpager被设置为不允许拦截,所以事件交给了listview,listview正常上下滑动没问题
- 2、左右滑动的时候,此时viewpager被设置为允许拦截,而在viewpager的onintercepttouchevent方法中move事件返回了true,所以拦截事件成功,viewpager会执行自己的ontouchevent,实现左右滑动
子view去执行事件逻辑:
dispatchtransformedtouchevent(ev, false, child, idbitstoassign) handled = child.dispatchtouchevent(event);
总结:
1、如果父view拦截了事件并消费了事件,则子view 的dispatchtouchevent就不会执行 2、如果父view并没有拦截事件,但是所有的子view都没有消费此事件,则最后也是执行父view的dispatchtouchevent 3、如果父view没有拦截事件,且某个子view拦截了此事件消费了,事件就不会再向下个子view传递,如果没有消费,则会继续遍历下一个子view(这段逻辑再第二部分的for循环中)
如果子view处理了就提前break
如何解决自定view 的滑动冲突呢:根据实际情况去分配事件
- 1、在子view中去处理(内部拦截法) 通常也会涉及父类的改动
- 2、在父view中去处理(外部拦截法)
以内部拦截法做一个例子:viewpager中嵌套listview
public class slideinflictfragment extends fragment { private basepager mpager; list<mylistview> mlistviews = new arraylist<>(); private string[] data={"apple","banana","orange","watermelon","pear","grape","pineapple", "strawberry","cherry","mango","apple","banana","orange","watermelon","pear","grape", "pineapple","strawberry","cherry","mango"}; @nullable @override public view oncreateview(@nonnull layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) { return inflater.inflate(r.layout.slide_inflict_view_layout, container, false); } @override public void oncreate(@nullable bundle savedinstancestate) { super.oncreate(savedinstancestate); } @override public void onviewcreated(@nonnull view view, @nullable bundle savedinstancestate) { super.onviewcreated(view, savedinstancestate); mpager = view.findviewbyid(r.id.viewpager); initlistviews(); mpager.setadapter(new mypageradapter(mlistviews)); } private void initlistviews(){ mylistview l1 = new mylistview(getcontext()); mylistview l2 = new mylistview(getcontext()); mylistview l3 = new mylistview(getcontext()); arrayadapter<string> adapter = new arrayadapter<>(getactivity(),r.layout.slide_inflict_list_item,data); l1.setadapter(adapter);l2.setadapter(adapter);l3.setadapter(adapter); mlistviews.add(l1);mlistviews.add(l2);mlistviews.add(l3); } public class mypageradapter extends pageradapter{ public list<mylistview> mlistviews; public mypageradapter(list<mylistview> mlistviews) { this.mlistviews = mlistviews; } @override public int getcount() { return mlistviews.size(); } @override public boolean isviewfromobject(@nonnull view view, @nonnull object object) { return view == object; } @nonnull @override public object instantiateitem(@nonnull viewgroup container, int position) { container.addview(mlistviews.get(position)); return mlistviews.get(position); } @override public void destroyitem(@nonnull viewgroup container, int position, @nonnull object object) { container.removeview(mlistviews.get(position)); } } }
public class mylistview extends listview { public mylistview(context context) { super(context); } public mylistview(context context, attributeset attrs) { super(context, attrs); } public mylistview(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); } private int mlastx,mlasty; @override public boolean dispatchtouchevent(motionevent event) { int x = (int) event.getx(); int y = (int) event.gety(); switch (event.getaction()){ case motionevent.action_down: getparent().requestdisallowintercepttouchevent(true);//表示父容器不能拦截此事件 break; case motionevent.action_move: int deltax = x-mlastx; int deltay = x-mlasty; if(math.abs(deltax)>math.abs(deltay)){ getparent().requestdisallowintercepttouchevent(false);//表示可以拦截 } break; default: break; } mlastx = x; mlasty = y; return super.dispatchtouchevent(event); } }
public class mypager extends viewpager { public mypager(@nonnull context context) { super(context); } public mypager(@nonnull context context, @nullable attributeset attrs) { super(context, attrs); } @override public boolean onintercepttouchevent(motionevent ev) { if (ev.getaction() == motionevent.action_down) { super.onintercepttouchevent(ev); return false;//必须要在down事件return false,否则listview就接收不到down事件,也就无法处理touchevent } return true; } }
原理解释:
首先down事件传递给mypager, down事件来的时候viewgroup会重置标志位,而且onintercepttouchevent方法一定会执行,所以这里一定要返回false,listview才会收到down事件,否则listview是否发下拉的
按照上述代码,此后viewgroup应该会执行第二块代码块去分发事件,即listview去处理事件,在listview中的down事件调用getparent().requestdisallowintercepttouchevent(true)方法,会改变viewgroup中mgroupflags标志位,进而影响viewpager中对后续事件的拦截回调的执行与否
当move事件到来的时候,由于listview在down事件的时候设置了不拦截事件,则viewpager也不会拦截move事件,所以此事件落到listview去处理,在listview中根据手指滑动情况去设置viewpager是否拦截move事件:
- 1、上下滑动的时候,此时viewpager被设置为不允许拦截,所以事件交给了listview,listview正常上下滑动没问题
- 2、左右滑动的时候,此时viewpager被设置为允许拦截,而在viewpager的onintercepttouchevent方法中move事件返回了true,所以拦截事件成功,viewpager会执行自己的ontouchevent,实现左右滑动
到此这篇关于view事件分发原理和viewpager+listview嵌套滑动冲突的文章就介绍到这了,更多相关view的事件分发 内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!