ViewGroup和View中onTouchEvent和onInterceptTouchEvent相关知识学习

为了研究ViewGroup和View中onTouchEvent和onInterceptTouchEvent中事件的传递和分发机制,写了个如下demo进行测试。

准备:两个ViewGroup和一个View,其中两个ViewGroup一层层包含view
如下是一个ViewGroup和View,另外一个ViewGroup一样的,

package guo.com.ontoucheventtest;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.LinearLayout;

/**
 * Created by ${GuoZhaoHui} on 2017/4/28.
 * email:[email protected]
 */

public class LL1 extends LinearLayout {
   

    private static final String TAG = "LL1";

    public LL1(Context context) {
        super(context);
    }

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

    public LL1(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:

                Log.d(TAG, "--------down--------------");

                break;
            case MotionEvent.ACTION_MOVE:

                Log.d(TAG, "--------move--------------");

                break;
            case MotionEvent.ACTION_UP:

                Log.d(TAG, "--------up--------------");

                break;
            case MotionEvent.ACTION_CANCEL:

                Log.d(TAG, "--------cancle--------------");

                break;
        }
        return false;
    }
}
package guo.com.ontoucheventtest;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.TextView;


/**
 * Created by ${GuoZhaoHui} on 2017/4/28.
 * email:[email protected]
 */

public class TV extends TextView {
   
    private static final String TAG = "TV";
    public TV(Context context) {
        super(context);
    }

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

    public TV(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:

                Log.d(TAG, "--------down--------------");

                break;
            case MotionEvent.ACTION_MOVE:

                Log.d(TAG, "--------move--------------");

                break;
            case MotionEvent.ACTION_UP:

                Log.d(TAG, "--------up--------------");

                break;
            case MotionEvent.ACTION_CANCEL:

                Log.d(TAG, "--------cancle--------------");

                break;
        }
        return true;
    }
}

xml如下

<?xml version="1.0" encoding="utf-8"?>
<guo.com.ontoucheventtest.LL1 xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context="guo.com.ontoucheventtest.MainActivity">

    <guo.com.ontoucheventtest.LL2
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <guo.com.ontoucheventtest.TV
        android:textSize="25sp"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

    </guo.com.ontoucheventtest.LL2>
</guo.com.ontoucheventtest.LL1>

分情况
1:ViewGroup和View的onTouchEvent都是返回false,且并不重写onInterceptTouchEvent
点击view或者ViewGroup,得到如下结果
点击view

/guo.com.ontoucheventtest D/TV: --------down--------------
/guo.com.ontoucheventtest D/LL2: ------
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值