如何确定是否在 MotionEvent.ACTION_SCROLL 之外单击了屏幕?
How do I determine if a screen is clicked outside of MotionEvent.ACTION_SCROLL?
我无法访问屏幕上对象的点击事件。所以我想用触摸监听器捕捉屏幕上的点击事件。但是当它滚动时,它也被视为点击。我该如何克服这个问题?
mMessagesList.setOnTouchListener(new View.OnTouchListener() {
private static final int MAX_CLICK_DURATION = 200;
private long startClickTime;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
startClickTime = Calendar.getInstance().getTimeInMillis();
break;
}
case MotionEvent.ACTION_UP: {
long clickDuration = Calendar.getInstance().getTimeInMillis() - startClickTime;
if (clickDuration < MAX_CLICK_DURATION) {
//click event has occurred
myAction();
}
break;
}
case MotionEvent.ACTION_MOVE:
return false;
case MotionEvent.ACTION_SCROLL:
return false;
case MotionEvent.AXIS_SCROLL:
return false;
case MotionEvent.ACTION_BUTTON_PRESS:
return true;
}
return false;
}
});
我为此使用了 "GestureDetector.OnGestureListener"。因此 "upScroll" 在 "downScroll" 情况下不会触发。我只在它真正触及屏幕时才这样做。
我无法访问屏幕上对象的点击事件。所以我想用触摸监听器捕捉屏幕上的点击事件。但是当它滚动时,它也被视为点击。我该如何克服这个问题?
mMessagesList.setOnTouchListener(new View.OnTouchListener() {
private static final int MAX_CLICK_DURATION = 200;
private long startClickTime;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
startClickTime = Calendar.getInstance().getTimeInMillis();
break;
}
case MotionEvent.ACTION_UP: {
long clickDuration = Calendar.getInstance().getTimeInMillis() - startClickTime;
if (clickDuration < MAX_CLICK_DURATION) {
//click event has occurred
myAction();
}
break;
}
case MotionEvent.ACTION_MOVE:
return false;
case MotionEvent.ACTION_SCROLL:
return false;
case MotionEvent.AXIS_SCROLL:
return false;
case MotionEvent.ACTION_BUTTON_PRESS:
return true;
}
return false;
}
});
我为此使用了 "GestureDetector.OnGestureListener"。因此 "upScroll" 在 "downScroll" 情况下不会触发。我只在它真正触及屏幕时才这样做。