-
Notifications
You must be signed in to change notification settings - Fork 239
Expand file tree
/
Copy pathScrollHelper.java
More file actions
91 lines (69 loc) · 2.55 KB
/
ScrollHelper.java
File metadata and controls
91 lines (69 loc) · 2.55 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package com.cleveroad.adaptivetablelayout;
import android.content.Context;
import androidx.annotation.Nullable;
import androidx.core.view.GestureDetectorCompat;
import android.view.GestureDetector;
import android.view.MotionEvent;
class ScrollHelper implements GestureDetector.OnGestureListener {
/**
* Gesture detector -> Scroll, Fling, Tap, LongPress, ...
* Using when user need to scroll table
*/
private final GestureDetectorCompat mGestureDetectorCompat;
@Nullable
private ScrollHelperListener mListener;
ScrollHelper(Context context) {
mGestureDetectorCompat = new GestureDetectorCompat(context, this);
mGestureDetectorCompat.setIsLongpressEnabled(true);
}
void setListener(@Nullable ScrollHelperListener listener) {
mListener = listener;
}
@Override
public boolean onDown(MotionEvent e) {
// catch down action
return mListener == null || mListener.onDown(e);
}
@Override
public void onShowPress(MotionEvent e) {
// nothing to do
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
// catch click action
return mListener != null && mListener.onSingleTapUp(e);
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
// catch scroll action
return mListener != null && mListener.onScroll(e1, e2, distanceX, distanceY);
}
@Override
public void onLongPress(MotionEvent e) {
// catch long click action
if (mListener != null) {
mListener.onLongPress(e);
}
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
// catch fling action
return mListener == null || mListener.onFling(e1, e2, velocityX, velocityY);
}
boolean onTouch(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP && mListener != null) {
// stop drag and drop mode
mListener.onActionUp(event);
}
// connect GestureDetector with our touch events
return mGestureDetectorCompat.onTouchEvent(event);
}
interface ScrollHelperListener {
boolean onDown(MotionEvent e);
boolean onSingleTapUp(MotionEvent e);
void onLongPress(MotionEvent e);
boolean onActionUp(MotionEvent e);
boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY);
boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY);
}
}