首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Gesturedetector和registerForContextMenu

Gesturedetector和registerForContextMenu
EN

Stack Overflow用户
提问于 2014-08-21 04:09:16
回答 1查看 243关注 0票数 0

我在这里发疯了,试图理解为什么我对此一无所知,为什么它不起作用。

我正在使用onFling执行一些操作。工作得很完美。但我想添加一个onlongpress,它将打开一个上下文菜单。onlongpress敬酒词也非常有效。但是当我尝试做一个registerForContextMenu时,问题就来了。如果我给出了我的线性布局,或者我在视图中的任何元素,onlongpress会使上下文菜单显示出来,但我的onfling (根本)不再起作用。

所以我试着只按registerForContextMenu,然后取消注册上下文菜单(是的,这个菜单存在,是的,我也不知道它会这样工作:o),但这不起作用。

那么,你知道为什么"registerForContextMenu“会让浮力停止吗?

如果你需要部分代码,请告诉我。

关于H

编辑;

在我的oncreate中,我调用: registerForContextMenu(ivMain);//这是一个图像视图

gDetect =新的GestureDetector(this,new GestureListener());

我的手势监听器:

代码语言:javascript
复制
public class GestureListener extends GestureDetector.SimpleOnGestureListener {

    @Override
    public boolean onDown(MotionEvent e) {

        return true;
    }

    @Override
    public void onLongPress(MotionEvent e) {

        super.onLongPress(e);
        openContextMenu(ivMain);//my imageview i used registerForContextMenu
    }


    @Override
    public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) {

        // calculate the change in X position within the fling gesture
        float horizontalDiff = event2.getX() - event1.getX();
        // calculate the change in Y position within the fling gesture
        float verticalDiff = event2.getY() - event1.getY();
        float absHDiff = Math.abs(horizontalDiff);
        float absVDiff = Math.abs(verticalDiff);
        float absVelocityX = Math.abs(velocityX);
        // float absVelocityY = Math.abs(velocityY);

        if (absHDiff > absVDiff && absHDiff > flingMin && absVelocityX > velocityMin) {
            // move forward or backward
            if (horizontalDiff < 0) {
                stuff();
                    return true;
                }

            } else {
                otherstuff();
                    return true;
                }
            }

        }

        return false;
    }
}
EN

回答 1

Stack Overflow用户

发布于 2014-08-21 05:18:43

正如pskink所建议的,我需要移除activity的ontouch覆盖,并将onclicklistener添加到我的视图中,并从那里调用我的手势检测器。

代码语言:javascript
复制
ivMain.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            gDetect.onTouchEvent(event);
            //return super.onTouchEvent(event);
            return true;
 }
});

非常感谢pskink。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25413559

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档