我想让ListView拦截除SingleTap之外的所有事件。
首先我做了下面的代码
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);
}
有了这个,我能够看到我真的在LogCat上检测到singleTap(intercept-down
和intercep-up
),但它阻止我的列表来处理其他动作。
之后,我将return super.onInterceptTouchEvent(event);
更改为return true
,这对其余的动作都有效。问题是我现在无法检测到singleTap。
我可以在Logcat上看到intercept-down
,但不是intercept-up
。
我已经尝试了另一个代码,但也没有。
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;
}
}
}
任何人都可以帮我正确检测singleTap吗?
答案 0 :(得分:0)
同时,尝试使用您的代码 您是否尝试过:通过以下方式向下/向上返回true:
public boolean onTouchEvent(MotionEvent e) {
return gestureScanner.onTouchEvent(e);
}
public boolean onSingleTapConfirmed(MotionEvent e) {
//this works for me
return true;
}
public boolean onDown(MotionEvent e) { return true; }
public void onLongPress(MotionEvent e) {
//
}