我定义了一个扩展GestureDetector.SimpleOnGestureListener
并覆盖onDown
和onFling
方法的类
class MyGestureDetector extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return super.onDown(e);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
Toast.makeText(PersianDatePicker.this.getContext(),
"fling " + velocityX + " - " + velocityY, Toast.LENGTH_LONG)
.show();
return super.onFling(e1, e2, velocityX, velocityY);
}
}
我还定义了GestureDetectorCompat
@Override
public boolean onTouchEvent(MotionEvent event) {
mDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
我在一项活动中对它进行了测试,效果很好。我想在自定义视图中使用它,但它不起作用!我没有错误。
答案 0 :(得分:3)
在自定义视图中,试试这个:
@Override
public boolean onTouchEvent(MotionEvent event) {
if (mGestureDetector.onTouchEvent(event)) {
return true;
}
return super.onTouchEvent(event);
}
或更简洁:
@Override
public boolean onTouchEvent(MotionEvent event) {
return mGestureDetector.onTouchEvent(event) || super.onTouchEvent(event);
}