显示可滚动的全屏底部 sheet Android
Show scrollable full screen Bottom sheet Android
我已经解决了需要实现可滚动模式底部的情况 sheet。基本上,一开始它会显示一半的屏幕,然后向上滚动时,它会拉伸到屏幕的 90%。如果继续向上滑动,它会滚动底部的内容 sheet.
我试过的
layout.xml
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/design_bottom_sheet"
style="@style/RootLayout"
android:background="@drawable/rounded_shape"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<...Content.../>
</android.core.widget.NestedScrollView>
</FrameLayout>
<androidx.coordinatorlayout.widget.CoordinatorLayout
Bottom Sheet Dialog class
我按照这里的回答
结果
打开时只显示一半屏幕,像这样向上滚动内容时不会将底部sheet滚动到展开状态result。
预期
我预计上面的代码应该是这样的expectation
有人有解决这种情况的方法吗?
谢谢
所以经过一个多星期,我找到了解决这个问题的方法,
我重写了 onCreateDialog 方法,如下所示
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
// This will disable the behavior of bottom sheet to have a flat corner
//https://github.com/material-components/material-components-android/pull/437#issuecomment-678742683
val dialog = BottomSheetDialog(requireContext(), theme)
dialog.behavior.disableShapeAnimations()
dialog.setOnShowListener { dialogInterface ->
val bottomSheetDialog = dialogInterface as BottomSheetDialog
val parentLayout =
bottomSheetDialog.findViewById<View>(com.google.android.material.R.id.design_bottom_sheet)
parentLayout?.let { it1 ->
val behaviour = BottomSheetBehavior.from(it1)
it1.layoutParams.also {
it.height =
context?.resources?.displayMetrics?.heightPixels?.times(0.9)?.toInt()!!
}
behaviour.peekHeight =
context?.resources?.displayMetrics?.heightPixels?.times(0.6)?.toInt()!!
}
}
return dialog
}
这段代码发生了什么?
我既没有使用 BottomSheetDialog 的对话框提供程序,也没有使用 super.onCreateDialog()
相反,我使用 BottomSheetDialog(context,theme) 来创建我的自定义底部 sheet 主题。
然后,我为对话框设置侦听器,获取父布局和底部 sheet 对话框,如下所示。最重要的部分是处理行为。
正确实现行为后,您就完成了。将其状态设置为按照@Nitish Chaudhary 的建议进行扩展,并将布局高度设置为您想要的高度。这就是我所需要的,无论您遵循模态底部 sheet XML 布局规则,下面的代码都可以重复使用。
编码愉快。
我已经解决了需要实现可滚动模式底部的情况 sheet。基本上,一开始它会显示一半的屏幕,然后向上滚动时,它会拉伸到屏幕的 90%。如果继续向上滑动,它会滚动底部的内容 sheet.
我试过的
layout.xml
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/design_bottom_sheet"
style="@style/RootLayout"
android:background="@drawable/rounded_shape"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<...Content.../>
</android.core.widget.NestedScrollView>
</FrameLayout>
<androidx.coordinatorlayout.widget.CoordinatorLayout
Bottom Sheet Dialog class
我按照这里的回答
结果
打开时只显示一半屏幕,像这样向上滚动内容时不会将底部sheet滚动到展开状态result。
预期
我预计上面的代码应该是这样的expectation
有人有解决这种情况的方法吗? 谢谢
所以经过一个多星期,我找到了解决这个问题的方法, 我重写了 onCreateDialog 方法,如下所示
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
// This will disable the behavior of bottom sheet to have a flat corner
//https://github.com/material-components/material-components-android/pull/437#issuecomment-678742683
val dialog = BottomSheetDialog(requireContext(), theme)
dialog.behavior.disableShapeAnimations()
dialog.setOnShowListener { dialogInterface ->
val bottomSheetDialog = dialogInterface as BottomSheetDialog
val parentLayout =
bottomSheetDialog.findViewById<View>(com.google.android.material.R.id.design_bottom_sheet)
parentLayout?.let { it1 ->
val behaviour = BottomSheetBehavior.from(it1)
it1.layoutParams.also {
it.height =
context?.resources?.displayMetrics?.heightPixels?.times(0.9)?.toInt()!!
}
behaviour.peekHeight =
context?.resources?.displayMetrics?.heightPixels?.times(0.6)?.toInt()!!
}
}
return dialog
}
这段代码发生了什么?
我既没有使用 BottomSheetDialog 的对话框提供程序,也没有使用 super.onCreateDialog()
相反,我使用 BottomSheetDialog(context,theme) 来创建我的自定义底部 sheet 主题。
然后,我为对话框设置侦听器,获取父布局和底部 sheet 对话框,如下所示。最重要的部分是处理行为。
正确实现行为后,您就完成了。将其状态设置为按照@Nitish Chaudhary 的建议进行扩展,并将布局高度设置为您想要的高度。这就是我所需要的,无论您遵循模态底部 sheet XML 布局规则,下面的代码都可以重复使用。
编码愉快。