前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >Android消息处理学习笔记(一)

Android消息处理学习笔记(一)

原创
作者头像
进击的阿斌
发布2024-11-22 21:08:01
发布2024-11-22 21:08:01
690
举报

Android事件分发机制

一、主要函数

事件分发设计的几个函数:

代码语言:txt
复制
public boolean dispatchTouchEvent(MotionEvent ev)
public boolean onInterceptTouchEvent(MotionEvent ev)
public boolean onTouchEvent(MotionEvent event)

MotionEvent 的几个取值

代码语言:txt
复制

    /**
     * Constant for {@link #getActionMasked}: A pressed gesture has started, the
     * motion contains the initial starting location.
     * <p>
     * This is also a good time to check the button state to distinguish
     * secondary and tertiary button clicks and handle them appropriately.
     * Use {@link #getButtonState} to retrieve the button state.
     * </p>
     */
    public static final int ACTION_DOWN             = 0;

    /**
     * Constant for {@link #getActionMasked}: A pressed gesture has finished, the
     * motion contains the final release location as well as any intermediate
     * points since the last down or move event.
     */
    public static final int ACTION_UP               = 1;

    /**
     * Constant for {@link #getActionMasked}: A change has happened during a
     * press gesture (between {@link #ACTION_DOWN} and {@link #ACTION_UP}).
     * The motion contains the most recent point, as well as any intermediate
     * points since the last down or move event.
     */
    public static final int ACTION_MOVE             = 2;

    /**
     * Constant for {@link #getActionMasked}: The current gesture has been aborted.
     * You will not receive any more points in it.  You should treat this as
     * an up event, but not perform any action that you normally would.
     */
    public static final int ACTION_CANCEL           = 3;

二、事件及事件传递过程

1、Activity

2、ViewGroup(一组view的集合)

3、View

三者之间的关系:

事件传递过程:Activity =》ViewGroup =》View

dispatchTouchEvent返回值

三种返回值:

代码语言:txt
复制
return super.onTouchEvent(event);
return true;
return false;

三、事件拦截

ACTION_DOWN消息

1、不包含onInterceptTouchEvent函数的消息传递流程

正常的消息传递流程:

(1)在dispatchTouchEvent函数中返回true拦截

在某一个传递对象中返回了true,即将此事件消费不会往下继续传递,比方说在Activity 中返回true拦截,即消费了该消息,消息不会传递到下一层view了。

(2)在dispatchTouchEvent函数中返回false拦截

当返回false进行拦截,消息不会继续在dispatchTouchEvent这条线上传递;但是消息不会停止传递,而是继续向父控件的onTouchEvent函数进行回传。比方说在某一个TextView的dispatchTouchEvent函数中返回false,则会将消息回传给其父控件的onTouchEvent函数。

(3)在onTouchEvent函数中拦截ACTION_DOWN消息

无论在哪个控件的onTouchEvent函数中拦截消息,消息都会停止传递,后面的父控件不会接收到此消息。

总结:dispatchTouchEvent和onTouchEvent函数返回true拦截消息,ACTION_DOWN消息都会被消费掉,即停止传递,正常流程下的后续节点都不会收到消息;

当dispatchTouchEvent返回fasle时,首先拦截消息向子控件中传递,然后将消息传递给父控件的onTouchEvent函数中继续回传。

2、包含onInterceptTouchEvent函数的消息传递流程

只有ViewGroup具有onInterceptTouchEvent函数

在ViewGroup函数中onInterceptTouchEvent函数返回true拦截Action_DOWN消息,消息会流向当前ViewGroup的OnTouchEvent函数,若OnTouchEvent函数没有返回true拦截消息,消息则会继续回传。若在某个OnTouchEvent函数返回true拦截,则消息停止传递。。

总结

ACTION_DOWN消息的传递总结:

1、若dispatchTouchEvent函数和OnTouchEvent函数返回true,消息直接拦截,后续组件不会收到消息

2、若在onInterceptTouchEvent函数拦截消息,只会改变消息的走向,将消息传递给自己的OnTouchEvent函数,不会拦截消息

3、若在dispatchTouchEvent函数返回false拦截消息,会改变消息的正常流向,消息流向父控件的OnTouchEvent函数,并不会截断消息。

4、dispatchTouchEvent函数返回true,消息直接拦截掉

平常开发中使用onInterceptTouchEvent和OnTouchEvent函数就可以实现消息的拦截。

下次记录学习ACTION_MOVE和ACTION_UP消息。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Android事件分发机制
    • 一、主要函数
    • 二、事件及事件传递过程
  • 三、事件拦截
    • ACTION_DOWN消息
    • 总结
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档