Android 触摸事件监听(Activity层,ViewGroup层,View层)详细介绍
Android不同层次的触摸事件监听
APP开发中,经常会遇到有关手势处理的操作,比如向右滑动返回上一个页面。关于触摸事件的处理,我们可以大概处理在不同的层次上。
Activity层:可以看做触摸事件获取的最顶层
ViewGroup层:ViewGroup层可以自主控制是否让子View获取触摸事件
View层:可以决定自己是否真正的消费触摸事件,如果不消费抛给上层ViewGroup
Activity级别的手势监听:(右滑动返回上层界面)
Activity层手势监听的使用场景:一般用于当前页面中没有过多的手势需要处理的时候,至多存在点击事件。对于右滑返回上层界面这种需求,可以将其定义在一个BaseActivity中,子Activity如果需要实现,通过某个开关打开即可。
注意事项:
1、Activity层,用dispatch可以抓取所有的事件。
2、对于滑动,要设定一个距离阈值mDistanceGat,用于标记手势是否有效,并且注意往回滑动的处理。
3、如果底层存在点击Item,为了防止滑动过程中变色,可以适时地屏蔽触摸事件:手动构造Cancle事件主动下发,这是为了兼容最基本的点击效果,不过,满足点击的手势判定前,Move事件要正常下发。具体实现如下:
@Override publicbooleandispatchTouchEvent(MotionEventevent){caseMotionEvent.ACTION_MOVE: if(Math.abs(event.getX()-down_X)>10 &&flagDirection==MotionDirection.HORIZION){ MotionEvente=MotionEvent.obtain(event.getEventTime(), event.getEventTime(), MotionEvent.ACTION_CANCEL, event.getX(), event.getY(),0); super.dispatchTouchEvent(e); }else{ super.dispatchTouchEvent(event);//不符合条件正常下发 }
4、防止手势的往回滑动,最好利用GestureDectetor来判断,如果存在往回滑动,则手势无效,使用方式如下:
mDetector=newGestureDetector(this,newGestureDetector.SimpleOnGestureListener(){ @Override publicbooleanonScroll(MotionEvente1,MotionEvente2,floatdistanceX,floatdistanceY){ if(!slideReturnFlag&&distanceX>5){ slideReturnFlag=true; }}
5、如何处理Up事件:dispatch是否往下派发。具体的做法是,根据手势是否有效,如果手势无效,那么Up肯定是需要往下派发的。如果有效,根据后续操作进行,因为有时候为了防止子View获取到不必要的点击事件。具体实现如下
@Override publicbooleandispatchTouchEvent(MotionEventevent){ caseMotionEvent.ACTION_UP: if(mGestureListener!=null&&!slideReturnFlag &&flagDirection==MotionDirection.HORIZION){ if(stateMotion==CurrentMotionState.SlideRight){ mGestureListener.onSlideRight(); } }else{super.dispatchTouchEvent(event);//无效的手势 } flagDirection=MotionDirection.NONE; stateMotion=CurrentMotionState.NONE; slideReturnFlag=false; break;
6、在disPatch中最好记录down_X、down_Y,为了后面的处理与判断,因为dispatch中最能保证你获取到该事件。同时要保证Dispatch事件的下发,
第二:父容器级别的手势监听
注意事项:容器级别的监听至少要使得当前容器强制获取手势的焦点,至于如何获取焦点,可以自己编写onTouch事件,并且returetrue。不过我们把判断处理放在dispatch里面,这样能够保证事件完全获取。因为,如果底层消费了事件,onTouch是无法完整获取事件的,但是我们有足够的能力保证dispatch获取完整的事件。无论在本层onTouch消费,还是底层消费,dispatch是用于不会漏掉的。对于手势的容器,最好用padding,而不采用Magin,为什么呢,因为Margin不在容器内部。
1、父容器监听的使用场景
- 容器中,子View是否存在交互事件,是否存在滑动
- 上层容器是否存在拦截事件的可能,比如SrollView
2、实现
子View不存在交互事件:
这类容器可以采用Dispatch来实现,不过需要强制获取焦点,同时也要适时的释放焦点。具体实现如下:
如何保证本层一定接收到Down后续事件。dispatch的Down事件能够返回True即可。
如何保证本层不被偶然的屏蔽,使用getParent().requestDisallowInterceptTouchEvent(true)即可。当然,有强制获取也要适时的释放,当手势判定为无效的时候就要释放,具体实现如下:
@Override publicbooleandispatchTouchEvent(MotionEventev){ getParent().requestDisallowInterceptTouchEvent(true);</strong></span> mGestureDetector.onTouchEvent(ev); switch(ev.getActionMasked()){ caseMotionEvent.ACTION_DOWN: down_X=ev.getX(); down_Y=ev.getY(); slideReturnFlag=false; break; caseMotionEvent.ACTION_CANCEL: caseMotionEvent.ACTION_MOVE: if(Math.abs(down_X-ev.getX())<Math.abs(down_Y-ev.getY()) &&Math.abs(ev.getY()-down_Y)>mDistanceGate/2){ getParent().requestDisallowInterceptTouchEvent(false);</span></strong> } default: break; } returnsuper.dispatchTouchEvent(ev); }
子View存在交互事件:子View存在交互事件,就要通过dispatch与onTouch的配合使用,dispatch为了判断手势的有效性,同时既然从容器层开始,强制获取焦点是必须的,底层如何强制获取焦点,不关心。这里如果没有消费Down,则说明底层View消费了。同时要兼容无效手势强制焦点获取的释放,防止上传滚动View,具体实现如下:
@Override publicbooleandispatchTouchEvent(MotionEventev){ mGestureDetector.onTouchEvent(ev); switch(ev.getActionMasked()){ caseMotionEvent.ACTION_DOWN: down_X=ev.getX(); down_Y=ev.getY(); slideReturnFlag=false; break; default: break; } returnsuper.dispatchTouchEvent(ev); }
onTouch中处理响应事件,主要是为了防止底层获取后,上层还处理
//ACTION_CANCEL嵌套如其他scrowView可能屏蔽 @Override publicbooleanonTouchEvent(MotionEventev){ switch(ev.getActionMasked()){ caseMotionEvent.ACTION_DOWN:
//ACTION_CANCEL嵌套如其他scrowView可能屏蔽 @Override publicbooleanonTouchEvent(MotionEventev){ switch(ev.getActionMasked()){ caseMotionEvent.ACTION_DOWN: getParent().requestDisallowInterceptTouchEvent(true); returntrue; caseMotionEvent.ACTION_CANCEL: returntrue; caseMotionEvent.ACTION_UP: if(Math.abs(down_X-ev.getX())>Math.abs(down_Y-ev.getY())&&!slideReturnFlag &&ev.getX()-down_X>mDistanceGate){ //返回上个Activity,也有可能是返回上一个Fragment FragmentActivitymContext=null; if(getContext()instanceofFragmentActivity){ mContext=(FragmentActivity)getContext(); FragmentManagerfm=mContext.getSupportFragmentManager(); if(fm.getBackStackEntryCount()>0){ fm.popBackStack(); }else{ mContext.finish(); } } } returntrue; caseMotionEvent.ACTION_MOVE: if(Math.abs(down_X-ev.getX())<Math.abs(down_Y-ev.getY()) &&Math.abs(ev.getY()-down_Y)>mDistanceGate/2){ getParent().requestDisallowInterceptTouchEvent(false); } returntrue; default: break; } returnsuper.onTouchEvent(ev); }
3、父容器手势的拦截,有些时候,子View具有点击事件,点击变颜色。给予一定容错空间后,强制拦截事件。dispatch返回true保证事件下传,不必担心
@Override publicbooleanonInterceptTouchEvent(MotionEventev){ if(ev.getActionMasked()==MotionEvent.ACTION_MOVE&&Math.abs(down_X-ev.getX())>20) returntrue; returnsuper.onInterceptTouchEvent(ev); }
第四:HorizontalScrollView边缘状态下,滑动手势的监听,具体实现如下,主要是边缘处的手势判断。
@Override publicbooleandispatchTouchEvent(MotionEventev){ getParent().requestDisallowInterceptTouchEvent(true); mGestureDetector.onTouchEvent(ev); switch(ev.getActionMasked()){ caseMotionEvent.ACTION_DOWN: slideReturnFlag=false; down_X=ev.getX(); down_Y=ev.getY(); oldScrollX=getScrollX(); break; caseMotionEvent.ACTION_UP: if(Math.abs(down_X-ev.getX())>Math.abs(down_Y-ev.getY()) &&ev.getX()-down_X>mDistanceGate&&!slideReturnFlag &&oldScrollX==0){ //返回上个Activity,也有可能是返回上一个Fragment FragmentActivitymContext=null; if(getContext()instanceofFragmentActivity){ mContext=(FragmentActivity)getContext(); FragmentManagerfm=mContext.getSupportFragmentManager(); if(fm.getBackStackEntryCount()>0){ fm.popBackStack(); }else{ mContext.finish(); } } } break; caseMotionEvent.ACTION_MOVE: if(Math.abs(down_X-ev.getX())<Math.abs(down_Y-ev.getY()) &&Math.abs(ev.getY()-down_Y)>mDistanceGate/2){ getParent().requestDisallowInterceptTouchEvent(false); } default: break; } returnsuper.dispatchTouchEvent(ev); }
第五:防止垂直滚动的ScrollView过早的屏蔽事件:重写拦截函数即可:
@Override publicbooleanonInterceptTouchEvent(MotionEventev){ if(Math.abs(ev.getY()-down_Y)<getResources().getDimensionPixelSize(R.dimen.slide_gesture_vertical_gate)){ super.onInterceptTouchEvent(ev); returnfalse; } returnsuper.onInterceptTouchEvent(ev); } @Override publicbooleandispatchTouchEvent(MotionEventev){ switch(ev.getAction()){ caseMotionEvent.ACTION_DOWN: down_X=ev.getX(); down_Y=ev.getY(); break;
第六:Viewpager第一页滑动手势;
1、防止过早拦击
@Override publicbooleandispatchTouchEvent(MotionEventev){ getParent().requestDisallowInterceptTouchEvent(true); mGestureDetector.onTouchEvent(ev); switch(ev.getActionMasked()){ caseMotionEvent.ACTION_DOWN: down_X=ev.getX(); down_Y=ev.getY(); slideReturnFlag=false; break; caseMotionEvent.ACTION_MOVE: if(Math.abs(down_X-ev.getX())<Math.abs(down_Y-ev.getY()) &&Math.abs(ev.getY()-down_Y)>mDistanceGate/2){ getParent().requestDisallowInterceptTouchEvent(false); } break; default: break; } returnsuper.dispatchTouchEvent(ev); }
2、防止往回滑动等
/* *触摸事件的处理,要判断是否是ViewPager不可滑动的时候 */ @Override publicbooleanonTouchEvent(MotionEventarg0){ //防止跳动 booleanret=super.onTouchEvent(arg0); switch(arg0.getActionMasked()){ caseMotionEvent.ACTION_DOWN: Log.v("lishang","down"); break; caseMotionEvent.ACTION_CANCEL: caseMotionEvent.ACTION_UP: Log.v("lishang","up"); if(slideDirection==SlideDirection.RIGHT){ if(slideReturnFlag||getCurrentItem()!=0||arg0.getX()-down_X<mDistanceGate||mPercent>0.01f) break; }elseif(slideDirection==SlideDirection.LEFT){ if(getAdapter()!=null){ if(slideReturnFlag||getCurrentItem()!=getAdapter().getCount()-1 ||down_X-arg0.getX()<mDistanceGate||mPercent>0.01f) break; } }else{
第七:getParent().requestDisallowInterceptTouchEvent
这个函数的的作用仅仅能够保证事件不被屏蔽,但是倘若本层dispatch在down的时候返回false,那么事件的处理就无效了,就算强制获取焦点
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!