Skip to content

Commit 373ba11

Browse files
committed
Fixing double click event handling: making isBusy variable outer of mHandler.postDelayed. Now we can truly receive double click events.; Making DOUBLE_CLICK_INTERVAL as a static field and adjusting his value to best fit double click sense. Some Google checkstyle was applied.
1 parent fd6ad40 commit 373ba11

File tree

2 files changed

+73
-42
lines changed

2 files changed

+73
-42
lines changed

app/src/main/java/com/pedromassango/doubleclick/DoubleClick.java

Lines changed: 61 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,76 @@
44
import android.view.View;
55

66
/**
7-
* Created by pedromassango on 12/20/17.
7+
* @author pedromassango on 12/20/17.
8+
*
9+
* @contributing to development samirtf
10+
* Interface to be notified from a new click event.
11+
*
812
*/
9-
1013
public class DoubleClick implements View.OnClickListener {
1114

12-
private int clicks;
13-
private boolean isBussy = false;
14-
private final DoubleClickListener doubleClickListener;
15-
Handler mHandler = new Handler();
16-
public DoubleClick( DoubleClickListener doubleClickListener) {
17-
this.doubleClickListener = doubleClickListener;
18-
}
15+
/*
16+
* Duration of click interval.
17+
* 200 milliseconds is a best fit to double click interval.
18+
*/
19+
private static final long DOUBLE_CLICK_INTERVAL = 200L; // Time to wait the second click.
20+
21+
/*
22+
* Handler to process click event.
23+
*/
24+
final Handler mHandler = new Handler();
25+
26+
/*
27+
* Click callback.
28+
*/
29+
private final DoubleClickListener doubleClickListener;
30+
31+
/*
32+
* Number of clicks in @DOUBLE_CLICK_INTERVAL interval.
33+
*/
34+
private int clicks;
1935

20-
@Override
21-
public void onClick( final View v) {
36+
/*
37+
* Flag to check if click handler is busy.
38+
*/
39+
private boolean isBusy = false;
2240

23-
if(!isBussy) {
24-
// Prevent multiple click in this short time
25-
isBussy = true;
41+
/**
42+
* Builds a DoubleClick.
43+
*
44+
* @param doubleClickListener
45+
*/
46+
public DoubleClick(final DoubleClickListener doubleClickListener) {
47+
this.doubleClickListener = doubleClickListener;
48+
}
2649

27-
// Increase clicks count
28-
this.clicks++;
29-
long DOUBLE_CLICK_INTERVAL = 250L; // Time to wait the second click.
30-
mHandler.postDelayed(new Runnable() {
31-
public final void run() {
50+
@Override
51+
public void onClick(final View view) {
3252

33-
if (clicks >= 2) { // Double tap.
34-
doubleClickListener.onDoubleClick(v);
35-
}
53+
if (!isBusy) {
54+
// Prevent multiple click in this short time
55+
isBusy = true;
3656

37-
if (clicks == 1) { // Single tap
38-
doubleClickListener.onSingleClick(v);
39-
}
57+
// Increase clicks count
58+
clicks++;
4059

41-
// we need to restore clicks count
42-
clicks = 0;
43-
isBussy = false;
44-
}
45-
}, DOUBLE_CLICK_INTERVAL);
60+
mHandler.postDelayed(new Runnable() {
61+
public final void run() {
62+
63+
if (clicks >= 2) { // Double tap.
64+
doubleClickListener.onDoubleClick(view);
65+
}
66+
67+
if (clicks == 1) { // Single tap
68+
doubleClickListener.onSingleClick(view);
69+
}
70+
71+
// we need to restore clicks count
72+
clicks = 0;
4673
}
74+
}, DOUBLE_CLICK_INTERVAL);
75+
isBusy = false;
4776
}
77+
78+
}
4879
}

app/src/main/java/com/pedromassango/doubleclick/DoubleClickListener.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@
44

55

66
/**
7-
* Created by pedromassango on 12/20/17.
8-
*/
9-
/*
10-
Interface to be notified from a new click event.
7+
* @author pedromassango on 12/20/17.
8+
* @contributing to development samirtf
9+
*
10+
* Interface to be notified from a new click event.
1111
*/
1212
public interface DoubleClickListener {
1313

14-
/*
15-
Called when the user make a single click.
16-
*/
17-
void onSingleClick( View view);
14+
/**
15+
* Called when the user make a single click.
16+
*/
17+
void onSingleClick(final View view);
1818

19-
/*
20-
Called when the user make a double click.
21-
*/
22-
void onDoubleClick( View view);
19+
/**
20+
* Called when the user make a double click.
21+
*/
22+
void onDoubleClick(final View view);
2323
}

0 commit comments

Comments
 (0)