admin管理员组

文章数量:1315810

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?

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?

Share Improve this question edited Aug 16, 2015 at 18:06 Aruna Tebel 1,4661 gold badge12 silver badges25 bronze badges asked Aug 16, 2015 at 17:58 Oleksandr FirsovOleksandr Firsov 1,4781 gold badge21 silver badges50 bronze badges 1
  • I think the following link will help you stackoverflow./questions/2640119/… – Prasad Commented Aug 16, 2015 at 18:03
Add a ment  | 

4 Answers 4

Reset to default 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;
    }
});

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));

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

To elaborate, Android doesn't deliver the ACTION_MOVE and ACTION_UP events to your listener if you didn't handle the corresponding ACTION_DOWN event first, i.e. returned true in the listener.

Option 1

val listener = object: SimpleOnGestureListener() {

    override fun onDown(e: MotionEvent): Boolean {
        return true // <<< This bit is important.
    }

    override fun onDoubleTap(e: MotionEvent): Boolean {
        // This now works.
        // ...
    }

    override fun onDoubleTapEvent(e: MotionEvent): Boolean {
        // This now works.
        // ...
    }

    override fun onSingleTapConfirmed(e: MotionEvent): Boolean {
        // This now works.
        // ...
    }
}

Option 2

view.setOnTouchListener { _, event -> 
    gestureDetector.onTouchEvent(event) || event.actionMasked == MotionEvent.ACTION_DOWN
}

本文标签: javascriptAndroid How to detect double tapStack Overflow