Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] kotlin – Custom CollapsingTopAppBar Jetpack Compose

Posted on November 11, 2022 By

Solution 1 :

you need to calculate the height that the appbar will have before drawing it into the screen. I have followed this issue and solved my problem with the last solution. hope it helps:

Get height of element Jetpack Compose

  • use the content you can put (ex. an image or a huge text) as the MainContent
  • use your appbar as the DependentContent and use the size given in lambda to give the height to your appbar
  • finally set placeMainContent false as I believe you don’t need to draw the image (or any other composable) directly in a box

and you will good to go

Problem :

The essence of the problem is that I want to write my own version of the AppBar that would include content as another Compose function. After looking at the source code of the current CollapsingTopAppBar implementation, I saw the following lines:

@Composable
private fun TwoRowsTopAppBar(
    ...
    scrollBehavior: TopAppBarScrollBehavior?
) {
    ...
    val pinnedHeightPx: Float = 64.dp
    val maxHeightPx: Float = 152.dp

    LocalDensity.current.run {
        pinnedHeightPx = pinnedHeight.toPx()
        maxHeightPx = maxHeight.toPx()
    }
    // Sets the app bar's height offset limit to hide just the bottom title area and keep top title
    // visible when collapsed.
    SideEffect {
        if (scrollBehavior?.state?.heightOffsetLimit != pinnedHeightPx - maxHeightPx) {
            scrollBehavior?.state?.heightOffsetLimit = pinnedHeightPx - maxHeightPx
        }
    }
    ...
    Surface(...) {
        Column {
            TopAppBarLayout(
                ...
                heightPx = pinnedHeightPx
                ...
            )
            TopAppBarLayout(
                ...
                heightPx = maxHeightPx - pinnedHeightPx + (scrollBehavior?.state?.heightOffset
                   ?: 0f),
                ...
            )
        }
    }
}

As I understand it, scrollBehavior is used to handle the collapse and expansion behavior. In the current implementation, just constant values are put in heightOffsetLimit. And since I need my appbar implementation to be able to contain content of any size, I need to somehow know the size of this content in advance and put this value in heightOffsetLimit.

I have already written the code for my AppBar, so that it also contains content. But since I can’t pass the height value of the content to scrollBehavior, the AppBar doesn’t collapse to the end.

READ  [FIXED] android - How to get instance of the Application Class?
Powered by Inline Related Posts

Comments

Comment posted by Жавохир Исаев

Thank you very much! This is exactly what I needed. It is a pity that there is so little information about SubcomposeLayout in the official documentation

Android Tags:android-jetpack-compose, kotlin

Post navigation

Previous Post: [FIXED] android – LottieAnimationView doesn’t show in my_layout.xml
Next Post: [FIXED] android – Firestore conditional array query

Related Posts

[FIXED] kotlin – Android TextInputLayout Exposed Dropdown Menu Android
[FIXED] android – How Do I Retrieve A Selected Image Using onActivityResult? Android
[FIXED] Android Kotlin: Translate animation on view is not working Android
[FIXED] android – Nexus7 can’t load native library Android
[FIXED] android – close prompt after three failure attempts in androidx biometric prompt? Android
[FIXED] java – After ItemClickListener call methot Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What are the main features of Islamic education?
  • Is the Jeep 4xe worth it?
  • How does the ringer work on cast iron?
  • What is the biggest size interior door?
  • Is blue raspberry an original Jolly Rancher flavor?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme