【发布时间】:2020-01-02 23:59:45
【问题描述】:
Android Studio、Java
有没有办法在单一视图上检测触摸、触摸释放和滑动手势?我有一个按钮,我需要能够在按钮上收听所有这些内容。
我正在寻找一种解决方案,让侦听器/检测器最终调用这些方法,以便我可以轻松地使用它们:
public void buttonPressedDown() {
}
public void buttonReleased() {
}
public void swipeUp() {
}
public void swipeRight() {
}
public void swipeDown() {
}
public void swipeLeft() {
}
在开发 ios 应用程序时,这在 xcode 中很容易实现,但我似乎无法为 android 找到方法。
额外信息: 这是我尝试过的:
timerButton.setOnTouchListener(new View.OnTouchListener() { //Detecting a press down
@Override
public boolean onTouch(View v, MotionEvent event) {
timerDown();
return false;
}
});
timerButton.setOnTouchListener(new OnSwipeTouchListener(timerActivity.this) {//Detecting swipes
public void onSwipeTop() {
showStats();
}
public void onSwipeBottom() {
showType();
}
public void onSwipeRight() {
showMenu();
}
public void onSwipeLeft() {
showTimes();
}
});
.
.
.
public void timerButtonPressed(View view) {//Detecting press releases
timerUp();
}
这似乎是我正在寻找的东西,但它不起作用。在同一视图上设置两个 onTouchListener 时,只调用了一个侦听器。当我测试它时,要么只有滑动检测有效,要么只有按下检测有效。
帮忙!?
编辑: 我试过这个:
OnSwipeTouchListener.java
public class OnSwipeTouchListener implements View.OnTouchListener {
private final GestureDetector gestureDetector;
public OnSwipeTouchListener(Context context) {
gestureDetector = new GestureDetector(context, new GestureListener());
}
public void onSwipeLeft() {
}
public void onSwipeRight() {
}
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
private final class GestureListener extends GestureDetector.SimpleOnGestureListener {
private static final int SWIPE_DISTANCE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
float distanceX = e2.getX() - e1.getX();
float distanceY = e2.getY() - e1.getY();
if (Math.abs(distanceX) > Math.abs(distanceY) && Math.abs(distanceX) > SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (distanceX > 0)
onSwipeRight();
else
onSwipeLeft();
return true;
}
return false;
}
}
}
在我的活动中:
timerButton.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
OnSwipeTouchListener onSwipeTouchListener = new OnSwipeTouchListener(timerActivity.this);
boolean result = onSwipeTouchListener.onTouch(v, event);
// check if swipe was detected:
if (result) return true;
// Otherwise, detect other types of motion event
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// touch down
timerDown();
break;
case MotionEvent.ACTION_UP:
// touch released
timerUp();
break;
}
return false;
}
});
但是我如何从这里检测到滑动?此外,当我运行应用程序时,它甚至不再检测到触摸。
我也在我的活动中尝试过这个:
timerButton.setOnTouchListener(new OnSwipeTouchListener(timerActivity.this) {
public void onSwipeLeft() {
Log.i("LEFT", "LEFT");
}
public void onSwipeRight() {
Log.i("RIGHT", "RIGHT");
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// touch down
timerDown();
break;
case MotionEvent.ACTION_UP:
// touch released
timerUp();
break;
}
return false;
}
});
现在它会检测触摸但不会检测滑动。如果我删除了上面的 @override onTouch 方法,但仍然有 onSwipe 方法,则检测到滑动。这怎么这么复杂?为什么它不能同时检测到滑动和触摸,我该如何解决?
【问题讨论】:
标签: java android android-studio touch swipe