关于View的事件分发,这篇博客 讲的很清楚,大致总结下事件的流程
View.dispatchEvent->View.setOnTouchListener->View.onTouchEvent
在dispatchTouchEvent中会进行OnTouchListener的判断,如果这个view处于enable状态,OnTouchListener不为null且返回true,则表示事件被消费,onTouchEvent不会被执行;否则执行onTouchEvent。
在onTouchEvent中,如果return true就说明事件被消费了,当然如果这个View可以被点击(可以在代码中设置view.setonClickListener),或者长按(可以设置view.setonLongClickListener),也可以说明事件被消费
而他的后续博客也对ViewGroup的源码分析的很清楚,但是看了源码还是自己动手验证下嘛,因为我就写一写ViewGroup的事件的demo
package com.example.user.swipeview_01;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.LinearLayout;
/**
* Created by USER on 2015/9/30.
*/
public class MyLinearLayout extends LinearLayout {
private static final String TAG = "zhou";
public MyLinearLayout(Context context) {
super(context);
}
public MyLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyLinearLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
Log.d(TAG,"MyLinearLayout -> dispatchTouchEvent");
return super.dispatchTouchEvent(ev);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
Log.d(TAG,"MyLinearLayout -> onInterceptTouchEvent");
return super.onInterceptTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
switch (action){
case MotionEvent.ACTION_DOWN:
Log.d(TAG,"MyLinearLayout -> onTouchEvent->ACTION_DOWN");
break;
case MotionEvent.ACTION_MOVE:
Log.d(TAG,"MyLinearLayout -> onTouchEvent->ACTION_MOVE");
break;
case MotionEvent.ACTION_UP:
Log.d(TAG,"MyLinearLayout -> onTouchEvent->ACTION_UP");
break;
}
return super.onTouchEvent(event);
}
}
package com.example.user.swipeview_01;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
/**
* Created by USER on 2015/9/30.
*/
public class MyView extends View {
private static final String TAG ="zhou";
public MyView(Context context) {
super(context);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
Log.d(TAG,"MyView->dispatchTouchEvent");
return super.dispatchTouchEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
switch(action){
case MotionEvent.ACTION_DOWN:
Log.d(TAG,"MyView->onTouchEvent->ACTION_DOWN");
break;
case MotionEvent.ACTION_MOVE:
Log.d(TAG,"MyView->onTouchEvent->ACTION_MOVE");
break;
case MotionEvent.ACTION_UP:
Log.d(TAG,"MyView->onTouchEvent->ACTION_UP");
break;
}
return super.onTouchEvent(event);
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <com.example.user.swipeview_01.MyLinearLayout android:background="#eeee5046" android:layout_width="match_parent" android:layout_height="match_parent"> <com.example.user.swipeview_01.MyView android:background="#ee93ee74" android:layout_width="300dp" android:layout_height="200dp" /> </com.example.user.swipeview_01.MyLinearLayout> </LinearLayout>
09-30 18:12:03.485 14514-14514/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> dispatchTouchEvent
09-30 18:12:03.485 14514-14514/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> onInterceptTouchEvent
09-30 18:12:03.486 14514-14514/com.example.user.swipeview_01 D/zhou﹕ MyView->dispatchTouchEvent
09-30 18:12:03.489 14514-14514/com.example.user.swipeview_01 D/zhou﹕ MyView->onTouchEvent->ACTION_DOWN
09-30 18:12:03.493 14514-14514/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> onTouchEvent->ACTION_DOWN
09-30 18:18:30.736 16701-16701/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> dispatchTouchEvent
09-30 18:18:30.736 16701-16701/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> onInterceptTouchEvent
09-30 18:18:30.737 16701-16701/com.example.user.swipeview_01 D/zhou﹕ MyView->dispatchTouchEvent
09-30 18:18:30.741 16701-16701/com.example.user.swipeview_01 D/zhou﹕ MyView->onTouchEvent->ACTION_DOWN
09-30 18:18:30.764 16701-16701/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> dispatchTouchEvent
09-30 18:18:30.764 16701-16701/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> onInterceptTouchEvent
09-30 18:18:30.765 16701-16701/com.example.user.swipeview_01 D/zhou﹕ MyView->dispatchTouchEvent
09-30 18:18:30.765 16701-16701/com.example.user.swipeview_01 D/zhou﹕ MyView->onTouchEvent->ACTION_MOVE
09-30 18:18:30.784 16701-16701/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> dispatchTouchEvent
09-30 18:18:30.784 16701-16701/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> onInterceptTouchEvent
09-30 18:18:30.784 16701-16701/com.example.user.swipeview_01 D/zhou﹕ MyView->dispatchTouchEvent
09-30 18:18:30.785 16701-16701/com.example.user.swipeview_01 D/zhou﹕ MyView->onTouchEvent->ACTION_MOVE
09-30 18:18:30.793 16701-16701/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> dispatchTouchEvent
09-30 18:18:30.793 16701-16701/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> onInterceptTouchEvent
09-30 18:18:30.793 16701-16701/com.example.user.swipeview_01 D/zhou﹕ MyView->dispatchTouchEvent
09-30 18:18:30.796 16701-16701/com.example.user.swipeview_01 D/zhou﹕ MyView->onTouchEvent->ACTION_UP
10-08 09:52:19.038 5891-5891/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> dispatchTouchEvent
10-08 09:52:19.039 5891-5891/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> onInterceptTouchEvent
10-08 09:52:19.041 5891-5891/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> onTouchEvent->ACTION_DOWN
10-08 09:52:20.009 5891-5891/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> dispatchTouchEvent
10-08 09:52:20.009 5891-5891/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> onInterceptTouchEvent
10-08 09:52:20.011 5891-5891/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> onTouchEvent->ACTION_DOWN
10-08 09:52:20.698 5891-5891/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> dispatchTouchEvent
10-08 09:52:20.699 5891-5891/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> onInterceptTouchEvent
10-08 09:52:20.702 5891-5891/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> onTouchEvent->ACTION_DOWN
10-08 09:52:21.544 5891-5891/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> dispatchTouchEvent
10-08 09:52:21.544 5891-5891/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> onInterceptTouchEvent
10-08 09:52:21.546 5891-5891/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> onTouchEvent->ACTION_DOWN
10-08 09:54:51.304 5891-5891/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> dispatchTouchEvent
10-08 09:54:51.305 5891-5891/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> onInterceptTouchEvent
10-08 09:54:51.307 5891-5891/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> onTouchEvent->ACTION_DOWN
10-08 09:58:18.037 7250-7250/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> dispatchTouchEvent
10-08 09:58:18.037 7250-7250/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> onInterceptTouchEvent
10-08 09:58:18.040 7250-7250/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> onTouchEvent->ACTION_DOWN
10-08 09:58:18.054 7250-7250/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> dispatchTouchEvent
10-08 09:58:18.056 7250-7250/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> onTouchEvent->ACTION_MOVE
10-08 09:58:18.071 7250-7250/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> dispatchTouchEvent
10-08 09:58:18.071 7250-7250/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> onTouchEvent->ACTION_MOVE
10-08 09:58:18.091 7250-7250/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> dispatchTouchEvent
10-08 09:58:18.091 7250-7250/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> onTouchEvent->ACTION_MOVE
10-08 09:58:18.108 7250-7250/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> dispatchTouchEvent
10-08 09:58:18.109 7250-7250/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> onTouchEvent->ACTION_MOVE
10-08 09:58:18.127 7250-7250/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> dispatchTouchEvent
10-08 09:58:18.127 7250-7250/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> onTouchEvent->ACTION_MOVE
10-08 09:58:18.147 7250-7250/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> dispatchTouchEvent
10-08 09:58:18.148 7250-7250/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> onTouchEvent->ACTION_MOVE
10-08 09:58:18.164 7250-7250/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> dispatchTouchEvent
10-08 09:58:18.165 7250-7250/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> onTouchEvent->ACTION_MOVE
10-08 09:58:18.184 7250-7250/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> dispatchTouchEvent
10-08 09:58:18.184 7250-7250/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> onTouchEvent->ACTION_MOVE
10-08 09:58:18.195 7250-7250/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> dispatchTouchEvent
10-08 09:58:18.195 7250-7250/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> onTouchEvent->ACTION_MOVE
10-08 09:58:18.204 7250-7250/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> dispatchTouchEvent
10-08 09:58:18.207 7250-7250/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> onTouchEvent->ACTION_UP
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
Log.d(TAG,"MyLinearLayout -> onInterceptTouchEvent");
return super.onInterceptTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
switch (action){
case MotionEvent.ACTION_DOWN:
Log.d(TAG,"MyLinearLayout -> onTouchEvent->ACTION_DOWN");
return true;
case MotionEvent.ACTION_MOVE:
Log.d(TAG,"MyLinearLayout -> onTouchEvent->ACTION_MOVE");
break;
case MotionEvent.ACTION_UP:
Log.d(TAG,"MyLinearLayout -> onTouchEvent->ACTION_UP");
break;
}
return super.onTouchEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
switch(action){
case MotionEvent.ACTION_DOWN:
Log.d(TAG,"MyView->onTouchEvent->ACTION_DOWN");
return super.onTouchEvent(event);
//break;
case MotionEvent.ACTION_MOVE:
Log.d(TAG,"MyView->onTouchEvent->ACTION_MOVE");
break;
case MotionEvent.ACTION_UP:
Log.d(TAG,"MyView->onTouchEvent->ACTION_UP");
break;
}
//return super.onTouchEvent(event);
return true;
}
10-08 10:06:23.156 9343-9343/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> dispatchTouchEvent
10-08 10:06:23.157 9343-9343/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> onInterceptTouchEvent
10-08 10:06:23.157 9343-9343/com.example.user.swipeview_01 D/zhou﹕ MyView->dispatchTouchEvent
10-08 10:06:23.162 9343-9343/com.example.user.swipeview_01 D/zhou﹕ MyView->onTouchEvent->ACTION_DOWN
10-08 10:06:23.164 9343-9343/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> onTouchEvent->ACTION_DOWN
10-08 10:06:23.178 9343-9343/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> dispatchTouchEvent
10-08 10:06:23.179 9343-9343/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> onTouchEvent->ACTION_MOVE
10-08 10:06:23.199 9343-9343/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> dispatchTouchEvent
10-08 10:06:23.200 9343-9343/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> onTouchEvent->ACTION_MOVE
10-08 10:06:23.218 9343-9343/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> dispatchTouchEvent
10-08 10:06:23.218 9343-9343/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> onTouchEvent->ACTION_MOVE
10-08 10:06:23.229 9343-9343/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> dispatchTouchEvent
10-08 10:06:23.229 9343-9343/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> onTouchEvent->ACTION_MOVE
10-08 10:06:23.236 9343-9343/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> dispatchTouchEvent
10-08 10:06:23.239 9343-9343/com.example.user.swipeview_01 D/zhou﹕ MyLinearLayout -> onTouchEvent->ACTION_UP