没有检测到Fling / Swipe手势

我正在使用内部类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);
    }

//内部类来处理guerter检测

    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/91257.html

上一篇: Fling/Swipe Gestures not detected

下一篇: OnTouchListener on entire Activity