刚刚翻出了多年前的笔记,是我实战测试View事件分发的结果。

主要测试dispatchTouchEvent、onInterceptTouchEvent和onTouchEvent方法调用流程。

为了时刻拿出来复习,决定发出来,与大家分享。

demo附件:DespatchTouchDemo.rar

源码

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<com.example.despatchtouchdemo.MLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.example.despatchtouchdemo.MTextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="match_parent text"
        android:textSize="30dp" />

</com.example.despatchtouchdemo.MLinearLayout>

MainActivity.java

package com.example.despatchtouchdemo;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
import android.widget.ViewSwitcher.ViewFactory;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    
    public void textClick(View v){
        Toast.makeText(this, "textClick", 1).show();
    }
    
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        // TODO Auto-generated method stub
        Log.i("i", "MainActivity ---> dispatchTouchEvent = super; --->" + Util.getActionString(ev));
        return super.dispatchTouchEvent(ev);
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        Log.i("i", "MainActivity ---> onTouchEvent = super; --->" + Util.getActionString(event));
        return super.onTouchEvent(event);
    }
}

MLinearLayout.java

 package com.example.despatchtouchdemo;

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

public class MLinearLayout extends LinearLayout {

    public MLinearLayout(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    
    public MLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        // TODO Auto-generated method stub
        Log.i("i", "MLinearLayout ---> dispatchTouchEvent = super; --->" + Util.getActionString(ev));
        return super.dispatchTouchEvent(ev);
    }
    
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        // TODO Auto-generated method stub
        Log.i("i", "MLinearLayout ---> onInterceptTouchEvent = false; --->" + Util.getActionString(ev));
//        return super.onInterceptTouchEvent(ev);
        return false;
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        Log.i("i", "MLinearLayout ---> onTouchEvent = true; --->" + Util.getActionString(event));
//        return super.onTouchEvent(event);
        return true;
    }
    
    
    
}

MTextView.java

 package com.example.despatchtouchdemo;

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

public class MTextView extends TextView {

    public MTextView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }
    
    public MTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public MTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        Log.i("i", "MTextView ---> dispatchTouchEvent = super; --->" + Util.getActionString(event));
        return super.dispatchTouchEvent(event);
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        Log.i("i", "MTextView ---> onTouchEvent = false; --->" + Util.getActionString(event));
//        return super.onTouchEvent(event);
        return false;
    }

}

Util.java

 package com.example.despatchtouchdemo;

import android.view.MotionEvent;

public class Util {
    
    public static String getActionString(MotionEvent ev){
        String action = "";
        switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            action = "ACTION_DOWN";
            break;
        case MotionEvent.ACTION_MOVE:
            action = "ACTION_MOVE";
            break;
        case MotionEvent.ACTION_UP:
            action = "ACTION_UP";
            break;
        default:
            break;
        }
        
        return action;
    }

}

测试结果

为了指代方便,下面将MLinearLayout简称为L,将MTextView简称为 T,L.onInterceptTouchEvent=true 表示的含义为MLinearLayout中的onInterceptTouchEvent方法返回值为true,通过程序运行时输出的Log来说明调用 时序。

所有方法的返回值都返回super,结果如下:

第1种情况 L.onInterceptTouchEvent=false&& L.onTouchEvent=true &&T.onTouchEvent=true 输出下面的Log

第2种情况 L.onInterceptTouchEvent=false&& L.onTouchEvent=true &&T.onTouchEvent=false 输出下面的Log:

第3种情况 L.onInterceptTouchEvent=true&& L.onTouchEvent=true 输出下面的Log:

第4种情况 L.onInterceptTouchEvent=true&& L.onTouchEvent=false 输出下面的Log:

(标签:android、View、事件分发)