You should simply extend GestureDetector.SimpleOnGestureListener
in some inner class of your view and override methods you want(double tap, long press etc.) and you can handle these gestures with gesture detector containing this listener.
Example view could look like this:
public class GestureView extends View
{
private GestureDetector mGestureDetector;
//only one constructor for shorter code, in final code all should be presented
public GestureView(Context context, AttributeSet attrs)
{
super(context, attrs);
final MyGestureListener myGestureListener = new MyGestureListener();
mGestureDetector = new GestureDetector(context, myGestureListener);
mGestureDetector.setOnDoubleTapListener(myGestureListener);
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
return mGestureDetector.onTouchEvent(event) || super.onTouchEvent(event);
}
private class MyGestureListener extends GestureDetector.SimpleOnGestureListener
{
@Override
public boolean onDown(MotionEvent event)
{
return true;
}
@Override
public void onLongPress(MotionEvent e)
{
//your code here
}
@Override
public boolean onDoubleTap(MotionEvent e)
{
//your code here return true if event handled
return super.onDoubleTap(e);
}
//this can be taken as click
@Override
public boolean onSingleTapConfirmed(MotionEvent e)
{
//your code here return true if event handled
return super.onSingleTapConfirmed(e);
}
}
}
If you want just to add gesture detector to some other view, you can simply call its onTouch method from onTouchListener like
view.setOnTouchListener(new View.OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
return gestureDetector.onTouchEvent(event);
}
});