如何在 BottomSheetDialogFragment 中正确使用 Jetpack Compose?

Mik*_*ike 10 android-jetpack-compose android-bottomsheetdialog bottomsheetdialogfragment android-jetpack-compose-list lazycolumn

例如,我在应用程序中有 MyBottomSheetDialogFragment 和 Compose LazyColumn 代码:

class MyBottomSheetDialogFragment : BottomSheetDialogFragment() {
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        return ComposeView(requireContext()).apply {
            setContent {
                Column(horizontalAlignment = Alignment.CenterHorizontally) {
                    Text("Header", color = Color.Black)
                    LazyColumn(
                        Modifier
                            .weight(1f)
                            .fillMaxWidth()) {
                        items(100) {
                            Text("Item $it", Modifier.fillMaxWidth(), Color.Black)
                        }
                    }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

并使用以下代码显示它:

MyBottomSheetDialogFragment().show(activity.supportFragmentManager, null)
Run Code Online (Sandbox Code Playgroud)

这就是我们所拥有的:

MyBottomSheetDialogFragment 屏幕图像.jpg

现在,如果向下滚动 LazyColumn 列表,则一切都会正常工作,但如果向上滚动 LazyColumn 列表,则底部工作表对话框将滚动,而不是 LazyColumn 列表。

如何在 BottomSheetDialogFragment 中正确实现 LazyColumn?

当我们使用 XML RecyclerView 列表时,要解决此问题,我们必须使用 NestedScrollView 包装 RecyclerView 列表,如此处所述,但是如何使用 Jetpack Compose 修复它?

Luc*_*ano 0

您应该使用 a并将与底部工作表内容一起BottomSheetScaffold设置。sheetContent不要使用 BottomSheetDialogFragment。

下面是 BottomSheetScaffold 基本结构的示例:

    val scaffoldState = rememberBottomSheetScaffoldState(
        bottomSheetState = rememberBottomSheetState(BottomSheetValue.Expanded)
    )

    BottomSheetScaffold(
        modifier = Modifier.navigationBarsPadding(),
        scaffoldState = scaffoldState,
        topBar = {
            // Your topBar
        },
        sheetShape = BottomSheetShape,
        sheetPeekHeight = MaterialTheme.spacing.large,
        sheetContent = {
            // Your sheet content
        }
    ) { innerPadding ->
      // You content
    }

Run Code Online (Sandbox Code Playgroud)

  • 如果我错了,请纠正我,但是 `// You content` 不一定是可组合项?对我来说,这个 API 看起来像是仅限 Compose,具有奇怪的结构,其中 BottomSheetScaffold 被视为屏幕其余部分的父容器。我认为 OP 希望在 View 和 Composables 之间以标准方式进行互操作,以与平常相同的方式显示 BottomSheetDialogFragment (不需要内部 lambda 内部的外部内容,只需在需要的地方放置一个 View/Composable 即可)。可以做吗? (2认同)