I have tried the following code to differentiate single click and double click. Single click is ok. When I double click the imageview, code inside both the single click and doub
Here's my code, which seem to work.
import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ImageView;
public class MultiTapImageView extends ImageView{
private GestureDetector gestureDetector;
private MultiTapImageViewListener mListener;
public interface MultiTapImageViewListener {
void onDoubleTap();
void onSingleTap();
}
public MultiTapImageView(Context context, AttributeSet attrs) {
super(context, attrs);
gestureDetector = new GestureDetector(context, new GestureListener());
}
public void setDoubleTapListener(MultiTapImageViewListener listener){
mListener = listener;
}
@Override
public boolean onTouchEvent(MotionEvent e) {
return gestureDetector.onTouchEvent(e);
}
private class GestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onDown(MotionEvent e) {
return true;
}
// event when double tap occurs
@Override
public boolean onDoubleTap(MotionEvent e) {
float x = e.getX();
float y = e.getY();
if(mListener != null){
mListener.onDoubleTap();
}
return true;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
if(mListener != null){
mListener.onSingleTap();
}
return true;
}
}
}