onTouchEven事件分发机制

来源:互联网 发布:生死狙击刷矩阵要多久 编辑:程序博客网 时间:2024/06/04 22:27
跟touch事件相关的3个方法:
public boolean dispatchTouchEvent(MotionEvent ev);    //用来分派event
是用来进行事件分发的。
public boolean onInterceptTouchEvent(MotionEvent ev); //用来拦截event onInterceptTouchEvent(昂因特赛普特他吃一温特)
public boolean onTouchEvent(MotionEvent ev);          //用来处理event
拥有三个方法的类
Activity类:ActivitydispatchTouchEvent();
onTouchEvent();View容器(ViewGroup的子类):FrameLayout、LinearLayout……
ListView、ScrollVIew……dispatchTouchEvent();
onInterceptTouchEvent();
onTouchEvent();View控件(非ViewGroup子类):Button、TextView、EditText……dispatchTouchEvent();
onTouchEvent();


三个方法的用法:
dispatchTouchEvent()用来分派事件。
其中调用了onInterceptTouchEvent()和onTouchEvent(),一般不重写该方法onInterceptTouchEvent()用来拦截事件。
ViewGroup类中的源码实现就是{return false;}表示不拦截该事件,
事件将向下传递(传递给其子View);
若手动重写该方法,使其返回true则表示拦截,事件将终止向下传递,
事件由当前ViewGroup类来处理,就是调用该类的onTouchEvent()方法onTouchEvent()用来处理事件。
返回true则表示该View能处理该事件,事件将终止向上传递(传递给其父View);
返回false表示不能处理,则把事件传递给其父View的onTouchEvent()方法来处理
【注】:ViewGroup的某些子类(GridView、ScrollView...)重写了onInterceptTouchEvent()方法,当发生ACTION_MOVE事件时,返回true进行拦截。
actitity里面只有分发机制没有拦截机制----》viewGroup有拦截机制和分发机制----》view里面可以消费




0 0