Android事件传递(二ViewGroup事件的传递)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/ChaoLi_Chen/article/details/51352066

    经过上一篇的实验,我门只是仅仅对View的事件的传递进行了分析,但是还有一个比较厉害的ViewGroup我们肯定是要说一下的,ViewGroup的二叉视图分析 我们能看到,一边是View一边是ViewGroup,而ViewGroup中又可以添加View和ViewGroup,但是我们都知道,ViewGroup是可以有自己的孩子的,也可以对自己的孩子进行拜访等等,而View就没有这些特性,那么我们就能想象一下它的传递机制到底和View有什么样的不同。下面给大家乘上我自己的实验结果在ViewGroup中消息的传递多了一个方法,就是onInterceptTouchEvent(MotionEvent ev),这个方法有什么用处那?这就和ViewGroup的机制有关了,ViewGroup里面可以存放很多的子View,那么ViewGroup就是通过onInterceptTouchEvent(MotionEvent ev)方法进行消息的拦截,默认的时候onInterceptTouchEvent(MotionEvent ev)分会的是false,也就是不拦截,如果onInterceptTouchEvent(MotionEvent ev)方法中返回true,那么事件就会被父控件进行拦截,不再向子View进行传递。

下面贴出我所做的测试的代码:

XML文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
 >
<com.chaoli_chen.ontouchdemo.MyRelativeLayout 
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:id="@+id/mrll"
    >
      <com.chaoli_chen.ontouchdemo.Mybutton 
        android:id="@+id/bt"
        android:onClick="bt_click"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="实验按钮"
        />
    
    
</com.chaoli_chen.ontouchdemo.MyRelativeLayout>
  
</RelativeLayout>

MainActivity

package com.chaoli_chen.ontouchdemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;

public class MainActivity extends Activity {
	private Button bt;
	private MyRelativeLayout mrll;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		mrll = (MyRelativeLayout) findViewById(R.id.mrll);
		
		bt = (Button) findViewById(R.id.bt);
		
		mrll.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				System.out.println("...mrll...");
			}
		});
		
		mrll.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					System.out.println(".mrll..TouchEvent.ACTION_DOWN...");
					break;
				case MotionEvent.ACTION_UP:
					System.out.println(".mrll..TouchEvent.ACTION_UP...");
					break;
				}
				
				return false;
			}
		});
		
		bt.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					System.out.println(".bt..TouchEvent.ACTION_DOWN...");
					break;
				case MotionEvent.ACTION_UP:
					System.out.println(".bt..TouchEvent.ACTION_UP...");
					break;
				}
				return false;
			}
		});
	}
	public void bt_click(View view) {
		System.out.println("...bt_click...");
	}
	@Override
	public boolean dispatchTouchEvent(MotionEvent event) {
		
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			System.out.println(".main.dispatchTouchEvent.ACTION_DOWN...");
			break;
		case MotionEvent.ACTION_UP:
			System.out.println(".main.dispatchTouchEvent.ACTION_UP...");
			break;
		}
		return super.dispatchTouchEvent(event);
	}
	
	
	
	
	
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			System.out.println(".main.onTouchEvent.ACTION_DOWN...");
			break;
		case MotionEvent.ACTION_UP:
			System.out.println(".main.onTouchEvent.ACTION_UP...");
			break;
		}
		return super.onTouchEvent(event);
	}

}
自定义的Button按钮

package com.chaoli_chen.ontouchdemo;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Button;

public class Mybutton extends Button {

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

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

	public Mybutton(Context context) {
		super(context);
	}
	
	@Override
	public boolean dispatchTouchEvent(MotionEvent event) {
		
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			System.out.println(".button.dispatchTouchEvent.ACTION_DOWN...");
			break;
			
		case MotionEvent.ACTION_UP:
			System.out.println(".button.dispatchTouchEvent.ACTION_UP...");
			break;
		}
		return super.dispatchTouchEvent(event);
	}

	
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {

		switch (event.getAction()) {
			
		case MotionEvent.ACTION_DOWN:
			System.out.println(".button.onTouchEvent.ACTION_DOWN...");
			break;

		case MotionEvent.ACTION_UP:
			System.out.println(".button.onTouchEvent.ACTION_UP...");
			break;
		}
		
		return super.onTouchEvent(event);
	}
}

新建一个MyRelativeLayout类如下复写其中的dispatchTouchEvent(MotionEvent ev)、onInterceptTouchEvent(MotionEvent ev)、onTouchEvent(MotionEvent event)方法

package com.chaoli_chen.ontouchdemo;

import android.content.Context;
import android.text.method.Touch;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.RelativeLayout;

public class MyRelativeLayout extends RelativeLayout {

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

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

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

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {

		switch (ev.getAction()) {
		case MotionEvent.ACTION_DOWN:
			System.out
					.println("..MyRelativeLayout..dispatchTouchEvent..ACTION_DOWN..");
			break;
		case MotionEvent.ACTION_UP:
			System.out
					.println("..MyRelativeLayout..dispatchTouchEvent..ACTION_UP..");
			break;
		}
		return super.dispatchTouchEvent(ev);
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		switch (ev.getAction()) {
		case MotionEvent.ACTION_DOWN:
			System.out
					.println("..MyRelativeLayout..onInterceptTouchEvent..ACTION_DOWN..");
			break;
		case MotionEvent.ACTION_UP:
			System.out
					.println("..MyRelativeLayout..onInterceptTouchEvent..ACTION_UP..");
			break;
		}
		return super.onInterceptTouchEvent(ev);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			System.out
					.println("..MyRelativeLayout..onTouchEvent..ACTION_DOWN..");
			break;
		case MotionEvent.ACTION_UP:
			System.out
					.println("..MyRelativeLayout..onTouchEvent..ACTION_UP..");
			break;
		}
		return super.onTouchEvent(event);
	}

}

结果如下:

由结果分析 我们能够看到,事件还是从Activity中的dispatchTouchEven方法入口,然后在我门自定义的MyRelativeLayout中先是进行事件的分发,紧接着就是执行了是否阻断的方法,当然我门查看onInterceptTouchEvent(MotionEvent ev)方法中默认的super.onInterceptTouchEvent(ev)代码如下:

public boolean onInterceptTouchEvent(MotionEvent ev) {
        return false;
    }

(返回的是false也就是事件默认是不进行拦截的。)

最后事件传递到了给我们自定义的button进行事件的分发,然后传给button的setOnTouchListener(l)方法中。最后如第一张所说的一样,调用了onclick()方法,执行了 onclick事件

有没有发现我的们自定义的MyRelativeLayout并没有响应setOnTouchListener(l)方法,那是因为它把事件传递给了button,并没有进行拦截。

下面我门看一下onInterceptTouchEvent(MotionEvent ev)返回true的情况。

@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		switch (ev.getAction()) {
		case MotionEvent.ACTION_DOWN:
			System.out
					.println("..MyRelativeLayout..onInterceptTouchEvent..ACTION_DOWN..");
			break;
		case MotionEvent.ACTION_UP:
			System.out
					.println("..MyRelativeLayout..onInterceptTouchEvent..ACTION_UP..");
			break;
		}
		return true;
	}

测试结果:


是不是到下载测底的明白了,当我们自定义的MyrelativeLayout进行事件拦截的时候,其内部的子View是不会进行事件的相应的,恰恰是我们自定义的MyrelativeLayout响应了自身的setOnTouchListener(l)方法和setOnclick(l)方法。

到处出我们ViewGroup事件传递测试完成了。


猜你喜欢

转载自blog.csdn.net/ChaoLi_Chen/article/details/51352066