view的事件分发机制实践

先上代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
public class extends LinearLayout {
private static final String TAG = "CustomView";
public (Context context) {
super(context);
}
public (Context context, AttributeSet attrs) {
super(context, attrs);
}
public (Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public boolean onInterceptTouchEvent(MotionEvent ev) {
boolean intercepted = false;
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.d(TAG, "CustomViewOne onInterceptTouchEvent: ACTION_DOWN");
break;
case MotionEvent.ACTION_MOVE:
Log.d(TAG, "CustomViewOne onInterceptTouchEvent: ACTION_MOVE");
break;
case MotionEvent.ACTION_UP:
Log.d(TAG, "CustomViewOne onInterceptTouchEvent: ACTION_UP");
break;
}
return intercepted;
}
public boolean onTouchEvent(MotionEvent event) {
boolean intercepted = false;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.d(TAG, "CustomViewOne onTouchEvent: ACTION_DOWN");
break;
case MotionEvent.ACTION_MOVE:
Log.d(TAG, "CustomViewOne onTouchEvent: ACTION_MOVE");
break;
case MotionEvent.ACTION_UP:
Log.d(TAG, "CustomViewOne onTouchEvent: ACTION_UP");
break;
}
return intercepted;
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
public class CustomViewTwo extends LinearLayout {
private static final String TAG = "CustomView";
public CustomViewTwo(Context context) {
super(context);
}
public CustomViewTwo(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomViewTwo(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public boolean onInterceptTouchEvent(MotionEvent ev) {
boolean intercepted = false;
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.d(TAG, "CustomViewTwo onInterceptTouchEvent: ACTION_DOWN");
break;
case MotionEvent.ACTION_MOVE:
Log.d(TAG, "CustomViewTwo onInterceptTouchEvent: ACTION_MOVE");
break;
case MotionEvent.ACTION_UP:
Log.d(TAG, "CustomViewTwo onInterceptTouchEvent: ACTION_UP");
break;
}
return intercepted;
}
public boolean onTouchEvent(MotionEvent event) {
boolean intercepted = false;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.d(TAG, "CustomViewTwo onTouchEvent: ACTION_DOWN");
break;
case MotionEvent.ACTION_MOVE:
Log.d(TAG, "CustomViewTwo onTouchEvent: ACTION_MOVE");
break;
case MotionEvent.ACTION_UP:
Log.d(TAG, "CustomViewTwo onTouchEvent: ACTION_UP");
break;
}
return intercepted;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class CustomViewThree extends View {
private static final String TAG = "CustomView";
public CustomViewThree(Context context) {
super(context);
}
public CustomViewThree(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomViewThree(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public boolean onTouchEvent(MotionEvent event) {
boolean intercepted = false;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.d(TAG, "CustomViewThree onTouchEvent: ACTION_DOWN");
break;
case MotionEvent.ACTION_MOVE:
Log.d(TAG, "CustomViewThree onTouchEvent: ACTION_MOVE");
break;
case MotionEvent.ACTION_UP:
Log.d(TAG, "CustomViewThree onTouchEvent: ACTION_UP");
break;
}
return intercepted;
}
}
  • 情形一
View onInterceptTouchEvent onTouchEvent
CustomViewOne false false
CustomViewTwo false false
CustomViewThree false

结果:

1
2
3
4
5
D/CustomView: CustomViewOne onInterceptTouchEvent: ACTION_DOWN
D/CustomView: CustomViewTwo onInterceptTouchEvent: ACTION_DOWN
D/CustomView: CustomViewThree onTouchEvent: ACTION_DOWN
D/CustomView: CustomViewTwo onTouchEvent: ACTION_DOWN
D/CustomView: CustomViewOne onTouchEvent: ACTION_DOWN

  • 情形二
View onInterceptTouchEvent onTouchEvent
CustomViewOne false false
CustomViewTwo false false
CustomViewThree true

结果:

1
2
3
4
5
6
7
8
9
10
D/CustomView: CustomViewOne onInterceptTouchEvent: ACTION_DOWN
D/CustomView: CustomViewTwo onInterceptTouchEvent: ACTION_DOWN
D/CustomView: CustomViewThree onTouchEvent: ACTION_DOWN
D/CustomView: CustomViewOne onInterceptTouchEvent: ACTION_MOVE
D/CustomView: CustomViewTwo onInterceptTouchEvent: ACTION_MOVE
D/CustomView: CustomViewThree onTouchEvent: ACTION_MOVE
...
D/CustomView: CustomViewOne onInterceptTouchEvent: ACTION_UP
D/CustomView: CustomViewTwo onInterceptTouchEvent: ACTION_UP
D/CustomView: CustomViewThree onTouchEvent: ACTION_UP

  • 情形三
View onInterceptTouchEvent onTouchEvent
CustomViewOne false false
CustomViewTwo DOWN:false MOVE:true UP:false false
CustomViewThree true

结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
D/CustomView: CustomViewOne onInterceptTouchEvent: ACTION_DOWN
D/CustomView: CustomViewTwo onInterceptTouchEvent: ACTION_DOWN
D/CustomView: CustomViewThree onTouchEvent: ACTION_DOWN
D/CustomView: CustomViewOne onInterceptTouchEvent: ACTION_MOVE
D/CustomView: CustomViewTwo onInterceptTouchEvent: ACTION_MOVE
D/CustomView: CustomViewOne onInterceptTouchEvent: ACTION_MOVE
D/CustomView: CustomViewTwo onTouchEvent: ACTION_MOVE
D/CustomView: CustomViewOne onInterceptTouchEvent: ACTION_MOVE
D/CustomView: CustomViewTwo onTouchEvent: ACTION_MOVE
D/CustomView: CustomViewOne onInterceptTouchEvent: ACTION_MOVE
D/CustomView: CustomViewTwo onTouchEvent: ACTION_MOVE
D/CustomView: CustomViewOne onInterceptTouchEvent: ACTION_MOVE
D/CustomView: CustomViewTwo onTouchEvent: ACTION_MOVE
D/CustomView: CustomViewOne onInterceptTouchEvent: ACTION_UP
D/CustomView: CustomViewTwo onTouchEvent: ACTION_UP