Android onFling没有回应

Android onFling没有回应,android,gesture,onfling,Android,Gesture,Onfling,我整晚都在盯着这个。我正在尝试为Android制作一个简单的交互式球类游戏,但我似乎无法让onFling方法发挥作用。我在onFling中的日志不会出现在LogCat中,并且对fling手势没有响应。我想我的手势检测器或手势监听器有问题。请帮帮我,我终于可以睡觉了:)。这是我的代码: 这在onCreate之外,但仍在活动中: @Override public boolean onTouchEvent(MotionEvent me) { return gestureDetector.onT

我整晚都在盯着这个。我正在尝试为Android制作一个简单的交互式球类游戏,但我似乎无法让onFling方法发挥作用。我在onFling中的日志不会出现在LogCat中,并且对fling手势没有响应。我想我的手势检测器或手势监听器有问题。请帮帮我,我终于可以睡觉了:)。这是我的代码:

这在onCreate之外,但仍在活动中:

@Override
public boolean onTouchEvent(MotionEvent me) {
    return gestureDetector.onTouchEvent(me);
}

SimpleOnGestureListener simpleOnGestureListener = new SimpleOnGestureListener() {

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        ballSpeed.x = velocityX;
        ballSpeed.y = velocityY;
        Log.d("TAG", "----> velocityX: " + velocityX);
        Log.d("TAG", "----> velocityY: " + velocityY);

        return true;

    }
};

GestureDetector gestureDetector = new GestureDetector(null,
        simpleOnGestureListener);
编辑:

显然,上面的代码确实有效。这意味着错误在其他地方@bobnoble说我应该发布我的onCreate()方法:


您的活动声明中是否有“implements OnTestureListener”

看到这个片段了吗


我将您提供的代码放入一个
活动
,注释掉
ballSpeed
行,并添加
Log
输出。一切正常。显示输出的logcat位于末尾

如果这对您不起作用,则问题在您的
活动中的其他地方。您可能想发布您的
onCreate
方法

@Override
public boolean onTouchEvent(MotionEvent me) {
    Log.i(TAG, "onTouchEvent occurred...");
    return gestureDetector.onTouchEvent(me);
}

SimpleOnGestureListener simpleOnGestureListener = new SimpleOnGestureListener() {

    @Override
    public boolean onDown(MotionEvent e) {
        Log.i(TAG, "onDown event occurred...");
        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
//          ballSpeed.x = velocityX;
//          ballSpeed.y = velocityY;
        Log.d("TAG", "----> velocityX: " + velocityX);
        Log.d("TAG", "----> velocityY: " + velocityY);

        return true;
    }
};

GestureDetector gestureDetector = new GestureDetector(null,
        simpleOnGestureListener);
Logcat显示多个
onTouchEvent
s、
onDown
onFling

11-18 15:51:25.364: I/SO_MainActivity(13415): onTouchEvent occurred...
11-18 15:51:25.364: I/SO_MainActivity(13415): onDown event occurred...
...
11-18 15:51:25.584: I/SO_MainActivity(13415): onTouchEvent occurred...
11-18 15:51:25.604: I/SO_MainActivity(13415): onTouchEvent occurred...
11-18 15:51:25.664: D/TAG(13415): ----> velocityX: 3198.8127
11-18 15:51:25.664: D/TAG(13415): ----> velocityY: -1447.966
根据查看
onCreate
方法进行编辑:

mainView.setOnTouchListener
onTouch
方法通过返回true来使用触摸。通过返回true,
onTouch
方法表明它已经完全处理了触摸事件,不再进一步传播它


将其更改为
返回false
会导致它传播触摸事件,您应该会看到其他触摸事件方法被调用。

问两次同样的问题不会有帮助。请阅读一些关于GetureDetector的教程,进行一些调试,并将您的问题简化为可管理的问题。到目前为止,您所做的有效工作就是丢弃所有代码,并说“请阅读本文,告诉我哪里出了问题”。这不太可能得到好的回答。我知道今晚早些时候我问了一个类似的问题。我不想(再次)修改这一条,而是想重新开始,试图更清楚地回答这个问题。但是,无法删除旧的。至于代码,我将尝试删除不重要的部分。再一次,对重新发布感到抱歉,但我今晚必须运行它:)。您需要实现视图的
onTouchEvent
方法,该方法必须调用
GestureDetector
onTouchEvent
,以便分析事件以确定它是否符合
OnTestureListener
的标准。我已经更新了上面的代码。如果我像这样实现它,它仍然不起作用。我做错什么了吗?谢谢你的回答:)。我已经试过了,但不幸的是没有用。非常感谢你的努力。我已经发布了我的onCreate()方法:)。顺便说一句,我想我不需要onTouchEvent方法。你觉得怎么样?@Legion-见最新答案。只需将`mainView.setOnTouchListener中的
onTouch
方法更改为返回false(而不是true)。@Legion-wrt关于需要
onTouchEvent
方法的问题,您需要通过调用
gestureDetector.onTouchEvent
方法来实现
View
onTouchEvent
方法。
11-18 15:51:25.364: I/SO_MainActivity(13415): onTouchEvent occurred...
11-18 15:51:25.364: I/SO_MainActivity(13415): onDown event occurred...
...
11-18 15:51:25.584: I/SO_MainActivity(13415): onTouchEvent occurred...
11-18 15:51:25.604: I/SO_MainActivity(13415): onTouchEvent occurred...
11-18 15:51:25.664: D/TAG(13415): ----> velocityX: 3198.8127
11-18 15:51:25.664: D/TAG(13415): ----> velocityY: -1447.966