Android App开发触摸事件中手势事件Event的分发流程讲解与实战(附源码 简单易懂)

简介: Android App开发触摸事件中手势事件Event的分发流程讲解与实战(附源码 简单易懂)

需要源码或运行有问题请点赞关注收藏后评论区留言~~~

一、手势事件的分发流程

智能手机的一大革命性技术就是把屏幕变为可触摸设备,既可用于信息输入也可以用于信息输出。与手势事件有关的方法主要有以下三个

dispatchTouchEvent  进行事件分发处理 返回结果表示该事件是否需要分发

onInterceptTouchEvent  进行事件拦截处理 返回结果表示当前容器是否需要拦截该处理

onTouchEvent  进行事件触摸事件 返回结果表示该事件是否处理完毕

上述手势方法的执行者有三个

页面类  包括Activity及其派生类

容器类  包括从ViewGroup派生出的各类容器

控件类  包括从View类派生的各类控件

处理流程如下

经过上图的分析,常见的手势处理方法可以总结为以下三种

页面类的dispatchTouchEvent 控制事件的分发 决定把手势交给谁处理

容器类的onInterceptTouchEvent  控制事件的拦截 决定是否要把手势交给子视图处理

控件类的onTouchEvent 进行手势处理的具体处理

下面是对不派发事件的处理效果

点击按钮后即会出现相应效果

下面是拦截事件的处理效果

代码如下

Java类

package com.example.event;
import com.example.event.util.DateUtil;
import com.example.event.widget.NotDispatchLayout;
import com.example.event.widget.NotDispatchLayout.NotDispatchListener;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.TextView;
public class EventDispatchActivity extends AppCompatActivity implements NotDispatchListener {
    private TextView tv_dispatch_yes; // 声明一个文本视图对象
    private TextView tv_dispatch_no; // 声明一个文本视图对象
    private String desc_yes = "", desc_no = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event_dispatch);
        tv_dispatch_yes = findViewById(R.id.tv_dispatch_yes);
        tv_dispatch_no = findViewById(R.id.tv_dispatch_no);
        NotDispatchLayout ndl_no = findViewById(R.id.ndl_no);
        // 设置不分发布局的事件分发监听器
        ndl_no.setNotDispatchListener(this);
        findViewById(R.id.btn_dispatch_yes).setOnClickListener(v -> {
            desc_yes = String.format("%s%s 您点击了按钮\n", desc_yes, DateUtil.getNowTime());
            tv_dispatch_yes.setText(desc_yes);
        });
        findViewById(R.id.btn_dispatch_no).setOnClickListener(v -> {
            desc_no = String.format("%s%s 您点击了按钮\n", desc_no, DateUtil.getNowTime());
            tv_dispatch_no.setText(desc_no);
        });
    }
    // 在分发触摸事件时触发
    @Override
    public void onNotDispatch() {
        desc_no = String.format("%s%s 触摸动作未分发,按钮点击不了了\n"
                , desc_no, DateUtil.getNowTime());
        tv_dispatch_no.setText(desc_no);
    }
}

拦截事件类

package com.example.event;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.TextView;
import com.example.event.util.DateUtil;
import com.example.event.widget.InterceptLayout;
import com.example.event.widget.InterceptLayout.InterceptListener;
public class EventInterceptActivity extends AppCompatActivity implements InterceptListener {
    private TextView tv_intercept_no; // 声明一个文本视图对象
    private TextView tv_intercept_yes; // 声明一个文本视图对象
    private String desc_no = "", desc_yes = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event_intercept);
        tv_intercept_no = findViewById(R.id.tv_intercept_no);
        tv_intercept_yes = findViewById(R.id.tv_intercept_yes);
        InterceptLayout il_yes = findViewById(R.id.il_yes);
        // 设置拦截布局的事件拦截监听器
        il_yes.setInterceptListener(this);
        findViewById(R.id.btn_intercept_no).setOnClickListener(v -> {
            desc_no = String.format("%s%s 您点击了按钮\n", desc_no, DateUtil.getNowTime());
            tv_intercept_no.setText(desc_no);
        });
        findViewById(R.id.btn_intercept_yes).setOnClickListener(v -> {
            desc_yes = String.format("%s%s 您点击了按钮\n", desc_yes, DateUtil.getNowTime());
            tv_intercept_yes.setText(desc_yes);
        });
    }
    // 在拦截触摸事件时触发
    @Override
    public void onIntercept() {
        desc_yes = String.format("%s%s 触摸动作被拦截,按钮点击不了了\n", desc_yes,
                DateUtil.getNowTime());
        tv_intercept_yes.setText(desc_yes);
    }
}

