Mer*_*rig 7 android kotlin android-jetpack android-jetpack-compose
我找不到有关此事的任何文档,是否有类似于CollapsingToolbarCompose 中的内容?
我发现的只是这里提到了它,但没有关于如何设置它
Man*_*eru 41
Material Design 3的 Jetpack Compose 实现包括 4 种类型的顶级应用栏 ( https://m3.material.io/components/top-app-bar/implementation ):
CenterAlignedTopAppBarSmallTopAppBarMediumTopAppBarLargeTopAppBarhttps://developer.android.com/reference/kotlin/androidx/compose/material3/package-summary
它们都有一个scrollBehavior参数,可用于折叠工具栏。库中有 3 种基本类型的滚动行为:
TopAppBarDefaults.pinnedScrollBehaviorTopAppBarDefaults.enterAlwaysScrollBehaviorTopAppBarDefaults.exitUntilCollapsedScrollBehaviorhttps://developer.android.com/reference/kotlin/androidx/compose/material3/TopAppBarDefaults
注意:此 API 目前被注释为实验性的。
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun Test() {
val scrollBehavior = TopAppBarDefaults.enterAlwaysScrollBehavior(rememberTopAppBarState())
Scaffold(
modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection),
topBar = {
MediumTopAppBar(
title = { Text(text = "Scroll Behavior Test") },
navigationIcon = {
IconButton(onClick = { /*TODO*/ }) {
Icon(imageVector = Icons.Default.Menu, contentDescription = "")
}
},
scrollBehavior = scrollBehavior
)
}
) {
LazyColumn(modifier = Modifier.fillMaxWidth()) {
items((1..50).toList()) { item ->
Text(modifier = Modifier.padding(8.dp), text = "Item $item")
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
ngl*_*ber 19
我找到了 Samir Basnet(来自 Kotlin Slack Channel)创建的解决方案,这对我很有用,我希望它对其他人有帮助......
@Composable
fun CollapsingEffectScreen() {
val items = (1..100).map { "Item $it" }
val lazyListState = rememberLazyListState()
var scrolledY = 0f
var previousOffset = 0
LazyColumn(
Modifier.fillMaxSize(),
lazyListState,
) {
item {
Image(
painter = painterResource(id = R.drawable.recife),
contentDescription = null,
contentScale = ContentScale.FillWidth,
modifier = Modifier
.graphicsLayer {
scrolledY += lazyListState.firstVisibleItemScrollOffset - previousOffset
translationY = scrolledY * 0.5f
previousOffset = lazyListState.firstVisibleItemScrollOffset
}
.height(240.dp)
.fillMaxWidth()
)
}
items(items) {
Text(
text = it,
Modifier
.background(Color.White)
.fillMaxWidth()
.padding(8.dp)
)
}
}
}
Run Code Online (Sandbox Code Playgroud)
结果如下:
您可以使用compose-collapsing-toolbar库。
安装:implementation "me.onebone:toolbar-compose:2.1.0"
以下是该库 Readme.md 中的一些 gif 图像:

我在 Android 文档中发现了这一点,我认为您在问题中链接的文档正在谈论使用嵌套滚动来执行此操作。
val toolbarHeight = 48.dp
val toolbarHeightPx = with(LocalDensity.current) { toolbarHeight.roundToPx().toFloat() }
val toolbarOffsetHeightPx = remember { mutableStateOf(0f) }
val nestedScrollConnection = remember {
object : NestedScrollConnection {
override fun onPreScroll(available: Offset, source: NestedScrollSource): Offset {
val delta = available.y
val newOffset = toolbarOffsetHeightPx.value + delta
toolbarOffsetHeightPx.value = newOffset.coerceIn(-toolbarHeightPx, 0f)
return Offset.Zero
}
}
}
Box(
Modifier
.fillMaxSize()
.nestedScroll(nestedScrollConnection)
) {
LazyColumn(contentPadding = PaddingValues(top = toolbarHeight)) {
items(100) { index ->
Text("I'm item $index", modifier = Modifier
.fillMaxWidth()
.padding(16.dp))
}
}
TopAppBar(
modifier = Modifier
.height(toolbarHeight)
.offset { IntOffset(x = 0, y = toolbarOffsetHeightPx.value.roundToInt()) },
title = { Text("toolbar offset is ${toolbarOffsetHeightPx.value}") }
)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2296 次 |
| 最近记录: |