I want to let the ListView intercept all the events, except the SingleTap.
First I did the code bellow
private float startX;
private float startY;
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN ) {
Log.d("intercept", "down");
startX = event.getX();
startY = event.getY();
}
if (event.getAction() == MotionEvent.ACTION_UP) {
Log.d("intercept", "up");
float deltaX = Math.abs(event.getX() - startX);
float deltaY = Math.abs(event.getY() - startY);
if(deltaX<5 && deltaY<5){
return false;
}
}
return super.onInterceptTouchEvent(event);
}
With that I was able to see that I really detect the singleTap (intercept-down
and intercep-up
on LogCat), but it block my list to handle other moves.
After that I change return super.onInterceptTouchEvent(event);
to return true
and it works for the rest of the moves. The problem is that I was not able anymor to detect the singleTap now.
I can see that intercept-down
on Logcat, but not intercept-up
.
I already tried this another code withou sucess too.
import android.content.Context;
import android.support.v4.view.GestureDetectorCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ListView;
public class SwipeListView extends ListView {
Context context;
private GestureDetectorCompat mDetector;
public SwipeListView(Context context) {
super(context);
}
public SwipeListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SwipeListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void setContext(Context context){
this.context= context;
mDetector = new GestureDetectorCompat(context, new MyGestureListener());
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
return !onTouchEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent e) {
return mDetector.onTouchEvent(e);
}
class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
private static final String DEBUG_TAG = "Gestures";
@Override
public boolean onDown(MotionEvent event) {
Log.e(DEBUG_TAG,"onDown: " + event.toString());
return true;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent event) {
Log.e(DEBUG_TAG, "onSingleTap: " + event.toString());
return true;
}
}
}
Anyone could help me to detect the singleTap correctly?