Gesture detector on custom view
I have defined a class that extends GestureDetector.SimpleOnGestureListener
and overrides methods onDown
andonFling
class MyGestureDetector extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return super.onDown(e);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
Toast.makeText(PersianDatePicker.this.getContext(),
"fling " + velocityX + " - " + velocityY, Toast.LENGTH_LONG)
.show();
return super.onFling(e1, e2, velocityX, velocityY);
}
}
And I also defined an instance GestureDetectorCompat
@Override
public boolean onTouchEvent(MotionEvent event) {
mDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
I've tested it in action and it works well. I want to use it in a custom way, but it doesn't work! And I have no errors.
+3
source to share
1 answer
In your custom view, try this:
@Override
public boolean onTouchEvent(MotionEvent event) {
if (mGestureDetector.onTouchEvent(event)) {
return true;
}
return super.onTouchEvent(event);
}
or, in more detail:
@Override
public boolean onTouchEvent(MotionEvent event) {
return mGestureDetector.onTouchEvent(event) || super.onTouchEvent(event);
}
+3
source to share