我的问题是我无法找到一种拦截水龙头的方法,而不会阻止其他事件(例如滚动)传播到下面的视图。
我正在使用自定义视图来处理点击事件:
public class TapableImageView extends AppCompatImageView {
private GestureDetectorCompat mDetector;
...
public void setOnTapListener(OnTapListener onTapListener){
mDetector = new GestureDetectorCompat(getContext(), new GestureDetector.SimpleOnGestureListener(){
@Override
public boolean onSingleTapUp(MotionEvent event) {
onTapListener.onTap(TapableImageView.this);
return true;
}
});
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
super.dispatchTouchEvent(ev);
if(mDetector != null)
return !mDetector.onTouchEvent(ev);
return false;
}
}
答案 0 :(得分:1)
试试这段代码:
view.setOnTouchListener(new OnTouchListener() {
private long startClickTime;
@Override
public boolean onTouch(View view, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
startClickTime = System.currentTimeMillis();
yourview.dispatchTouchEvent(event);
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
yourview.dispatchTouchEvent(event);
}
else if (event.getAction() == MotionEvent.ACTION_UP) {
if (System.currentTimeMillis() - startClickTime < ViewConfiguration.getTapTimeout()) {
// Touch was a simple tap
return true;
} else {
// Touch was a not a simple tap.
return false;
}
}
return true;
}
});
检查ACTION_DOWN
和ACTION_UP
事件之间的时间是否低于或高于ViewConfiguration.getTapTimeout()
答案 1 :(得分:0)
您必须返回false以指示该事件未被使用