I want to detect fling-gestures on a RelativeLayout called belowContainer, my code looks like this:
// Create a new GestureListener
GestureDetector.SimpleOnGestureListener gestureListener = new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2,
float velocityX, float velocityY) 开发者_开发问答{
Log.d("test", "velocityx=" + velocityX);
return true;
}
};
// Setup a new GestureDetector, attach the custom GestureListener
final GestureDetector gestureDetector = new GestureDetector(this, gestureListener);
// Attach the GestureDetector to the belowContainer onTouchListener
belowContainer.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
Log.d("test", "clicked!");
if(gestureDetector.onTouchEvent(event)) {
Log.d("test", "gesture detected");
return true;
}
return false;
}
});
My app registers the belowContainer onTouch fine, but somehow the fling gesture does not get detected (the log just shows "clicked!" and nothing else). How come?
Are you testing on a device or in the emulator?
I don't see a problem in your code. Is the belowContainer large enough so you have enough room to make a fling gesture? Do you have any views inside it that may be "intercepting" the touch events?
I advice you to log all other methods that the gesture listener may catch so you know if you are getting other gestures instead.
You can also try to pass the onTouchEvents from the activity and not from your view... This way it would be easier to catch the gesture events (just to make sure everything is working!)
(Override Activity.onTouchEvent
and pass the event to the gesture detector.
精彩评论