[英]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
}
}
}
}
現在列表以正確的方式滾動。
如果您在啟動底部工作表對話框片段的 Activity 中使用 compose,則最好堅持使用純粹的 compose 實現並利用 compose 等效的底部工作表組件:ModalBottomSheetLayout
你可以試試這個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.