从射击停止OnLongClickListener同时拖动 [英] Stop OnLongClickListener from firing while dragging

查看:157
本文介绍了从射击停止OnLongClickListener同时拖动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义视图与它的位图,用户可以拖动左右。

I have a custom View with bitmaps on it that the user can drag about.

我想使它所以当他们长的点击其中一个,我可以弹出的选项,如复位位置等上下文菜单。

I want to make it so when they long click one of them I can pop up a context menu with options such as reset position etc.

在自定义视图中添加我OnLongClickListener:

In the custom View I add my OnLongClickListener:

this.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        // show context menu..
        return true;
    }
});

和覆盖的onTouchEvent看起来是这样的:

And override onTouchEvent to look something like this:

public boolean onTouchEvent(MotionEvent event) {
    handleDrag(event);
    super.onTouchEvent(event);
    return true;
}

在handleDrag函数查找什么对象是pressed和处理更新它的位置。

The handleDrag function finds what object is been pressed, and handles updating it's position.

我的问题是,当我开始拖动图像的OnLongClickListener火也。我不知道解决这个问题的最好办法。

My problem is that when I start to drag an image the OnLongClickListener fires also. I'm not sure the best way around this.

我已经尝试添加一个门槛handleDrag如果用户触摸下来,但不会尝试拖动到返回假的,但我发现它拿到仍难以正确处理解雇了。

I've tried adding a threshold to handleDrag to return false if user touches down but doesn't attempt to drag, but I'm finding it still difficult to get the correct handler fired.

任何人都可以提出一个方法来跳过OnLongClickListener同时拖动?

Can anyone suggest a way to skip the OnLongClickListener while dragging?

推荐答案

我想我必须通过调整我的门槛的做法这解决了。

I think I have this solved through tweaking my threshold approach.

首先,我改变了我的onTouchEvent看起来是这样的:

First, I changed my onTouchEvent to look like this:

 public boolean onTouchEvent(MotionEvent event) {
     mMultiTouchController.handleDrag(event);
     return super.onTouchEvent(event);
 }

现在,他们两个火,让我再改变了我的OnLongClickListener为以下内容:

They now both fire, so I then changed my OnLongClickListener to the following:

 this.setOnLongClickListener(new View.OnLongClickListener() {
     @Override
     public boolean onLongClick(View v) {
         if (!mMultiTouchController.has_moved) {
             // Pop menu and done...
             return false;
         }
         return true;
     }
 });

(mMultiTouchController是包含所有的手势检测code类)。 这里的关键是该类别中,我添加了布尔has_moved。当我开始拖我,然后计算增量:

(mMultiTouchController is the class containing all my gesture detection code). The key here is within this class, I added the bool 'has_moved'. When I go to start a drag I then compute the delta:

 float diffX = Math.abs(mCurrPtX - mPrevPt.getX());
 float diffY = Math.abs(mCurrPtY - mPrevPt.getY());
 if (diffX < threshold && diffY < threshold) {
     has_moved = false;
     return;
 }

现在,当onLongClick火灾我知道是否采取行动或不。

Now when the onLongClick fires I know whether to take action or not.

最后一块是设置:

setHapticFeedbackEnabled(false);

在我看来,这样的用户没有得到一个振动longClick火灾,但没有采取行动每次。我打算做手工的振动作为下一个步骤。

in my View so that the user doesn't get a vibrate every time the longClick fires but no action is taken. I plan to do the vibration manually as a next step.

这似乎是确定迄今为止,希望帮助人谁碰到过类似的情况这一个。

This seems to be ok so far, hope that helps anyone who has come across a similar situation as this one.

这篇关于从射击停止OnLongClickListener同时拖动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