Este es un oyente de doble clic que creé para detectar un doble clic de dos dedos.
Las variables utilizadas:
private GestureDetector gesture;
private View.OnTouchListener gestureListener;
boolean click1 = false;
boolean click2 = false;
long first = 0;
long second = 0;
En los eventos de toque de onCreate()
para registrar la actividad:
gesture = new GestureDetector(getApplicationContext(), new SimpleOnGestureListener(){
public boolean onDown(MotionEvent event) {
return true;
}
});
gestureListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return gesture.onTouchEvent(event);
}
};
Fuera de onCreate()
dentro de la actividad:
@Override
public boolean onTouchEvent(MotionEvent event) {
try {
int action = event.getAction() & MotionEvent.ACTION_MASK;
//capture the event when the user lifts their fingers, not on the down press
//to make sure they're not long pressing
if (action == MotionEvent.ACTION_POINTER_UP) {
//timer to get difference between clicks
Calendar now = Calendar.getInstance();
//detect number of fingers, change to 1 for a single-finger double-click, 3 for a triple-finger double-click, etc.
if (event.getPointerCount() == 2) {
if (!click1) {
//if this is the first click, then there hasn't been a second
//click yet, also record the time
click1 = true;
click2 = false;
first = now.getTimeInMillis();
} else if (click1) {
//if this is the second click, record its time
click2 = true;
second = now.getTimeInMillis();
//if the difference between the 2 clicks is less than 500 ms (1/2 second)
//Math.abs() is used because you need to be able to detect any sequence of clicks, rather than just in pairs of two
//(e.g. click1 could be registered as a second click if the difference between click1 and click2 > 500 but
//click2 and the next click1 is < 500)
if (Math.abs(second-first) < 500) {
//do something!!!!!!
} else if (Math.abs(second-first) >= 500) {
//reset to handle more clicks
click1 = false;
click2 = false;
}
}
}
}
} catch (Exception e){
}
return true;
}
Esta respuesta ¡es increíble! – dowi
He intentado este código (¡gracias!) Y no funcionó, solo recibía ACTION_DOWN eventos. Creo que la función onTouchEvent() debe devolver True (para ACTION_DOWN) o de lo contrario ACTION_UP no se recibirá después. Consulte: http://stackoverflow.com/a/16495363/1277048 – FuzzyAmi