gpt4 book ai didi

android - 在 GestureDetector 中长按也会触发

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:55:53 25 4
gpt4 key购买 nike

我尝试从这个问题中实现代码:Detecting a long press with Android ,但它总是检测到长按,即使我只是点击它。我的代码如下(我扩展了ImageView,因为里面的代码比较多,与本题无关):

public class EbsImageView extends ImageView  {
final GestureDetector gestureDetector = new GestureDetector(this.getContext(), new GestureDetector.SimpleOnGestureListener() {
public void onLongPress(MotionEvent e) {
Log.e("", "Longpress detected");
}
public boolean onSingleTapUP(MotionEvent e) {
Log.e("", "Press detected");

// don't consume, we want the long press to fire if it was a
// long press
return false;
}
});

@Override
public boolean onTouchEvent(MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
}

最佳答案

您需要覆盖 onDown 并使其返回 true。以下代码对我有用:

class SimpleGestureListener extends GestureDetector.SimpleOnGestureListener {

@Override
public boolean onDown(MotionEvent event) {
// triggers first for both single tap and long press
return true;
}

@Override
public boolean onSingleTapUp(MotionEvent event) {
// triggers after onDown only for single tap
return true;
}

@Override
public void onLongPress(MotionEvent event) {
// triggers after onDown only for long press
super.onLongPress(event);
}
}

关于android - 在 GestureDetector 中长按也会触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24326530/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com