问题
Hi I'm using swipe layout in my recyclerview. Also I want to implement single and double tap for my list item layout. If i use touch listener my swipe will not work. So i want to detect single and double click of an layout using setOnClickListener. Please suggest me an idea to detect single and double tap for a layout.
回答1:
Hi I found a solution both single tap and double tap will work for single view using onSingleTapConfirmed like below,
public class OnSwipeTouchListener implements View.OnTouchListener {
private GestureDetector gestureDetector;
public OnSwipeTouchListener(Context c) {
gestureDetector = new GestureDetector(c, new GestureListener());
}
public boolean onTouch(final View view, final MotionEvent motionEvent) {
return gestureDetector.onTouchEvent(motionEvent);
}
private final class GestureListener extends GestureDetector.SimpleOnGestureListener {
private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
return super.onSingleTapUp(e);
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
onClick();
return super.onSingleTapConfirmed(e);
}
@Override
public boolean onDoubleTap(MotionEvent e) {
onDoubleClick();
return super.onDoubleTap(e);
}
@Override
public void onLongPress(MotionEvent e) {
onLongClick();
super.onLongPress(e);
}
// Determines the fling velocity and then fires the appropriate swipe event accordingly
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
}
} else {
if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY > 0) {
onSwipeDown();
} else {
onSwipeUp();
}
}
}
} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
}
public void onSwipeRight() {
}
public void onSwipeLeft() {
}
public void onSwipeUp() {
}
public void onSwipeDown() {
}
public void onClick() {
}
public void onDoubleClick() {
}
public void onLongClick() {
}}
then implement this class like below,
btn.setOnTouchListener(new OnSwipeTouchListener(adapter.getContext()) {
@Override
public void onClick() {
super.onClick();
AlertUtils.showToast(adapter.getContext(),"single tap");
// your on click here
}
@Override
public void onDoubleClick() {
super.onDoubleClick();
// your on onDoubleClick here
}
@Override
public void onLongClick() {
super.onLongClick();
// your on onLongClick here
}
@Override
public void onSwipeUp() {
super.onSwipeUp();
// your swipe up here
}
@Override
public void onSwipeDown() {
super.onSwipeDown();
// your swipe down here.
}
@Override
public void onSwipeLeft() {
super.onSwipeLeft();
// your swipe left here.
}
@Override
public void onSwipeRight() {
super.onSwipeRight();
// your swipe right here.
}
});
Happy coding..
回答2:
Use the following steps:
- Add on touch listener to the view
- Add a global variable to record the time of the touch
- Compare the touch time with previous touch time. If difference is less than threshold for example 1 seconds, then it is a double tap.
回答3:
Try this
int c = 0;
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
c++;
Handler handler = new Handler();
Runnable r = new Runnable() {
@Override
public void run() {
c = 0;
}
};
if (c == 1) {
//Single click
handler.postDelayed(r, 250);
} else if (c == 2) {
//Double click
c = 0;
ShowDailog();
}
}
});
来源:https://stackoverflow.com/questions/38912207/how-to-detect-single-and-double-tap-for-specific-layout-using-setonclicklistener