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?
- I think the following link will help you stackoverflow./questions/2640119/… – Prasad Commented Aug 16, 2015 at 18:03
4 Answers
Reset to default 2try 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
版权声明:本文标题:javascript - Android How to detect double tap? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741987200a2408754.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论