Android中的事件传递:
思路:1.在Activity中重写dispatchTouchEvent事件和onTouchEvent事件
2.定义两个View,View1和View2并分别重写这两个View的dispatchTouchEvent和onTouchEvent
代码:MainActivity类
package com.example.eventtestdemo;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
/**
*
* @author huozhenpeng
* 事件的分发测试分析
*
*
*/
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.e("abc", "activity------dispatch---->down");
break;
case MotionEvent.ACTION_MOVE:
Log.e("abc", "activity-----dispatch----->move");
break;
case MotionEvent.ACTION_UP:
Log.e("abc", "activity-----dispatch----->up");
break;
default:
break;
}
return super.dispatchTouchEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.e("abc", "activity------touch---->down");
break;
case MotionEvent.ACTION_MOVE:
Log.e("abc", "activity-----touch----->move");
break;
case MotionEvent.ACTION_UP:
Log.e("abc", "activity-----touch----->up");
break;
default:
break;
}
return super.onTouchEvent(event);
}
}
View1
package com.example.eventtestdemo;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
public class View1 extends View{
public View1(Context context, AttributeSet attrs, int defStyleAttr,
int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public View1(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public View1(Context context, AttributeSet attrs) {
super(context, attrs);
}
public View1(Context context) {
super(context);
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.e("abc", "View1------dispatch---->down");
break;
case MotionEvent.ACTION_MOVE:
Log.e("abc", "View1-----dispatch----->move");
break;
case MotionEvent.ACTION_UP:
Log.e("abc", "View1-----dispatch----->up");
break;
default:
break;
}
return super.dispatchTouchEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.e("abc", "View1------touch---->down");
break;
case MotionEvent.ACTION_MOVE:
Log.e("abc", "View1-----touch----->move");
break;
case MotionEvent.ACTION_UP:
Log.e("abc", "View1-----touch----->up");
break;
default:
break;
}
return super.onTouchEvent(event);
}
}
View2
package com.example.eventtestdemo;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
public class View2 extends View{
public View2(Context context, AttributeSet attrs, int defStyleAttr,
int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public View2(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public View2(Context context, AttributeSet attrs) {
super(context, attrs);
}
public View2(Context context) {
super(context);
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.e("abc", "View2------dispatch---->down");
break;
case MotionEvent.ACTION_MOVE:
Log.e("abc", "View2-----dispatch----->move");
break;
case MotionEvent.ACTION_UP:
Log.e("abc", "View2-----dispatch----->up");
break;
default:
break;
}
return super.dispatchTouchEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.e("abc", "View2------touch---->down");
break;
case MotionEvent.ACTION_MOVE:
Log.e("abc", "View2-----touch----->move");
break;
case MotionEvent.ACTION_UP:
Log.e("abc", "View2-----touch----->up");
break;
default:
break;
}
return super.onTouchEvent(event);
}
}
布局文件:
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >
<com.example.eventtestdemo.View1
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#00ff00"
/>
<com.example.eventtestdemo.View2
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#ff0000"
/>
</LinearLayout>
以上是所有的代码,特别简单。
测试:
1.上面的代码不做任何修改,就是说View1和View2均没有处理事件,点击View1时打印结果:
可以看到事件都是先经过Activity的,如果View1没有处理down事件,那么move事件和up事件将不会到达View1。
2.修改View1的代码
private boolean flag;
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.e("abc", "View1------touch---->down");
flag=true;
break;
case MotionEvent.ACTION_MOVE:
Log.e("abc", "View1-----touch----->move");
flag=false;
break;
case MotionEvent.ACTION_UP:
Log.e("abc", "View1-----touch----->up");
flag=false;
break;
default:
break;
}
return flag;
}
修改内容:View1处理了down事件,但是没有处理move和up事件。
运行结果:
可以看到:1.如果View1处理了down事件,那么后续还会接到move事件和up事件。
2.如果View1处理了down事件,那么Activity将不会接受到down事件。
3.View1接受到了move事件和up事件,但是没有处理,那么Activity将会接受到move事件和up事件。
那么存不存在这种情况,View1不处理down事件,只处理move事件和up事件呢,事实上这种情况是无效的,因为一旦View1不处理down事件,那么View1将不会再收到后续事件。
验证:
private boolean flag;
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.e("abc", "View1------touch---->down");
flag=false;
break;
case MotionEvent.ACTION_MOVE:
Log.e("abc", "View1-----touch----->move");
flag=true;
break;
case MotionEvent.ACTION_UP:
Log.e("abc", "View1-----touch----->up");
flag=true;
break;
default:
break;
}
return flag;
}
可以看到View1没有处理down事件,那么Activity就接收到了down事件,但是View1将不会再收到move事件和up事件。
3.如果View1处理了down事件和move事件和up事件呢
private boolean flag;
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.e("abc", "View1------touch---->down");
flag=true;
break;
case MotionEvent.ACTION_MOVE:
Log.e("abc", "View1-----touch----->move");
flag=true;
break;
case MotionEvent.ACTION_UP:
Log.e("abc", "View1-----touch----->up");
flag=true;
break;
default:
break;
}
return flag;
}
运行结果:
可以看到View1如果处理了move和up事件,那么activity将不会收到move事件和up事件(指的是onTouchEvent中)
4.View1处理了down和up事件,没有处理move事件呢
private boolean flag;
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.e("abc", "View1------touch---->down");
flag=true;
break;
case MotionEvent.ACTION_MOVE:
Log.e("abc", "View1-----touch----->move");
flag=false;
break;
case MotionEvent.ACTION_UP:
Log.e("abc", "View1-----touch----->up");
flag=true;
break;
default:
break;
}
return flag;
}
运行结果:
可以看到activity没有收到down和up事件,但是收到了move事件。
那么
requestDisallowInterceptTouchEvent这个方法对事件的传递有什么影响呢。