事件分发

来源:互联网 发布:js equals方法 编辑:程序博客网 时间:2024/06/03 21:24

1.一个应用的布局有好几层最外面的linearlayout 里面嵌套viewgroup   viewgroup里面有一个小view   每一层viewgroup都有事件分发包括

dispatchHoverEvent    onInterceptTouchEvent(拦截事件)   onTouchEvent 要处理就返回true 不处理就返回false
拦截事件如果返回true,那么onTouchEvent则会启动onTouchEvent,返回true就是本方法处理,是false的话就是dispatchHoverEvent处理

如果都是false的话就会一直往子view传 如果都不处理那么这个事件就会没有任何作用      viewgroup就是布局里面可以放控件的的都是
比如线性布局,相对布局,帧布局,绝对布局     控件的话没有拦截方法,因为控件里面没有子控件了。
2.在项目中自己创一个类继承布局    然后在里面重写那3个方法
public class mylayout extends LinearLayout {    public mylayout(Context context) {        super(context);    }    public mylayout(Context context, AttributeSet attrs) {        super(context, attrs);    }    @Override    protected boolean dispatchHoverEvent(MotionEvent event) {        switch (event.getAction()) {            case MotionEvent.ACTION_DOWN:                //手指按下                break;            case MotionEvent.ACTION_MOVE:                //手指移动                break;            case MotionEvent.ACTION_UP:                //手指抬起                break;            case MotionEvent.ACTION_CANCEL:                //取消触摸事件的传递                break;        }        return super.dispatchHoverEvent(event);    }        @Override    public boolean onInterceptTouchEvent(MotionEvent ev) {        switch (ev.getAction()) {            case MotionEvent.ACTION_DOWN:                break;            case MotionEvent.ACTION_MOVE:                break;            case MotionEvent.ACTION_UP:                break;            case MotionEvent.ACTION_CANCEL:                break;        }        return super.onInterceptTouchEvent(ev);    }    @Override    public boolean onTouchEvent(MotionEvent event) {        switch (event.getAction()) {            case MotionEvent.ACTION_DOWN:                break;            case MotionEvent.ACTION_MOVE:                break;            case MotionEvent.ACTION_UP:                break;            case MotionEvent.ACTION_CANCEL:                break;        }        return super.onTouchEvent(event);    }}

 
原创粉丝点击