-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoubleClickListner.txt
57 lines (50 loc) · 1.69 KB
/
DoubleClickListner.txt
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
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
/**
* A simple double-click listener
* Usage:
* // Scenario 1: Setting double click listener for myView
* myView.setOnClickListener(new DoubleClickListener() {
*
* @Override
* public void onDoubleClick() {
* // double-click code that is executed if the user double-taps
* // within a span of 200ms (default).
* }
* });
*
* // Scenario 2: Setting double click listener for myView, specifying a custom double-click span time
* myView.setOnClickListener(new DoubleClickListener(500) {
*
* @Override
* public void onDoubleClick() {
* // double-click code that is executed if the user double-taps
* // within a span of 500ms (default).
* }
* });
*
*/
public abstract class DoubleClickListener implements OnClickListener {
// The time in which the second tap should be done in order to qualify as
// a double click
private static final long DEFAULT_QUALIFICATION_SPAN = 200;
private long doubleClickQualificationSpanInMillis;
private long timestampLastClick;
public DoubleClickListener() {
doubleClickQualificationSpanInMillis = DEFAULT_QUALIFICATION_SPAN;
timestampLastClick = 0;
}
public DoubleClickListener(long doubleClickQualificationSpanInMillis) {
this.doubleClickQualificationSpanInMillis = doubleClickQualificationSpanInMillis;
timestampLastClick = 0;
}
@Override
public void onClick(View v) {
if((SystemClock.elapsedRealtime() - timestampLastClick) < doubleClickQualificationSpanInMillis) {
onDoubleClick();
}
timestampLastClick = SystemClock.elapsedRealtime();
}
public abstract void onDoubleClick();
}