未检测到Fling / Swipe手势

时间:2014-10-07 22:40:28

标签: android gesture-recognition

我正在使用内部类SimpleOnGestureListener实现手势检测以进行滑动/拖动动作。但是没有检测到手势。

xml布局有一个父ScrollView,所以我想知道这是否是一个潜在原因。

onCreate我初始化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());

重写onTouchEvent方法以确保在检测到触摸事件时使用手势侦听器。

@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);
    }

//处理猜测器检测的内部类

    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;
        }

1 个答案:

答案 0 :(得分:0)

知道了,scrollview将注册所有触摸事件,因此您需要通过覆盖活动disptachTouchEvent将它们发送到Gesture探测器对象...希望这可以帮助处于同样情况的任何人。

@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);
    }