gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:38:39 25 4
gpt4 key购买 nike

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

现在我有以下代码:

BottomSheetBehavior mBottomSheetBehavior;

@Override
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 的行为?

最佳答案

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

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

@Override
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;
}

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

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (beginningOnBottom && e1.getY() > e2.getY()) {
cameraFragment.expandBottomSheet();
return true;
}
return false;
}
}

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com