XML文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:orientation="vertical" >
        <Button
            android:id="@+id/btn_dispatch_yes"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="这里允许分发给下级"
            android:textColor="@color/black"
            android:textSize="17sp" />
        <TextView
            android:id="@+id/tv_dispatch_yes"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/black"
            android:textSize="17sp" />
    </LinearLayout>
    <com.example.event.widget.NotDispatchLayout
        android:id="@+id/ndl_no"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:orientation="vertical" >
        <Button
            android:id="@+id/btn_dispatch_no"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="这里不允许发给下级"
            android:textColor="@color/black"
            android:textSize="17sp" />
        <TextView
            android:id="@+id/tv_dispatch_no"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:textColor="@color/black"
            android:textSize="17sp" />
    </com.example.event.widget.NotDispatchLayout>
</LinearLayout>

拦截事件的XML文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:orientation="vertical" >
        <Button
            android:id="@+id/btn_intercept_no"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="这里不拦截下级的事件"
            android:textColor="@color/black"
            android:textSize="17sp" />
        <TextView
            android:id="@+id/tv_intercept_no"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:textColor="@color/black"
            android:textSize="17sp" />
    </LinearLayout>
    <com.example.event.widget.InterceptLayout
        android:id="@+id/il_yes"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:orientation="vertical" >
        <Button
            android:id="@+id/btn_intercept_yes"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="这里拦截了下级的事件"
            android:textColor="@color/black"
            android:textSize="17sp" />
        <TextView
            android:id="@+id/tv_intercept_yes"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:textColor="@color/black"
            android:textSize="17sp" />
    </com.example.event.widget.InterceptLayout>
</LinearLayout>

创作不易 觉得有帮助请点赞关注收藏~~~

相关文章
|
10月前
|
存储 Java PHP
轻量化短视频电商直播带货APP源码全解析:核心功能与设计流程​
在电商直播热潮下,开发专属直播带货APP成为抢占市场关键。本文详解原生开发轻量化APP的核心功能与全流程设计,涵盖用户登录、商品浏览、直播互动、购物车、订单及售后功能,并介绍安卓端Java、苹果端Object-C、后台PHP的技术实现,助力打造高效优质的直播电商平台。
|
9月前
|
存储 小程序 Java
热门小程序源码合集:微信抖音小程序源码支持PHP/Java/uni-app完整项目实践指南
小程序已成为企业获客与开发者创业的重要载体。本文详解PHP、Java、uni-app三大技术栈在电商、工具、服务类小程序中的源码应用,提供从开发到部署的全流程指南,并分享选型避坑与商业化落地策略,助力开发者高效构建稳定可扩展项目。
|
11月前
|
小程序 Java 关系型数据库
圈子系统公众号app小程序系统源码圈子系统带即时通讯 多级圈子系统源码 兴趣小组系统开源 私密圈子系统代码 会员制社区系统
本圈子系统解决方案提供即时通讯、多级圈子、兴趣小组、私密社区及会员制管理功能。支持开源与商业方案,推荐ThinkSNS+、EasyClub及OpenFire等系统,并提供前后端技术选型建议,助力快速搭建社交平台。
591 0
|
11月前
不封号的外卖抢单神器,美团抢单辅助器app,autojs版本源码
这个代码提供了基础框架,包含主循环、订单检测和点击功能。实际使用时需要根据美团骑手AP
|
Android开发
Android Touch事件分发(源码分析)
Android一文让你轻松搞定Touch事件分发 源码分析 Activity事件分发机制 Activity.dispatchTouchEvent()源码 Activity.onTouchEvent()源码 Activity源码总结 ViewGroup事件分发机制 ViewGroup.dispatchTouchEvent()源码 ViewGroup.onInterceptTouchEvent()源码 ViewGroup.onTouchEvent()源码 ViweGroup源码总结 View的事件分发机制 View.dispatchTouchEvent()源码
340 0
Android Touch事件分发(源码分析)
|
Java Android开发
【Android 事件分发】事件分发源码分析 ( Activity 中各层级的事件传递 | Activity -> PhoneWindow -> DecorView -> ViewGroup )
【Android 事件分发】事件分发源码分析 ( Activity 中各层级的事件传递 | Activity -> PhoneWindow -> DecorView -> ViewGroup )
487 0
【Android 事件分发】事件分发源码分析 ( Activity 中各层级的事件传递 | Activity -> PhoneWindow -> DecorView -> ViewGroup )
|
Java Android开发
【Android 事件分发】事件分发源码分析 ( ViewGroup 事件传递机制 七 )(二)
【Android 事件分发】事件分发源码分析 ( ViewGroup 事件传递机制 七 )(二)
259 0
|
Android开发
【Android 事件分发】事件分发源码分析 ( ViewGroup 事件传递机制 六 )(四)
【Android 事件分发】事件分发源码分析 ( ViewGroup 事件传递机制 六 )(四)
765 0
|
Android开发
【Android 事件分发】事件分发源码分析 ( ViewGroup 事件传递机制 六 )(二)
【Android 事件分发】事件分发源码分析 ( ViewGroup 事件传递机制 六 )(二)
233 0
|
Java Android开发 容器
【Android 事件分发】事件分发源码分析 ( ViewGroup 事件传递机制 五 )(四)
【Android 事件分发】事件分发源码分析 ( ViewGroup 事件传递机制 五 )(四)
218 0