Android dispatchTouchEvent源码分析

本文详细分析了Android中Activity、ViewGroup和View在处理Touch事件分发的流程。从Activity的dispatchTouchEvent开始,事件会传递到ViewGroup,其中ViewGroup通过dispatchTouchEvent、onInterceptTouchEvent和onTouchEvent进行事件处理和拦截。ViewGroup的onInterceptTouchEvent决定了事件是否由自身处理或传递给子View。在View中,若mOnTouchListener存在并返回false,事件会触发onTouchEvent。整个事件分发路径可能涉及多个ViewGroup和View的嵌套交互。
摘要由CSDN通过智能技术生成

大家应该对Android Touch事件分发流程有了大致的了解,其中主要的分发对象包括

Activity:

在设备获取到事件之后首先流转到的既是Activity,如果在所有View都不处理Touch事件的情况下最后也是传回Activity处理

  • 我们先来看Activity中的dispatchTouchEvent
public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            onUserInteraction();
        }
        if (getWindow().superDispatchTouchEvent(ev)) {
            return true;
        }
        return onTouchEvent(ev);
    }
1.onUserInteraction() 在Activity中为空实现
2.getWindow().superDispatchTouchEvent(ev) windows的具体唯一实现PhoneWindow,所以去PhoneWindow查找superDispatchTouchEvent()
  • PhoneWindow.java
    @Override
    public boolean superDispatchTouchEvent(MotionEvent event) {
        return mDecor.superDispatchTouchEvent(event);
    }

mDecor 即为DecorView,DecorView为一个FrameLayout,我们知道FrameLayout父类也为ViewGroup,所以到此就由Activity传到了ViewGroup中去了

public class DecorView extends FrameLayout
  • 最后我们梳理一下Activity传递到ViewGroup的流程:
Created with Raphaël 2.1.0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值