I have a custom view that extends FrameLayout. It also uses a GestureDetector
to handle onFling events. The view also contains two buttons and a checkbox, all of which work just fine.
However, I also want to be able to handle a tap or click event on the view itself. The problem is I can't figure out how to let the children buttons have a first go at the event. When I click a button, both the click event of the button and the onSingleTapUp event are triggered. I need the button to essentially consume the click event.
Here's the part of my custom view class that handles the touch events.
@Override
public boolean onInterceptTouchEvent(MotionEvent ev)
{
if(mGestureDetector == null)
mGestureDetector = new GestureDetector(this.getContext(), new MyGestureListener());
return mGestureDetector.onTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent ev)
{
mGestureDetector.onTouchEvent(ev);
return true;
}
class MyGestureListener extends SimpleOnGestureListener
{
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
if( Math.abs(e1.getY() - e2.getY()) > MAX_SWIPE_OFF_PATH)
return false;//probably a scroll or move event
float dx = e1.getX() - e2.getX();
//right to left
if(dx > MIN_SWIPE_LENGTH)
{
showButtons();
return true;
}
//left to right
else if(-dx > MIN_SWIPE_LENGTH)
{
hideButtons();
return true;
}
return false;
}
@Override
public boolean onSingleTapUp(MotionEvent ev)
{
toggleExpanded();
return false;
}
}
How can I rearrange this so that the events will be handled properly. If there's a better way to handle the tap event for the view than with the GestureDetector
please share that as well.