为什么没有检测到Fling手势

时间:2014-06-13 15:35:44

标签: android gesture gesture-recognition gestures

我试图通过扩展onFling来检测SimpleOnGestureListener手势。我也检查了网上的一些代码,知道我错过了什么,但我没有注意到任何差异。问题出在onFlingonDown上。请让我知道我错过了什么。

Code_Java:

private boolean setTouchableInfoDialog(LinearLayout infoReportContainer) {
        // TODO Auto-generated method stub
        Log.i(TAG, "@setTouchableInfoDialog(): Inside setTouchableInfoDialog()");

        if (infoReportContainer == null) {
            Log.i(TAG, "@setTouchableInfoDialog(): LinearLayout infoReportContainer Is NULL");
            return false;
        }else {
            final GestureDetector mGestiredetector = new GestureDetector(getApplicationContext(), new mSwipeGestureDetector());
            infoReportContainer.setOnTouchListener(new OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    // TODO Auto-generated method stub
                    Log.i(TAG, "@setTouchableInfoDialog()->onTouch(): Binding the LinearLayout infoReportContainer"
                            + " To the GestureDetector");
                    return mGestiredetector.onTouchEvent(event);
                }
            });
        }
        return true;

    }

class mSwipeGestureDetector extends SimpleOnGestureListener {

    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_THRESHOLD_VELOCITY = 150;

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        // TODO Auto-generated method stub
                    //Added Upon Update
                    Log.i(TAG, "@onFling(): Fling Gesture Detected");
        try {
            if (e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
                Log.i("@onFling(): ", "UpToDown Swipe");
                return true;
            }else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
                Log.i("@onFling(): ", "DownToUp Swipe");
                return true;
            }
        }catch (Exception e){

        }
        //return super.onFling(e1, e2, velocityX, velocityY);
        return false;
    }

1 个答案:

答案 0 :(得分:0)

我认为我的错误与我从onDown()返回的内容有关。在onDown()之前调用onFling(),如果onDown返回false,则永远不会调用onFling

我刚刚将onDown的返回值更改为true。并且onFling正在运作。

我指的是this question