android - detect gesture on different views
I need to detect different gestures on more then one views. My views need to be able to receive Tap, Double Tap and Drag Events. I tried the Gesture Detector but my implementation shows me only global gesture events and I can't connect these events to a specific view.
in my activity.onCreate:
dthandler = new DoubleTapHandler();
mDetector = new GestureDetector(this,dthandler);
gestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
Log.d("myLog","touch");
mDetector.onTouchEvent(event);
return false;
}
};
in my activity I override the dispatchTouch function:
@Override
public boolean dispatchTouchEvent(MotionEvent me){
this.mDetector.onTouchEvent(me);
return super.dispatchTouchEvent(me);
}
this is how I try to connect the touchevent with my views:
prod.setOnTouchListener(this.gestureListener);
my DoubleTapHandler:
public class DoubleTapHandler implements OnDoubleTapListener, OnGestureListener {
private ProductView relatedView;
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
Log.d("myLog", "onDoubleTapEvent");
Log.d("myLog",""+e.getSource());
return false;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
Log.d("myLog", "onDoubleTap"+relatedView);
return false;
}
@Override
public boolean onSingleTapCon开发者_如何转开发firmed(MotionEvent e) {
Log.d("myLog", "singletap");
return false;
}
}
Anyone has an advice? Thanks!
To make it working, attach gestures directly to each View, and you can have different implementations then.
Crazy thought. Create a global gesture listener on some view that wraps them all. And manually call to dispatchTouchEvent on all of your views. It's tricky but it can work.
Then add onTouchEventListenrer on your views.
And if this not working, than the proper way would be: Implementing gesture listeners by your self(taps and drags shouldn't be that hard) and working with intercept touch events.
精彩评论