IntercepteClickRelativeLayout.java
2.0 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.xdy.ui.viewgroup;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.RelativeLayout;
/**
* Created by Administrator on 2017/4/5.
*/
public class IntercepteClickRelativeLayout extends RelativeLayout {
public boolean intercept;
public IntercepteClickRelativeLayout(Context context) {
super(context);
}
public IntercepteClickRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public IntercepteClickRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
private int downX;
private int downY;
private int upX;
private int upY;
private static final int DEVIATION = 7;
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return intercept;
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
downX = (int) ev.getX();
downY = (int) ev.getY();
break;
case MotionEvent.ACTION_UP:
upX = (int) ev.getX();
upY = (int) ev.getY();
if (downX == upX && downY == upY && onClickListener != null) {
onClickListener.onClick(this);
} else if (onClickListener != null) {
if (upY < downY + DEVIATION && upY > downY - DEVIATION
&& upX < downX + DEVIATION && upX > downX - DEVIATION
)
onClickListener.onClick(this);
}
break;
}
return super.dispatchTouchEvent(ev);
}
OnClickListener onClickListener;
@Override
public void setOnClickListener(OnClickListener l) {
this.onClickListener = l;
}
public void setIntercept(boolean intercept) {
this.intercept = intercept;
}
}