我想要检测某人以类似于Chrome的方式从工具栏中拖拽/向下移动,而是打算关闭活动。
然而,我发现根本没有检测到手势(我发现这是因为onFling()
从未被调用过),但仍检测到单/双击。
以下是我用来测试代码的代码片段:
@Override
protected void onCreate(Bundle savedInstanceState) {
/*...*/
Toolbar toolbar = (Toolbar) findViewById(R.id.wizard_toolbar);
final GestureDetector toolbarGestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener () {
static final int MIN_SWIPE_VELOCITY = 200;
static final int MIN_SWIPE_DISTANCE = 200;
@Override
public boolean onDown(MotionEvent e) { return true; }
@Override
public boolean onSingleTapUp(MotionEvent e) {
Toast.makeText(EventCreationWizard.this, "You touched me!!!", Toast.LENGTH_LONG).show();
return true;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
Toast.makeText(EventCreationWizard.this, "You touched me!!! Twice!!!", Toast.LENGTH_LONG).show();
return super.onDoubleTap(e);
}
@Override
public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) {
if((event2.getY() - event2.getY() >= MIN_SWIPE_DISTANCE) && (Math.abs(velocityY) >= MIN_SWIPE_VELOCITY)) {
Toast.makeText(EventCreationWizard.this, "You swiped me!!!", Toast.LENGTH_LONG).show();
return true;
}
return false;
}
});
toolbar.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
return toolbarGestureDetector.onTouchEvent(motionEvent);
}
});
setSupportActionBar(toolbar);
/*...*/
}
答案 0 :(得分:0)
在 onFling 而不是flase
返回:
super.onFling(e1, e2, velocityX, velocityY);
并在true
setOnTouchListener
toolbar.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
toolbarGestureDetector.onTouchEvent(motionEvent);
return true;
}
});
答案 1 :(得分:0)
if((event2.getY() - event2.getY() >= MIN_SWIPE_DISTANCE) && (Math.abs(velocityY) >= MIN_SWIPE_VELOCITY)) {
Toast.makeText(EventCreationWizard.this, "You swiped me!!!", Toast.LENGTH_LONG).show();
return true;
}
您要减去event2.getY()两次,因此它永远不会超过MIN_SWIPE_DISTANCE。