拖动时停止 OnLongClickListener 触发 [英] Stop OnLongClickListener from firing while dragging

查看:18
本文介绍了拖动时停止 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 函数查找被按下的对象,并处理更新它的位置.

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 添加一个阈值,以在用户触地但不尝试拖动时返回 false,但我发现仍然很难触发正确的处理程序.

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 是包含我所有手势检测代码的类).这里的关键是在这个类中,我添加了 bool '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天全站免登陆