-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInsensitiveListView.java
88 lines (69 loc) · 1.7 KB
/
InsensitiveListView.java
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.widget.ListView;
/**
* ListView that isn't sensitive to horizontal scrolling
*
* @author [email protected]
*
*/
public class InsensitiveListView extends ListView {
private static final String TAG = "InsensitiveListView";
private VelocityTracker mVelocityTracker;
private float mDownX;
private int mViewWidth = 1;
private float mDownY;
public InsensitiveListView(Context context) {
super(context);
init();
}
public InsensitiveListView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public InsensitiveListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (mViewWidth < 2) {
mViewWidth = getWidth();
}
switch (ev.getActionMasked()) {
case MotionEvent.ACTION_DOWN: {
mVelocityTracker = VelocityTracker.obtain();
mVelocityTracker.addMovement(ev);
mDownX = ev.getRawX();
mDownY = ev.getRawY();
break;
}
case MotionEvent.ACTION_UP: {
if (mVelocityTracker == null) {
break;
}
mVelocityTracker.addMovement(ev);
mVelocityTracker.computeCurrentVelocity(1000);
mDownX = 0;
break;
}
case MotionEvent.ACTION_MOVE: {
if (mVelocityTracker == null) {
break;
}
mVelocityTracker.addMovement(ev);
float deltaX = ev.getRawX() - mDownX;
float deltaY = ev.getRawY() - mDownY;
if (Math.abs(deltaY) < Math.abs(deltaX)) {
return false;
}
break;
}
}
return super.onInterceptTouchEvent(ev);
}
}