android - 未检测到滑动

标签 android touch swipe gesture gesture-recognition

在下面发布的代码中,我尝试使用 onDownonFling 检测滑动,但是当我运行应用程序时,没有任何显示。

当我按住屏幕并快速将手指拖过屏幕时,我希望看到来自 onFling 的输出,但我什么也没收到。此外,我希望在按下屏幕时看到来自 onDown 的输出,但也没有显示任何内容。

请告诉我我遗漏了什么或者我误解了 onDownonFling 的功能?!

Java代码:

super.onCreate(savedInstanceState);
    setContentView(R.layout.swipe_gesture_activivty);

    mGestureDetector = new GestureDetector(getApplicationContext(), new MySwipeGestureDetector());

            mViewGestureDetector = new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            return mGestureDetector.onTouchEvent(event);
        }
    };
    final TextView view = (TextView) findViewById(R.id.accXLabel);      
}

private class MySwipeGestureDetector extends SimpleOnGestureListener implements OnTouchListener {

    @Override
    public boolean onDown(MotionEvent e) {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), "onDown", Toast.LENGTH_LONG).show();
        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        // TODO Auto-generated method stub
        try {
            if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                return false;
            // right to left swipe
            if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                Toast.makeText(getApplicationContext(), "Left Swipe", Toast.LENGTH_SHORT).show();
            }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                Toast.makeText(getApplicationContext(), "Right Swipe", Toast.LENGTH_SHORT).show();
            }
        } catch (Exception e) {
            // nothing
        }
        return false;

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        return mGestureDetector.onTouchEvent(event);
    }

}

最佳答案

首先,我假设您在 layout.xml 文件中的所有 View 都包含在 framelayout 中。并且您希望在整个 framelayout 中都能检测到您的滑动.如果是这样,请注册您的 framelayoutGestureDetector 类型的对象如下代码所示:

FrameLayout mFL = (FrameLayout) findViewById(R.id.framelayoutID);

    mGestureDetector = new GestureDetector(getApplicationContext(), new MySwipeGestureDetector());
    mFL.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            return mGestureDetector.onTouchEvent(event);
        }
    }); 

关于 simpleOnGestureListener ,这里是如何实现 onFling .它对我有用。:

//define these variables globally:
/* private static final int MIN_DIST = 100;
private static final int MAX_OFF_PATH = 200;
private static final int THRESHOLD_VELOC = 200;*/
...
...
...
@Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        // TODO Auto-generated method stub
        try {
            if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                return false;
            // right to left swipe
            if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                Log.i("SwipeGesture", "Left Swipe");
            }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                Log.i("SwipeGesture", "Right Swipe");
            }
        } catch (Exception e) {
            // nothing
        }
        return false;
    }       
}

关于android - 未检测到滑动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23858204/

相关文章:

android - Parse.com 禁用推送通知通知

java - 正则表达式和 RSS 源

javascript - 如何在 openlayers 3 中的图标周围添加可点击的边距?

java - 如何使用幻灯片在布局(如 ICS+ 计算器之类)之间切换?

angularjs - 如何在e2e测试中触发 'swipeleft'事件( Protractor )

Android - 检测 OpenGLRenderer 内存不足

android - 找不到字段的 setter - 将 Kotlin 与 Room 数据库结合使用

javascript - 如何使用 Hammer.js Touch Emulator 编辑文本输入字段?

android - 如何在 Android Studio 的后台服务中检测屏幕是否被触摸?

java - 检测 View 上的滑动手势和触摸事件