zoukankan      html  css  js  c++  java
  • Android Touch事件分发

    跟touch事件相关的3个方法:
    public boolean dispatchTouchEvent(MotionEvent ev); //用来分派event
    public boolean onInterceptTouchEvent(MotionEvent ev); //用来拦截event
    public boolean onTouchEvent(MotionEvent ev); //用来处理event
    三个方法的用法:
    dispatchTouchEvent() 用来分派事件
    其中调用了onInterceptTouchEvent()和onTouchEvent()一般不重写该方法
    onInterceptTouchEvent() 用来拦截事件。
    ViewGroup类中的源码实现就是{return false;}表示不拦截该事件,
    事件将向下传递(传递给其子View);
    若手动重写该方法,使其返回true则表示拦截,事件将终止向下传递
    事件由当前ViewGroup类来处理,就是调用该类的onTouchEvent()方法
    onTouchEvent() 用来处理事件。
    返回true则表示该View能处理该事件,事件将终止向上传递(传递给其父View);
    返回false表示不能处理,则把事件传递给其父View的onTouchEvent()方法来处理
    【注】:ViewGroup的某些子类(GridView、ScrollView…)重写了onInterceptTouchEvent()方法,当发生ACTION_MOVE事件时,返回true进行拦截。

    拥有这三个方法的类:
    Activity        :dispatchTouchEvent();                 onTouchEvent();
    ViewGroup       :dispatchTouchEvent();   onInterceptTouchEvent();     onTouchEvent();
    View         :dispatchTouchEvent();                 onTouchEvent();

    现在重写  activity ViewGroup  View 的相应方法,构造布局。

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <com.event.MyViewGroup
            android:id="@+id/myViewGroup"
            android:background="@color/colorPrimary"
            android:layout_width="400dp"
            android:layout_height="400dp">
    
            <com.event.MyView
                android:id="@+id/myView"
                android:layout_width="200dp"
                android:layout_height="200dp" />
        </com.pig.event.MyViewGroup>
    
    
    </android.support.constraint.ConstraintLayout>

    1、点击空白处(activity),输出:

    activity   dispatchTouchEvent: 
    activity   onTouchEvent: 

    直接点击  activity  触发  activity相应方法。与 内部控件无关

    2、点击蓝色部分(ViewGroup),输出:(当我们手动设置OnTouchListener  并且返回true时,会拦截事件, onTouchEvent不会触发   )

     activity   dispatchTouchEvent: 
     group dispatchTouchEvent: 
     group  onInterceptTouchEvent: 
     group  onTouch:  (OnTouchListener 部分)
     group onTouchEvent: 
     activity   onTouchEvent: 

    (1)由于activity没有拦截事件的方法,事件肯定会传进ViewGroup。

     (2)ViewGroup 中的onTouchEvent 默认返回fasle  表示事件未处理完,则会触发activity的onTouchEvent

    3、点击按钮(View),输出:(当我们手动设置OnTouchListener  并且返回true时,会拦截事件, onTouchEvent不会触发   )

     activity   dispatchTouchEvent: 
     group dispatchTouchEvent: 
     group  onInterceptTouchEvent: 
     view   dispatchTouchEvent: 
     view   onTouch: (OnTouchListener 部分)
     view  onTouchEvent: 

    (1)activity分发事件进ViewGroup ,ViewGroup分发事件时,选择是否拦截事件,若不拦截则分发事件到View,若拦截事件,ViewGroup自己将事件处理,View不会接收到事件。

     (2)view 的onTouchEvent默认返回true ,表示事件以处理完,不会触发ViewGroup  和 activity 的onTouchEvent。

  • 相关阅读:
    【网络公开课总结】砸蛋中奖效果
    【网络收集】如何修改vs tfs的登录名和密码 .
    在rdlc 中 显示成 yyyy年MM月dd日
    sql 将某一列的值拼接成字符串
    【网络收集】获取JavaScript 的时间使用内置的Date函数完成
    处理传递过来的拼接的值,中间用逗号相隔
    行列转换 pivot
    【网络收集】存储过程中自动生成主键
    Java多线程基础知识整理二
    Java多线程基础知识整理
  • 原文地址:https://www.cnblogs.com/the-wang/p/9842678.html
Copyright © 2011-2022 走看看