View事件分发

dispatchTouchEvent()

      onInterceptTouchEvent()  

      onTouchEvent()  

 

dispatchTouchEvent()

该方法是用来处理事件的分发。如果事件能够传递到当前View,那么一定会调用此方法

源码:

public boolean dispatchTouchEvent(Motion e){
     boolean result=false;
     if(onInterceptTouchEvent(e)){
     //如果当前View截获事件,那么事件就会由当前View处理,即调用onTouchEvent()
        result=onTouchEvent(e);
     }else{
        //如果不截获那么交给其子View来分发
        result=child.dispatchTouchEvent(e);
     }
     return result;
}

 

 onInterceptTouchEvent()

该方法在上边可以看出,是在dispatchTouchEvent中调用,来判断自己是否需要截取事件,如果该方法返回为true,那么View将消费该事件,即会调用onTouchEvent()方法。如果返回false,那么通过调用子View的dispatchTouchEvent()将事件交由子View来处理

 

 onTouchEvent()

和onInterceptTouchEvent()一样也是在dispatchTouchEvent中调用的。用来处理点击事件,包括ACTION_DOWN,ACTION_MOVE,ACTION_UP。如果返回结果为false表示不消费该事件,并且也不会截获接下来的事件序列。如果返回为true表示当前View消费该事件。

 E/ViewGroup1: ViewGroup1 dispatchTouchEvent
 E/ViewGroup1: ViewGroup1 onInterceptTouchEvent
 E/ViewGroup2: ViewGroup2 dispatchTouchEvent
 E/ViewGroup2: ViewGroup2 onInterceptTouchEvent
 E/CustomView: CustomView dispatchTouchEvent
 E/CustomView: CustomView onTouchEvent
 E/ViewGroup2: ViewGroup2 onTouchEvent
 E/ViewGroup1: ViewGroup1 onTouchEvent

https://www.tuicool.com/articles/byAbuqa

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值