2

I'm trying to refresh Canvas on DoubleTap in android. I use GestureDetector in custom View.

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

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

But I'm getting the error

The method onDoubleTap(MotionEvent) of type new GestureDetector.OnGestureListener(){} must override or implement a supertype method

with

Remove '@Override' annotation

solution. I remove override and get this warning

The method onDoubleTap(MotionEvent) from the type new GestureDetector.OnGestureListener() {} is never used locally.

Then I tried to test whether this works and made a function to change TextView string whenever I DoubleTap. Nothing happens.

I also looked at GestureDetector Reference for explanations, but they don't even have DoubleTap there, which everybody uses. What should I do?

Aruna Tebel
  • 1,436
  • 1
  • 12
  • 24
Oleksandr Firsov
  • 1,428
  • 1
  • 21
  • 48
  • I think the following link will help you http://stackoverflow.com/questions/2640119/how-to-detect-doubletap-on-a-view – Prasad Aug 16 '15 at 18:03

3 Answers3

2

try this

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;
    }
});
Davide
  • 126
  • 8
  • Nope it does not work. `The value of the local variable Detector is not used` - highlighted mDetector warning. – Oleksandr Firsov Aug 16 '15 at 19:02
  • I check whether stuff works by writing this function - `text = MainActivity.text` `text.setText("stuff")` – Oleksandr Firsov Aug 16 '15 at 19:03
  • With some other sources I made it work, but now I have a problem - I use this in `WebView`, so double tap zooms in and out, and I have `GestureDetector.OnGestureListener`. When I use also `SimpleOnGestureListener` zooming is disabled. – Oleksandr Firsov Aug 17 '15 at 14:00
  • please note that the warning "The value of the local variable Detector is not used" is not an error, so you need to set the detector `mDetector.setOnDoubleTapListener(this);`. Try to read this is vr useful https://developer.android.com/training/gestures/detector.html – Davide Aug 17 '15 at 14:04
  • Yep. This link did the thing, but again, now zooming feature's disabled. How can I keep both of them? – Oleksandr Firsov Aug 17 '15 at 14:59
  • i suggest to close this question and open another one where you'll able to post more detailed code for this issue (in this way more people can help you) – Davide Aug 17 '15 at 15:08
1

For the ones, who were wondering how to set it also to the corresponding view:

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));
Javatar
  • 2,518
  • 1
  • 31
  • 43
0

My approach to this problem was different since I needed to perform something for the onClick listener as well, and also it was in a list view, so I needed to know what was the item content. here is my approach using kotlin Job: At the top of the class I've declared something like this:

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

where CartViewItem is the model that is used in the list. and this is my onClickListener logic:

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()
}

here I wait for 200 milliseconds for the second tap, and if it didn't happen, I will make clickedViewItem null, so its not valid anymore

Amin Keshavarzian
  • 3,646
  • 1
  • 37
  • 38