前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >D7-测试Android事件处理机制和生命函数调用

D7-测试Android事件处理机制和生命函数调用

作者头像
张风捷特烈
发布2018-09-29 11:21:54
4050
发布2018-09-29 11:21:54
举报

布局很简单,代码就是打个日志看看,为了不影响阅读,放在最后 Android事件处理机制真是个磨人的小妖精,被她卡过两次,一卡住就不行玩安卓,跑过去玩html、js、css了 最好自己动手测试一下,印象更深

一、妖鸟三姐妹:dispatchTouchEvent---onInterceptTouchEvent---onTouchEvent

点击:ViewGroup1(最外层)

I/System.out: MainActivity dispatchTouchEvent
I/System.out: ViewGroup1 dispatchTouchEvent
              ViewGroup1 onInterceptTouchEvent
I/System.out: ViewGroup1 onTouchEvent
              MainActivity onTouchEvent
I/System.out: MainActivity dispatchTouchEvent
              MainActivity onTouchEvent

点击ViewGroup 1.png


点击:ViewGroup2(中间层)

I/System.out: MainActivity dispatchTouchEvent
              ViewGroup1 dispatchTouchEvent
              ViewGroup1 onInterceptTouchEvent
              ViewGroup2 dispatchTouchEvent
              ViewGroup2 onInterceptTouchEvent
              ViewGroup2 onTouchEvent
              ViewGroup1 onTouchEvent
              MainActivity onTouchEvent
I/System.out: MainActivity dispatchTouchEvent
              MainActivity onTouchEvent

点击ViewGroup 2.png


点击:View1

I/System.out: MainActivity dispatchTouchEvent
I/System.out: ViewGroup1 dispatchTouchEvent
              ViewGroup1 onInterceptTouchEvent
              ViewGroup2 dispatchTouchEvent
              ViewGroup2 onInterceptTouchEvent
              View1 dispatchTouchEvent
              View1 onTouchEvent
              ViewGroup2 onTouchEvent
              ViewGroup1 onTouchEvent
              MainActivity onTouchEvent
I/System.out: MainActivity dispatchTouchEvent
              MainActivity onTouchEvent

点击View.png


苹果树上掉下(分发:dispatchTouchEvent false)个苹果(event),爷爷(ViewGroup1)拿到。 爷爷如果不分发(dispatchTouchEvent true)了,相当于把苹果扔了,谁都不吃,包括他自己 他如果截断(onInterceptTouchEvent true),自己可以把苹果吃了(执行onTouchEvent 并返回true),也可以不吃(flase),但只要截断,就不会再向下传了。

点击ViewGroup.png

dispatchTouchEvent:决定了事件是否继续分发下去和是否响应事件
false:继续分发,
true:不继续分发--此次事件到此结束,也不会有任何控件执行onTouchEvent方法。

onInterceptTouchEvent:决定了是否拦截该事件
false:不拦截
true:拦截---此时当前控件执行onTouchEvent方法。

onTouchEvent:决定了是否消费该事件
false:不消费
true:消费。

二、事件处理机制对view.setOnClickListener的影响

对于View1来说:setOnClickListener: 能执行必须前面的分发,不截断,畅通无阻, 并且自己的onTouchEvent返回super.onTouchEvent(event),有一个环节出现意外都无法触发

对于ViewGroup2(也就是View1的父容器),所有均默认,如果View1的onTouchEvent返回false 则点击View1的时候ViewGroup2的setOnClickListener会触发。

所以来看setOnClickListener的触发条件还是蛮苛刻的。


三、View的几个生命函数的调用顺序

用一个ViewGroup3,包裹View2和View3两个View,进行测试,详情看图

ViewGroup.png


四、代码:
1.事件测试部分

View1

public class View1 extends View {
    public View1(Context context) {
        super(context);
    }

