手势检测器:检测长按何时释放



我正在覆盖 GestureDetector 的某些功能,我检测到长按,并希望在发布后也进行检测,但我还没有找到该功能。实际上,我发现释放水龙头的唯一功能是onSingleTapUp。是否可以检测到长按何时释放?

我的代码:

class GestureListener (val position: Int) : GestureDetector.SimpleOnGestureListener() {
override fun onLongPress(e: MotionEvent?) {
// Do something here
super.onLongPress(e)
}

在 Java 中你可以这样做

boolean isDown = false;

textView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
Toast.makeText(v.getContext(), "down: " , 1000).show();
isDown = true;
}
else if (action == MotionEvent.ACTION_UP) {

if (isDown) {
// touch press complete, show toast
Toast.makeText(v.getContext(), "User Release Long Press: " , 1000).show();
isDown = false;
}
}

return true;
}
});

显然,您必须将 GestureDetector 与 OnTouchEvent 结合使用:

val detector = GestureDetector(object: GestureDetector.SimpleOnGestureListener() {
override fun onScroll(
e1: MotionEvent?,
e2: MotionEvent?,
distanceX: Float,
distanceY: Float
): Boolean {
currentAction = "isScrolling"
Log.d("TAG", "SCROLLING")
return true
}
override fun onLongPress(e: MotionEvent?) {
Log.d("TAG", "Long press!")
currentAction = "isLongPressed"
super.onLongPress(e)
}
override fun onDown(e: MotionEvent?): Boolean {
return true
}
})
val gestureListener = View.OnTouchListener(function = { view, event ->
detector.onTouchEvent(event)
if(event.getAction() == MotionEvent.ACTION_UP) {
when (currentAction) {
"isScrolling" -> {
Log.d("TAG", "Done scrolling")
currentAction = ""
}
"isLongPressed" -> {
Log.d("TAG", "Done long pressing")
currentAction = ""
}
}
}
false
})
profilePic1.setOnTouchListener(gestureListener)

该ACTION_UP可用,可以检测手指的任何释放,无论是长按、滚动等

最新更新