Long press in GestureDetector also fires on tap

前端 未结 3 2082
野趣味
野趣味 2020-12-17 17:24

I tried implementing code from this question: Detecting a long press with Android, but it always detects a long press, if even I just tap it. My code is as follows (I extend

相关标签:
3条回答
  • 2020-12-17 17:45

    As I mention in my comment, you should distinguish between onSingleTapUp() and onSingleTapUP(). Using the Override annotation is a good practice.

    I'm a bit confused because the title of your question seems to indicate that a long press also fires the on tap event, while the text of your question says it the other way around. That is that a tap fires a long press event. If the later is the case, try returning true from your onSingleTapUp(). This works for me using the SimpleGestureListener:

    class SimpleGestureListener extends GestureDetector.SimpleOnGestureListener {
    
      @Override
      public boolean onDown(MotionEvent event) {
         //...
        return super.onDown(event);
      }
    
      @Override
      public boolean onSingleTapUp(MotionEvent event) {
        //...
        return true;
      }
    
      @Override
      public void onLongPress(MotionEvent event) {
        //...
        super.onLongPress(event);
      }
    }
    

    I hope it helps.

    0 讨论(0)
  • 2020-12-17 18:07

    Solution above works (returning true in onDown()). I have also made it work by setting clickable and focusable property of View to true before setting GestureDetector.

    val gestureDetectorCompat = GestureDetectorCompat(context, object : GestureDetector.SimpleOnGestureListener() {
            override fun onSingleTapConfirmed(e: MotionEvent?): Boolean {
                // log "Single Tap Confirm"
                return true
            }
    
            override fun onLongPress(e: MotionEvent?) {
                // log "Long Press"
            }
        })
    image_stateful.isClickable = true
    image_stateful.isFocusable = true
    image_stateful.setOnTouchListener { _, event -> gestureDetectorCompat.onTouchEvent(event) }
    
    0 讨论(0)
  • 2020-12-17 18:10

    You need to override onDown and make it return true. The following code worked for me:

    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题