Fling/Swipe Gestures not detected
I am implementing Gesture detection for swipe/fling movements using a inner class SimpleOnGestureListener
. However no gestures are detected.
The xml layout has a parent ScrollView
so I a wondering if this is a potential cause.
In onCreate
I initialise the GestureListener
:
public class ViewSelectedDive extends Activity implements OnClickListener,
OnRatingBarChangeListener {...
public GestureDetectorCompat gestureDetectorObject;
protected void onCreate(Bundle savedInstanceState) {..
// instantiate gesture detector
gestureDetectorObject = new GestureDetectorCompat(this, new GestureListener());
Override the onTouchEvent
method to ensure that the gesture listener is used when a touch events is detected.
@Override
public boolean onTouchEvent(MotionEvent event) {
// tell teh activity tto use gerture listener when touch event is detected
gestureDetectorObject.onTouchEvent(event);
Log.d(TAG, "312 ON TOUCH EVENT");
return super.onTouchEvent(event);
}
// inner class to handle guerter detection
public class GestureListener extends GestureDetector.SimpleOnGestureListener{
private float flingMin=100;
private float velocityMin = 100;
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
Log.d(TAG, "ON FLING INNER CLASS 951");
// user move to next dive in db
boolean moveToNextDive=false;
boolean moveToPreviousDive=false;
// caulate the change in x pos within fling ges
float horizontalDiff = e2.getX()-e1.getX();
float verticalDiff = e2.getY() - e1.getY();
// calulate the abs values
float absHDiff = Math.abs(horizontalDiff);
float absVDiff = Math.abs(verticalDiff);
float absVelocityX = Math.abs(velocityX);
float absVelocityY = Math.abs(velocityY);
// now of hor distance > vertival distance , move back or forward
if(absHDiff>absVDiff && absHDiff>flingMin && absVelocityX> velocityMin){
// swiping forwad
if(horizontalDiff>0) {
moveToPreviousDive=true;
Toast.makeText(getApplicationContext(), "BACKWARD GESTURE DETECTED", Toast.LENGTH_LONG).show();
Log.d(TAG, "SWIPE HORIZONTAL DETECTED BACKWARD");
}else{
moveToNextDive=true;
Toast.makeText(getApplicationContext(), "FORWARD GESTURE DETECTED", Toast.LENGTH_LONG).show();
Log.d(TAG, "SWIPE HORIZONTAL FORWAD DETECTED BACKWARD");
}
}// outer ifelse if(absVDiff>flingMin && absVelocityY>velocityMin){
else if(absVDiff>flingMin && absVelocityY>velocityMin){
// vertical swipe detected
if(verticalDiff>0) {
moveToPreviousDive=true;
Toast.makeText(getApplicationContext(), "VERTICAL BACKWARD GESTURE DETECTED", Toast.LENGTH_LONG).show();
Log.d(TAG, "SWIPE VERTICAL DETECTED BACKWARD");
}
else{
moveToNextDive=true;
Log.d(TAG, "SWIPE VERTICAL FORWARD DETECTED BACKWARD");
Toast.makeText(getApplicationContext(), "VERTICAL FORWARD GESTURE DETECTED", Toast.LENGTH_LONG).show();
}
}
return true;
}
@Override
public boolean onDown(MotionEvent e) {
// Returning true tells the operating system that your code
// is interested in the remaining gesture events.
Log.d(TAG, "ON DOWN INNER CLASS 1010");
return true;
}
得到它,滚动视图将注册所有的触摸事件,所以你需要通过重写活动disptachTouchEvent将它们发送到手势探测器对象...希望这可以帮助任何人在相同的情况..
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
// must use this to pass event on to Gesture object from scrollvoew n xml
super.dispatchTouchEvent(ev);
return this.gestureDetectorObject.onTouchEvent(ev);
}
链接地址: http://www.djcxy.com/p/91258.html
上一篇: ListView Fling不起作用
下一篇: 没有检测到Fling / Swipe手势