首页 > 代码库 > 安卓事件分发机制

安卓事件分发机制

首先,先要区分view和viewgroup,一个是控件,一个是控件的容器(意思是里面可以包含控件)

接下来,熟悉三个方法:

public boolean dispatchTouchEvent (MotionEventev)      //分发TouchEvent

默认时,InterceptTouchEvent处理

当返回true时,自己处理

返回false时,onTouchEvent

public booleanon  InterceptTouchEvent(MotionEvent ev) //拦截TouchEvent

当返回true时,由他自己的OnTouchEvent处理

返回false时,继续分发

public boolean onTouchEvent(MotionEvent ev)                //处理TouchEvent

当onTouchEvent返回true时自己处理

当onTouchEvent返回false,继续传递

默认继续传递

其中view类中有dispatchTouchEvent和onTouchEvent两个方法,ViewGroup继承View,而且还新添了一个onInterceptTouchEvent方法。Activity中也无onInterceptTouchEvent方法,但有另外两种方法。

如图,是默认的传播顺序:

技术分享

布局如下:

<span style="font-family:Microsoft YaHei;"><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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

   <com.example.dispatchtest.MyViewGroup android:id="@+id/myviewgroup"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">
        <com.example.dispatchtest.MyView android:id="@+id/myview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
    </com.example.dispatchtest.MyViewGroup>

</RelativeLayout>
</span>

自定义的viewgroup和view如下:

<span style="font-family:Microsoft YaHei;">package com.example.dispatchtest;

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

public class MyViewGroup extends LinearLayout{

	public MyViewGroup(Context context,AttributeSet attrs) {
		super(context,attrs);  
		// TODO Auto-generated constructor stub
	}
	
	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		// TODO Auto-generated method stub
		System.out.println("MyViewGroup dispatchTouchEvent");
		return super.dispatchTouchEvent(ev);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		System.out.println("MyViewGroup onTouchEvent");
		return super.onTouchEvent(event);
	}
	
	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		// TODO Auto-generated method stub
		System.out.println("MyViewGroup onInterceptTouchEvent");
		return super.onInterceptTouchEvent(ev);
	}

}
</span>

<span style="font-family:Microsoft YaHei;">package com.example.dispatchtest;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

public class MyView extends ImageView{

	public MyView(Context context,AttributeSet attrs) {
		super(context,attrs); 
		// TODO Auto-generated constructor stub
	}
	
	@Override
	public boolean dispatchTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		System.out.println("MyView dispatchTouchEvent");
		return super.dispatchTouchEvent(event);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		System.out.println("MyView onTouchEvent");
		return super.onTouchEvent(event);
	}

}
</span>

mainactivity如下:

<span style="font-family:Microsoft YaHei;">package com.example.dispatchtest;

import android.os.Bundle;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		MyView myView = (MyView) findViewById(R.id.myview);
		myView.setBackgroundResource(R.drawable.ic_launcher);
		myView.setOnClickListener(this);
		findViewById(R.id.myviewgroup).setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		// TODO Auto-generated method stub
		System.out.println("MainActivity dispatchTouchEvent");
		return super.dispatchTouchEvent(ev);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		System.out.println("MainActivity onTouchEvent");
		return super.onTouchEvent(event);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.myviewgroup:
			Toast.makeText(MainActivity.this, "MyViewGroup", Toast.LENGTH_SHORT).show();
			break;
		case R.id.myview:
			Toast.makeText(MainActivity.this, "MyView", Toast.LENGTH_SHORT).show();
			break;

		default:
			break;
		}
	}
}
</span>

这个时候是Toast的内容是MyView,如果把MyViewGroup的onInterceptTouchEvent返回值改成false,那么Toast的值就是MyViwGroup。这是因为onInterceptTouchEvent返回值为true时,触摸事件就不往下传递了,留给MyViewGroup自己处理。

事件的分发是从上往下一层一层(Activity——ViewGroup——View)

事件的处理则是从下往上一级一级传递(View——ViewGroup——Activity)

比如将MyView的onTouchEvent返回值改为false,则MyView不处理触摸事件,由MyViewGroup处理,Toast的内容是MyViewGroup。



安卓事件分发机制