[英]How to implement swipe (left/right) gesture in a layout in Android Studio?
我的意图是在布局中实施滑动。 在版面中从左向右滑动时,一个视图将可见和隐藏。
我正在使用Android Studio。 是否有任何可直接使用的库?
您可以使用onfling手势侦听器。 下面为您提供的示例代码
public class MyActivity extends Activity {
private void onCreate() {
// Set your layout
final ImageView imageView = (ImageView) findViewById(R.id.image_view);
imageView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(final View view, final MotionEvent event) {
return gdt.onTouchEvent(event);
}
});
}
private final GestureDetector gdt = new GestureDetector(new GestureListener());
private class GestureListener extends SimpleOnGestureListener {
private final int SWIPE_MIN_DISTANCE = 120;
private final int SWIPE_THRESHOLD_VELOCITY = 200;
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
// Right to left, your code here
return true;
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
// Left to right, your code here
return true;
}
if(e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
// Bottom to top, your code here
return true;
} else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
// Top to bottom, your code here
return true;
}
return false;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.