android的事件分发机制

本文详细解析了Android事件分发机制,通过实例说明了事件如何在不同层级的视图中进行传递与拦截,帮助读者更好地理解这一核心概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们在面试的时候经常会被问到android事件分发机制,对于这个知识点其实也不算太难,关键在于不好理解,其实总结下来就两句话:

android事件分发过程:先由父类控件判断是否拦截(onInterceptTouchEvent() is true or false),拦截的话则执行该View的onTouchEvent()事件,否则则继续分发...

android事件处理过程:由子View先处理,如果子View不处理则交由父控件处理,否则一直向上传递。

举个例子:
这里有3个控件,分别实现了dispatchTouchEvent(),onInterceptTouchEvent(),onTouchEvent();

<?xml version="1.0" encoding="utf-8"?>
<com.luck.anim.touch.MyLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.luck.anim.touch.MyRelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.luck.anim.touch.MyButton
            android:id="@+id/my_btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="自定义Button" />
        
    </com.luck.anim.touch.MyRelativeLayout>
</com.luck.anim.touch.MyLinearLayout>

当我们都按默认返回的话,我点击MyButton时,执行的顺序是:
MyLinearLayout--->: dispatchTouchEvent
MyLinearLayout--->: onInterceptTouchEvent
MyRelativeLayout--->: dispatchTouchEvent
MyRelativeLayout--->: onInterceptTouchEvent
MyButton--->: dispatchTouchEvent
MyButton--->: onTouchEvent

但我们在MyRelativeLayout中拦截此事件的话他的一个分发过程又是如何的呢?
MyLinearLayout--->: dispatchTouchEvent
MyLinearLayout--->: onInterceptTouchEvent
MyRelativeLayout--->: dispatchTouchEvent
MyRelativeLayout--->: onInterceptTouchEvent
MyRelativeLayout--->: onTouchEvent
MyLinearLayout--->: onTouchEvent

从这里可以看出当我们在某一层View中拦截掉事件的话,他不会继续向下分发了,而是执行自身View的3个函数了,但当我们并没有在当前View的onTouchEvent()事件做处理时,他还同时向上父级执行了onTouchEvent()事件。



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值