sách gpt4 ăn đã đi

android - 如何通过从屏幕底部边缘拉动来显示 BottomSheet?

In lại Tác giả: Walker 123 更新时间:2023-11-30 00:38:39 26 4
mua khóa gpt4 giày nike

我是 Android 的新手 BottomSheet并且我已经成功地尝试了有关如何使用它的示例,但大多数示例仅演示了通过查看点击事件显示/隐藏 BottomSheet。

现在我有以下代码:

BottomSheetBehavior mBottomSheetBehavior;

@Ghi đè
protected void onCreate() {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View bottomSheet = findViewById(R.id.bottom_sheet);
mBottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
mBottomSheetBehavior.setPeekHeight(0);
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
}

现在,我只想知道当状态为 BottomSheetBehavior.STATE_HIDDEN 时,是否可以通过从屏幕底部边缘拉/拖来显示 BottomSheet。 peekHeight 是 0 ?,类似于 DrawerLayout 的行为?

câu trả lời hay nhất

我认为 BottomSheetBehavior 不提供该功能,但您可以拦截包含 Activity 中的触摸事件,并在检测到底部拉动时展开 Bottom Sheet :

@Ghi đè
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bottomPullDetector = new GestureDetectorCompat(this, new BottomPullListener());
}

@Ghi đè
public boolean dispatchTouchEvent(MotionEvent ev) {
return bottomPullDetector.onTouchEvent(ev) || super.dispatchTouchEvent(ev);
}


private class BottomPullListener extends GestureDetector.SimpleOnGestureListener {
private static final int SWIPE_THRESHOLD = 20;
private final int displayHeight;
private boolean beginningOnBottom;

BottomPullListener() {
final Point displaySize = new Point();
getWindowManager().getDefaultDisplay().getSize(displaySize);
displayHeight = displaySize.y;
}

@Ghi đè
public boolean onDown(MotionEvent event) {
beginningOnBottom = displayHeight - event.getY() < SWIPE_THRESHOLD;
return beginningOnBottom;
}

@Ghi đè
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (beginningOnBottom && e1.getY() > e2.getY()) {
cameraFragment.expandBottomSheet();
trả về giá trị đúng;
}
trả về false;
}
}

关于android - 如何通过从屏幕底部边缘拉动来显示 BottomSheet?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42942060/

26 4 0
Walker 123
Hồ sơ

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá taxi Didi miễn phí
Phiếu giảm giá taxi Didi
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress