简体   繁体   English

使用 Jetpack compose 实现 CollapsingToolbar

[英]Implement CollapsingToolbar using Jetpack compose

I am trying to implement collapsing toolbar in my Detail screen using Jetpack compose : https://github.com/alirezaeiii/Compose-MultiModule-Cache/blob/master/feature_list/src/main/java/com/android/sample/app/feature/list/ui/detail/DetailsScreen.kt我正在尝试使用 Jetpack compose 在我的详细信息屏幕中实现折叠工具栏: https ://github.com/alirezaeiii/Compose-MultiModule-Cache/blob/master/feature_list/src/main/java/com/android/sample/app /feature/list/ui/detail/Det​​ailsS​​creen.kt

val toolbarHeightPx = with(LocalDensity.current) {
        278.dp.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()
            // attach as a parent to the nested scroll system
            .nestedScroll(nestedScrollConnection)
    ) {
        DetailsContent(
            scrollState = scrollState,
            onNamePosition = { newNamePosition ->
                // Comparing to Float.MIN_VALUE as we are just interested on the original
                // position of name on the screen
                if (detailScroller.namePosition == Float.MIN_VALUE) {
                    detailScroller =
                        detailScroller.copy(namePosition = newNamePosition)
                }
            },
            item = item,
            boxHeight = with(LocalDensity.current) {
                440.dp + toolbarOffsetHeightPx.value.toDp()
            },
            imageHeight = with(LocalDensity.current) {
                420.dp + toolbarOffsetHeightPx.value.toDp()
            },
            sendNotification = sendNotification,
            contentAlpha = { contentAlpha.value }
        )
        DetailsToolbar(
            toolbarState, item.name, pressOnBack,
            contentAlpha = { contentAlpha.value }
        )
    }

The idea is taken from sunflower Google Github project.这个想法来自向日葵谷歌 Github 项目。 When we scroll up it works as expected but when we scroll down, it will not sometimes fully scrolled.当我们向上滚动时,它按预期工作,但是当我们向下滚动时,它有时不会完全滚动。 toolbarOffsetHeightPx should become 0 when we scroll down, but sometimes it is a negative value that cause image does not fully scrolled.当我们向下滚动时, toolbarOffsetHeightPx应该变为 0,但有时它是一个负值,导致图像没有完全滚动。 It is not stable at all and 0 or any negative value may happen.它根本不稳定,可能会出现 0 或任何负值。 it happens since we have :它发生是因为我们有:

boxHeight = with(LocalDensity.current) {
                440.dp + toolbarOffsetHeightPx.value.toDp()
            },
imageHeight = with(LocalDensity.current) {
                420.dp + toolbarOffsetHeightPx.value.toDp()
            }

Why is that and how to resolve it?为什么会这样以及如何解决?

我将其报告为问题跟踪器中的一个小错误: https ://issuetracker.google.com/issues/238177355

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

相关问题 在 Jetpack Compose 中使用 ViewModel 实现 startActivity 的最佳实践 - Best practice to implement startActivity using ViewModel in Jetpack Compose 如何使用 Android Jetpack Compose 实现 BottomAppBar 和 BottomDrawer 模式? - How to implement BottomAppBar and BottomDrawer pattern using Android Jetpack Compose? 使用 jetpack compose 的麻烦 - The trouble with using jetpack compose 在 Jetpack compose 中使用 textAppearance - using textAppearance in Jetpack compose 如何在 Jetpack Compose 中实现平移 + 缩放动画? - How to implement a translate + scale animation in Jetpack Compose? 如何使用 Jetpack Compose 在应用程序中实现本地化 - How to implement in app localization with Jetpack Compose 无法在 Jetpack Compose 中实现 ViewModel - Can't implement ViewModel in Jetpack Compose 如何在 Material 3 Jetpack Compose 中实现 BottomSheet Android - How to implement BottomSheet in Material 3 Jetpack Compose Android 如何在 Jetpack Compose 中实现搜索 - Android - How to implement search in Jetpack Compose - Android 如何在 Jetpack Compose 中实现 list multiSelect? - How to implement list multiSelect in Jetpack Compose?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM