Gesturedetector和registerForContextMenu

时间:2014-08-20 20:09:16

标签: android gesturedetector onfling android-contextmenu

我在这里变得疯狂,试图理解为什么我一无所知,为什么它不起作用。

我正在使用onFling做一些动作。工作完美。 但是我想添加一个打开上下文菜单的onlongpress。 onlongpress吐司也很完美。但是当我尝试做一个registerForContextMenu时,问题出现了。 如果我在视图中给出了linearlayout或我的任何元素,onlongpress会让上下文显示出来,但是我的onfling不再起作用了(根本)。

所以我试着只注册了ForForTetextMenu onlongpress,然后unregisterforcontextmenu(是的,那个存在,是的,我没有想过它会像以下那样工作:o))但是这不起作用。

所以,任何想法为什么" registerForContextMenu"使停止停止?

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

此致 ħ

编辑;

在我的oncreate中我打电话: registerForContextMenu(ivMain); //这是一个imageview

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

我的手势听众:

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;
    }
}

1 个答案:

答案 0 :(得分:0)

随着pskink的消化,我需要删除活动的ontouch覆盖,并在我的视图中添加onclicklistener并从那里调用我的gesturedetector。

ivMain.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            gDetect.onTouchEvent(event);
            //return super.onTouchEvent(event);
            return true;
 }
});

非常感谢pskink。

相关问题