阻止 OnLongClickListener 在拖动时触发
我有一个带有位图的自定义视图,用户可以拖动它。
我想做到这一点,这样当他们长按其中一个时,我可以弹出一个上下文菜单,其中包含重置位置等选项。
在自定义视图中,我添加我的 OnLongClickListener:
this.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// show context menu..
return true;
}
});
并覆盖 onTouchEvent 使其看起来像这样:
public boolean onTouchEvent(MotionEvent event) {
handleDrag(event);
super.onTouchEvent(event);
return true;
}
handleDrag 函数找到按下了什么对象,并处理更新其位置。
我的问题是,当我开始拖动图像时,OnLongClickListener 也会触发。我不确定解决这个问题的最佳方法。
我尝试向handleDrag 添加一个阈值,以便在用户触地但不尝试拖动时返回 false,但我发现仍然很难触发正确的处理程序。
任何人都可以建议一种在拖动时跳过 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.
In the custom View I add my OnLongClickListener:
this.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// show context menu..
return true;
}
});
And override onTouchEvent to look something like this:
public boolean onTouchEvent(MotionEvent event) {
handleDrag(event);
super.onTouchEvent(event);
return true;
}
The handleDrag function finds what object is been pressed, and handles updating it's position.
My problem is that when I start to drag an image the OnLongClickListener fires also. I'm not sure the best way around this.
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.
Can anyone suggest a way to skip the OnLongClickListener while dragging?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想我已经通过调整阈值方法解决了这个问题。
首先,我将 onTouchEvent 更改为如下所示:
现在它们都会触发,因此我将 OnLongClickListener 更改为以下内容:
(mMultiTouchController 是包含我所有手势检测代码的类)。
这里的关键是在这个类中,我添加了布尔值“has_moved”。当我开始拖动时,我会计算增量:
现在,当 onLongClick 触发时,我知道是否采取行动。
最后一部分是在我的视图中设置:,
以便用户不会在每次 longClick 触发时收到振动但不采取任何操作。我计划下一步手动进行振动。
到目前为止似乎还可以,希望对遇到类似情况的人有所帮助。
I think I have this solved through tweaking my threshold approach.
First, I changed my onTouchEvent to look like this:
They now both fire, so I then changed my OnLongClickListener to the following:
(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:
Now when the onLongClick fires I know whether to take action or not.
The final piece was to set:
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 并实现您自己的,这很容易做到。然后您就可以进行控制以防止它们相互干扰。
以下代码实现以下手势:拖动、点击、双击、长按和捏合。
I would stop using the onLongClickListener and just implement your own, which is pretty easy to do. Then you have the control you need to keep them from interfering with each other.
The following code implements the following gestures: drag, tap, double tap, long click, and pinch.
//此代码用于处理手势检测
//This code is to handle the gestures detection