我无法访问屏幕上对象的点击事件。所以我想用触摸侦听器捕获屏幕上的点击事件。但在滚动时也被视为点击。我该如何克服?
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;
}
});
答案 0 :(得分:1)
为此,我使用了“ GestureDetector.OnGestureListener”。因此,在“ downScroll”情况下不会触发“ upScroll”。我只会在它真正触摸屏幕时这样做。