[英]Fling Gesture Detection on Toolbar
I'm wanting to detect someone dragging/flinging down from the toolbar, in a fashion similar to the Chrome, with the intention of closing the activity instead. 我想检测有人以类似于Chrome的方式从工具栏向下拖动/拖动,目的是关闭活动。
I however found that the gesture is not detected at all (I found this out due to onFling()
never being called), however single/double taps are still detected detected. 但是,我发现根本没有检测到手势(我发现这是由于从未调用过
onFling()
导致的),但是仍然检测到检测到单点/双击。
Below is a snippets from the code that I used to test if it works: 以下是我用来测试是否有效的代码片段:
@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);
/*...*/
}
In your onFling instead of flase
return: 在您的onFling而不是
flase
返回:
super.onFling(e1, e2, velocityX, velocityY);
And return true
in setOnTouchListener
并在
setOnTouchListener
返回true
toolbar.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
toolbarGestureDetector.onTouchEvent(motionEvent);
return true;
}
});
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;
}
You're subtracting event2.getY() twice so it will never be more than MIN_SWIPE_DISTANCE. 您要减去event2.getY()两次,因此它永远不会超过MIN_SWIPE_DISTANCE。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.