【问题标题】:Android How to detect double tap?Android 如何检测双击?
【发布时间】:2015-11-09 08:55:25
【问题描述】:

我正在尝试在 android 中刷新 DoubleTap 上的 Canvas。我在自定义View 中使用GestureDetector

final GestureDetector mDetector = new GestureDetector(
    getContext(), new GestureDetector.OnGestureListener() {

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        invalidate();
        return true;
    }
}

但我得到了错误

new 类型的 onDoubleTap(MotionEvent) 方法 GestureDetector.OnGestureListener(){} 必须覆盖或实现 超类型方法

Remove '@Override' annotation

解决方案。我删除覆盖并收到此警告

new 类型的 onDoubleTap(MotionEvent) 方法 GestureDetector.OnGestureListener() {} 从未在本地使用。

然后我尝试测试这是否有效,并创建了一个函数来更改 TextView 字符串,每当我 DoubleTap 时。什么都没有发生。

我还查看了GestureDetector Reference 的解释,但他们甚至没有DoubleTap,每个人都使用它。我该怎么办?

【问题讨论】:

标签: javascript android double-click gesturedetector


【解决方案1】:

试试这个

final GestureDetector mDetector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener {

    @Override
    public boolean onDown(MotionEvent e) {

        return true;
    }

    @Override
    public boolean onDoubleTap(MotionEvent e) {

        return true;
    }
});

【讨论】:

  • 不,它不起作用。 The value of the local variable Detector is not used - 突出显示 mDetector 警告。
  • 我通过编写这个函数来检查东西是否有效 - text = MainActivity.text text.setText("stuff")
  • 通过其他一些来源,我让它工作了,但现在我遇到了一个问题 - 我在 WebView 中使用它,所以双击放大和缩小,我有 GestureDetector.OnGestureListener。当我也使用SimpleOnGestureListener 时,缩放功能被禁用。
  • 请注意警告“The value of the local variable Detector is not used”不是错误,所以需要设置检测器mDetector.setOnDoubleTapListener(this);。尝试阅读这篇文章很有用developer.android.com/training/gestures/detector.html
  • 是的。这个链接做了这件事,但同样,现在缩放功能被禁用了。我怎样才能同时保留它们?
【解决方案2】:

对于那些想知道如何将其设置为相应视图的人:

final GestureDetector gDetector = new GestureDetector(getBaseContext(), new GestureDetector.SimpleOnGestureListener() {

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        doIt();
        return true;
    }
});

// Set it to the view
mButton.setOnTouchListener((v, event) -> gDetector.onTouchEvent(event));

【讨论】:

    【解决方案3】:

    我解决这个问题的方法不同,因为我还需要为 onClick 侦听器执行一些操作,而且它也在列表视图中,所以我需要知道项目内容是什么。 这是我使用 kotlin Job 的方法: 在类的顶部,我声明了这样的内容:

    private var doubleTapTimerJob: Job = Job()
    private var clickedViewItem: CartViewItem? = null
    val DOUBLE_TAP_DELAY = 200L
    

    其中 CartViewItem 是列表中使用的模型。 这是我的 onClickListener 逻辑:

    if (clickedViewItem == null || clickedViewItem != cartViewItem) {
        doubleTapTimerJob.cancel()
        doubleTapTimerJob = lifecycleScope.launch {
            delay(DOUBLE_TAP_DELAY)
            clickedViewItem = null
        }
        clickedViewItem = cartViewItem
        onClicked(cartViewItem)
    } else {
        onDoubleClicked(cartViewItem)
        clickedViewItem = null
        doubleTapTimerJob.cancel()
    }
    

    这里我等待第二次点击 200 毫秒,如果没有发生,我会将 clickedViewItem 设为 null,因此它不再有效

    【讨论】:

      猜你喜欢
      • 2011-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-23
      • 1970-01-01
      • 2015-04-25
      相关资源
      最近更新 更多