问题
I am having a surfaceview on which gesture detection is implemented using following code.
surfaceview.setOnTouchListener(new OnSwipeTouchListener(this ) {
});
public class OnSwipeTouchListener implements OnTouchListener {
private final GestureDetector gestureDetector;
public OnSwipeTouchListener(Context ctx) {
gestureDetector = new GestureDetector(ctx, new GestureListener());
}
public boolean onTouch(final View view, final MotionEvent motionEvent) {
return gestureDetector.onTouchEvent(motionEvent);
}
private final class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e) {
//Toast.makeText(getApplicationContext(),"On Down press", Toast.LENGTH_SHORT).show();
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
boolean result = false;
return result;
}
@Override
public void onLongPress(MotionEvent arg0) {
// TODO Auto-generated method stub
Log.d("g","g");
Toast.makeText(getApplicationContext(),"On long press", Toast.LENGTH_SHORT).show();
Log.d("h","h");
}
@Override
public boolean onScroll(MotionEvent arg0, MotionEvent arg1,
float arg2, float arg3) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onShowPress(MotionEvent arg0) {
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(),"On Show press", Toast.LENGTH_SHORT).show();
}
@Override
public boolean onSingleTapUp(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onDoubleTap(MotionEvent arg0) {
return true;
}
}
}
I want to do an action after user touches screen and holds there for some time. I think such action should go inside onLongPress
method. However the toast inside the method is initiated when user presses long after first tap i.e. on second tap. Is this how this method is supposed to work or I am making some mistake ?
来源:https://stackoverflow.com/questions/23252675/onlongpress-not-working-as-expected