繁体   English   中英

BottomSheetDialogFragment 内的 LazyColumn 滚动问题

[英]Scroll issue with LazyColumn inside BottomSheetDialogFragment

我在BottomSheetDialogFragment中使用LazyColumn ,但是如果向上滚动LazyColumn列表,那么Bottom工作表对话框将滚动而不是LazyColumn列表。 似乎BottomSheetDialogFragment拦截了用户触摸输入。

这就是它的样子:

如何在BottomSheetDialogFragment中正确使用LazyColumn

MyBottomSheetDialogFragment.kt:

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)
                        }
                    }
                }
            }
        }
    }
}

并使用此代码显示它:

MyBottomSheetDialogFragment().show(activity.supportFragmentManager, null)

当我们使用 XML RecyclerView列表时,为了解决这个问题,我们必须像这里描述的那样用NestedScrollView包装RecyclerView列表,但是如何用 Jetpack Compose 修复它呢?

由于 compose 1.2.0-beta01问题可以通过使用rememberNestedScrollInteropConnection来解决:

Modifier.nestedScroll(rememberNestedScrollInteropConnection())

在我的例子中, BottomSheetDialogFragment是标准View ,它有ComposeView和 id container onViewCreated我做的:

binding.container.setContent {
    AppTheme {
        Surface(
            modifier = Modifier.nestedScroll(rememberNestedScrollInteropConnection())
        ) {
            LazyColumn {
                // ITEMS
            }
        }
    }
}

现在列表以正确的方式滚动。

你可以试试这个https://gist.github.com/chrisbanes/053189c31302269656c1979edf418310

这是https://issuetracker.google.com/issues/174348612的解决方法,这意味着 Compose 中的嵌套滚动布局不能作为视图系统中的嵌套滚动子项工作。

您的案例中的示例用法:

class MyBottomSheetDialogFragment : BottomSheetDialogFragment() {
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        return ComposeView(requireContext()).apply {
            setContent {
                Surface(
                    modifier = Modifier.nestedScroll(rememberViewInteropNestedScrollConnection())
                ){
                    LazyColumn(
                        Modifier
                            .weight(1f)
                            .fillMaxWidth()) {
                        items(100) {
                            Text("Item $it", Modifier.fillMaxWidth(), Color.Black)
                        }
                    }
                }
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM