我创建了一个CustomListView,它包含覆盖方法dispatchTouchEvent,返回false如下所示
我需要区分点击,长按和双击。?
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN || ev.getAction() == MotionEvent.ACTION_POINTER_DOWN) {
getParent().requestDisallowInterceptTouchEvent(true);
}
if (ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_POINTER_UP) {
getParent().requestDisallowInterceptTouchEvent(false);
}
return false;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
gestureDetector.onTouchEvent(event);
return false;
}
final GestureDetector gestureDetector = (new GestureDetector(new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
@Override
public void onLongPress(MotionEvent e) {
super.onLongPress(e);
Log.e("test", "Long press detected");
}
@Override
public boolean onDoubleTap(MotionEvent e) {
Log.e("test", "Double Tab detected");
return super.onDoubleTap(e);
}
}));
提供建议。
答案 0 :(得分:0)
在将onTouchListener设置为视图之前声明您的gestureDetector。您还必须在onTouchEvent中提供gestureListener,如下所示:
final GestureDetector.SimpleOnGestureListener simpleOnGestureListener = new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
...
} //etc
}
viewHolder.itemView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event, simpleOnGestureListener);
}
});