-
Notifications
You must be signed in to change notification settings - Fork 0
/
DoubleClickListener.java
57 lines (46 loc) · 1.49 KB
/
DoubleClickListener.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
package com.example.cosmic.androidappclasses;
import android.os.Handler;
import android.view.View;
import java.util.Timer;
import java.util.TimerTask;
public abstract class DoubleClickListener implements View.OnClickListener {
private Timer timer = null; //at class level;
private int DELAY = 100;
private static final long DOUBLE_CLICK_TIME_DELTA = 200; //milliseconds
long lastClickTime = 0;
@Override
public void onClick(View v) {
long clickTime = System.currentTimeMillis();
if (clickTime - lastClickTime < DOUBLE_CLICK_TIME_DELTA){
processDoubleClickEvent(v);
} else {
processSingleClickEvent(v);
}
lastClickTime = clickTime;
}
public void processSingleClickEvent(final View v){
final Handler handler=new Handler();
final Runnable mRunnable=new Runnable(){
public void run(){
onSingleClick(v); //Do what ever u want on single click
}
};
TimerTask timertask = new TimerTask(){
@Override
public void run(){
handler.post(mRunnable);
}
};
timer = new Timer();
timer.schedule(timertask,DELAY);
}
public void processDoubleClickEvent(View v){
if(timer!=null) {
timer.cancel();
timer.purge();
}
onDoubleClick(v);
}
public abstract void onSingleClick(View v);
public abstract void onDoubleClick(View v);
}