【发布时间】:2021-05-27 18:12:32
【问题描述】:
我期待在LazyColumn 列表上滚动时实现折叠效果的方法。我一直在检查文档,但没有发现任何相关内容。如何实现?
目前我正在使用 BottomNavigation 设置在我的 Scaffold 中,我可以将内部填充添加到来自脚手架内容 lambda 的屏幕。但我没有找到任何类型的可滚动状态或类似的东西。
【问题讨论】:
标签: android android-jetpack-compose
我期待在LazyColumn 列表上滚动时实现折叠效果的方法。我一直在检查文档,但没有发现任何相关内容。如何实现?
目前我正在使用 BottomNavigation 设置在我的 Scaffold 中,我可以将内部填充添加到来自脚手架内容 lambda 的屏幕。但我没有找到任何类型的可滚动状态或类似的东西。
【问题讨论】:
标签: android android-jetpack-compose
您可以使用nestedScroll 修饰符。
类似:
val bottomBarHeight = 48.dp
val bottomBarHeightPx = with(LocalDensity.current) { bottomBarHeight.roundToPx().toFloat() }
val bottomBarOffsetHeightPx = remember { mutableStateOf(0f) }
// connection to the nested scroll system and listen to the scroll
// happening inside child LazyColumn
val nestedScrollConnection = remember {
object : NestedScrollConnection {
override fun onPreScroll(available: Offset, source: NestedScrollSource): Offset {
val delta = available.y
val newOffset = bottomBarOffsetHeightPx.value + delta
bottomBarOffsetHeightPx.value = newOffset.coerceIn(-bottomBarHeightPx, 0f)
return Offset.Zero
}
}
}
然后将nestedScroll 应用到脚手架:
Scaffold(
Modifier.nestedScroll(nestedScrollConnection),
scaffoldState = scaffoldState,
//..
bottomBar = {
BottomAppBar(modifier = Modifier
.height(bottomBarHeight)
.offset { IntOffset(x = 0, y = -bottomBarOffsetHeightPx.value.roundToInt()) }) {
IconButton(
onClick = {
coroutineScope.launch { scaffoldState.drawerState.open() }
}
) {
Icon(Icons.Filled.Menu, contentDescription = "Localized description")
}
}
},
content = { innerPadding ->
LazyColumn(contentPadding = innerPadding) {
items(count = 100) {
Box(
Modifier
.fillMaxWidth()
.height(50.dp)
.background(colors[it % colors.size])
)
}
}
}
)
【讨论】:
if 条件?只是问一下,我不习惯在撰写中思考。我有点害怕这会导致未来的重构。如果我想在某些屏幕上实现折叠效果,而在其他屏幕上不需要折叠效果,我最终需要为显示底栏的路线创建一个if ,添加scrollConnection modifier 并修改InnerPadding 修饰符以获得额外的填充在底部。非常感谢您的回答!