问题 - 向下滚动会导致底部表单滚动而不是优先给予LazyColumn滚动(RecyclerView没有这个问题。它被NestedScrollView包装)
我刚刚在CoordinatorLayout中引入了Compose LazyColumn替换Recycler。Coordinator(作为底部表单实现)可以在收起和展开状态之间自由滚动。我的问题是,在LazyColumn中向下拖动项目区域时,底部表单会接替滚动而不是LazyColumn。如果我先向上滚动,然后再向下滚动(没有释放),则LazyColumn会接替滚动,并且滚动优先级会给予LazyColumn(预期行为)。
BottomSheetFragment
|-CoordinatorLayout
|--ConstraintLayout (BottomSheetBehavior)
|---MyListFragment
|----ComposeView
|-----Theme
|------Surface
|-------Box
|--------LazyColumn
我刚开始使用Compose,希望有人告诉我如何解决这种新的滚动行为?
**编辑 通过切换Coordinator的^^ BottomSheetBehavior.isDragglable,我已经部分实现了这个功能,但需要释放拖动而不是平稳地从列表滚动到底部表单滚动 - 有人能建议一个修复方法吗?
fun MyUi(listener:Listener) {
val listState = rememberLazyListState()
LaunchedEffect(listState) {
listState.interactionSource.interactions.collect {
//at the top of the list so allow sheet scrolling
listener.allowSheetDrag(listState.firstVisibleItemScrollOffset == 0)
}
}
val nestedScrollConnection = remember {
object : NestedScrollConnection {
override fun onPreScroll(available: Offset, source: NestedScrollSource): Offset {
Timber.i("NestedScrollConnection onPreScroll($available: Offset, $source: NestedScrollSource)")
return super.onPreScroll(available, source)
}
override fun onPostScroll(consumed: Offset, available: Offset, source: NestedScrollSource): Offset {
Timber.i("NestedScrollConnection onPostScroll($consumed: Offset, $available: Offset, $source: NestedScrollSource)")
if (available.y > 0.0 && consumed.y == 0.0f) {
//scolling down up but we're already at the top - kick over to sheet scrolling
listener.allowSheetDrag(true)
}
return super.onPostScroll(consumed, available, source)
}
}
}
Box(
modifier = Modifier
.fillMaxSize()
.nestedScroll(nestedScrollConnection)
) {
LazyColumn(
modifier =
Modifier
.fillMaxSize()
.padding(vertical = 12.dp), state = listState
) {
item {
Row() {}
}
}
}
}
然后在Fragment中:
override fun allowSheetDrag(allowSheetDrag: Boolean) {
bottomSheetFragment?.bottomSheetBehavior?.isDraggable = allowSheetDrag
}