    public View1(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public View1(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        System.out.println(getClass().getSimpleName() + " dispatchTouchEvent:" );
        return super.dispatchTouchEvent(event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        System.out.println(getClass().getSimpleName() + " onTouchEvent:" );
        return super.onTouchEvent(event);
    }
}

ViewGroup1

public class ViewGroup1 extends LinearLayout {
    public ViewGroup1(Context context) {
        super(context);
    }

    public ViewGroup1(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public ViewGroup1(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        System.out.println(getClass().getSimpleName() + " dispatchTouchEvent:" );
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        System.out.println(getClass().getSimpleName() + " onInterceptTouchEvent:");
        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        System.out.println(getClass().getSimpleName() + " onTouchEvent:" );
        return super.onTouchEvent(event);
    }
}

ViewGroup2

public class ViewGroup2 extends LinearLayout {
    public ViewGroup2(Context context) {
        super(context);
    }

    public ViewGroup2(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public ViewGroup2(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        System.out.println(getClass().getSimpleName() + " dispatchTouchEvent:" );
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        System.out.println(getClass().getSimpleName() + " onInterceptTouchEvent:" );
        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        System.out.println(getClass().getSimpleName() + " onTouchEvent:");
        return super.onTouchEvent(event);
    }
}

布局:

<com.toly1994.d.event.ViewGroup1
    android:id="@+id/vg1"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <com.toly1994.d.event.ViewGroup2
        android:id="@+id/vg2"
        android:layout_width="@dimen/dp_200"
        android:layout_height="@dimen/dp_200"
        android:background="@android:color/darker_gray"
        android:orientation="vertical"
        android:padding="20dp">

        <com.toly1994.d.event.View1
            android:id="@+id/v1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@android:color/white"
            android:gravity="center"
            android:padding="20dp"
            android:text="me is textview"/>
    </com.toly1994.d.event.ViewGroup2>

</com.toly1994.d.event.ViewGroup1>
2.生命函数测试

View2

public class View2 extends View {
    public View2(Context context) {
        super(context);
    }

    public View2(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public View2(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        System.out.println(getClass().getSimpleName() + " onMeasure:" );
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        System.out.println(getClass().getSimpleName() + " onSizeChanged:" );
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        System.out.println(getClass().getSimpleName() + " onLayout:" );
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        System.out.println(getClass().getSimpleName() + " onFinishInflate:" );
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        System.out.println(getClass().getSimpleName() + " onDraw:" );
    }
}

View3

public class View3 extends View {
    public View3(Context context) {
        super(context);
    }

    public View3(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public View3(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        System.out.println(getClass().getSimpleName() + " onMeasure:" );
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        System.out.println(getClass().getSimpleName() + " onSizeChanged:" );
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        System.out.println(getClass().getSimpleName() + " onLayout:" );
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        System.out.println(getClass().getSimpleName() + " onFinishInflate:" );
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        System.out.println(getClass().getSimpleName() + " onDraw:" );
    }
}

ViewGroup3

public class ViewGroup3 extends LinearLayout {
    public ViewGroup3(Context context) {
        super(context);
    }

    public ViewGroup3(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public ViewGroup3(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        System.out.println(getClass().getSimpleName() + " onMeasure:");
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        System.out.println(getClass().getSimpleName() + " onFinishInflate:");
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        System.out.println(getClass().getSimpleName() + " onSizeChanged:");
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        System.out.println(getClass().getSimpleName() + " onLayout:");
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        System.out.println(getClass().getSimpleName() + " onDraw:");
    }
}

布局:

<?xml version="1.0" encoding="utf-8"?>
<com.toly1994.d.event.ViewGroup3
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="5dp"
    android:background="@color/bisque"
    android:orientation="horizontal">

    <com.toly1994.d.event.View2
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@color/blue_dark"/>


    <com.toly1994.d.event.View3
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@color/darkkhaki"/>

</com.toly1994.d.event.ViewGroup3>

后记、
1.声明:

[1]本文由张风捷特烈原创,转载请注明 [2]欢迎广大编程爱好者共同交流 [3]个人能力有限,如有不正之处欢迎大家批评指证,必定虚心改正 [4]你的喜欢与支持将是我最大的动力

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.09.08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

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

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、妖鸟三姐妹:dispatchTouchEvent---onInterceptTouchEvent---onTouchEvent
  • 四、代码:
    • 1.事件测试部分
      • 2.生命函数测试
      • 后记、
        • 1.声明:
        相关产品与服务
        容器服务
        腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档