There are two implementations of this layout. The fitting implementation depends on your root composeable for the content below the top app bar. If you only need Column as your root, use CollapsingTopAppBarLayout. If your screen content is a LazyColumn than use the CollapsingTopAppBarLazyLayout implementation.
See the exact usage for your needs:
Column implementation
val collapsingTopAppBarState = rememberCollapsingTopAppBarColumnState()
CollapsingTopAppBarLayout(
modifier = Modifier.fillMaxSize(),
state = collapsingTopAppBarState,
barStaticContent = { collapsingState: CollapsingState ->
BarStaticContent(collapsingState)
},
barStaticBackgroundColor = MaterialTheme.colors.primary,
barCollapsingContent = { collapsingState: CollapsingState ->
BarCollapsingContent(collapsingState)
},
barCollapsingBackgroundColor = MaterialTheme.colors.primaryVariant,
barCollapsingRadiusBottomStart = 16.dp,
barCollapsingRadiusBottomEnd = 16.dp,
endedInPartialTransitionStrategy = EndedInPartialTransitionStrategy.CollapseOrExpandToNearest(),
screenContent = {
MainContent()
}
)state: Optional. Can be used to pass an ownCustomScrollStateorCoroutineScope.barStaticContent: The composable that defines the top part (that will never scroll away). You receive the currentCollapsingStateif the scrolling is in progress. This can be used to animate your content fluently according to the current scrolling progress.barStaticBackgroundColor: Optional. Defaults toMaterialTheme.colors.primary.barCollapsingContent: The composable that defines the collapsing part of the top app bar. This will shrink on scrolling down thescreenContent. You receive the currentCollapsingStateif the scrolling is in progress. This can be used to animate your content fluently according to the current scrolling progress.barCollapsingBackgroundColor: Optional. Defaults toMaterialTheme.colors.primaryVariant.barCollapsingRadiusBottomStart: Optional. Defaults to0dp.barCollapsingRadiusBottomEnd: Optional. Defaults to0dp.endedInPartialTransitionStrategy: Optional. Defaults toEndedInPartialTransitionStrategy.CollapseOrExpandToNearest(). This defines how thebarCollapsingContentbehaves when the scrolling ended in state where it is not fully collapsed or expanded. Have a look into the java docs fromEndedInPartialTransitionStrategyto see all possible strategies.screenContent: This is the main screen content composable. Your Content is being composed in aColumnScopeon root level.
Lazy Column implementation
val collapsingTopAppBarState = rememberCollapsingTopAppBarLazyColumnState()
CollapsingTopAppBarLazyLayout(
modifier = Modifier.fillMaxSize(),
state = collapsingTopAppBarState,
barStaticContent = { collapsingState: CollapsingState ->
BarStaticContent(collapsingState)
},
barStaticBackgroundColor = MaterialTheme.colors.primary,
barCollapsingContent = { collapsingState: CollapsingState ->
BarCollapsingContent(collapsingState)
},
barCollapsingBackgroundColor = MaterialTheme.colors.primaryVariant,
barCollapsingRadiusBottomStart = 16.dp,
barCollapsingRadiusBottomEnd = 16.dp,
endedInPartialTransitionStrategy = EndedInPartialTransitionStrategy.CollapseOrExpandToNearest(),
screenContent = {
mainContentLazyColumn()
}
)state: Optional. Can be used to pass an ownLazyListStateorCoroutineScope.barStaticContent: The composable that defines the top part (that will never scroll away). You receive the currentCollapsingStateif the scrolling is in progress. This can be used to animate your content fluently according to the current scrolling progress.barStaticBackgroundColor: Optional. Defaults toMaterialTheme.colors.primary.barCollapsingContent: The composable that defines the collapsing part of the top app bar. This will shrink on scrolling down thescreenContent. You receive the currentCollapsingStateif the scrolling is in progress. This can be used to animate your content fluently according to the current scrolling progress.barCollapsingBackgroundColor: Optional. Defaults toMaterialTheme.colors.primaryVariant.barCollapsingRadiusBottomStart: Optional. Defaults to0dp.barCollapsingRadiusBottomEnd: Optional. Defaults to0dp.endedInPartialTransitionStrategy: Optional. Defaults toEndedInPartialTransitionStrategy.CollapseOrExpandToNearest(). This defines how thebarCollapsingContentbehaves when the scrolling ended in state where it is not fully collapsed or expanded. Have a look into the java docs fromEndedInPartialTransitionStrategyto see all possible strategies.screenContent: This is the main screen content. Your Content is being composed in aLazyListScopeon root level.
Add it in your root build.gradle at the end of repositories
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
Add the dependency to your module
dependencies {
implementation 'com.github.leonard-palm:compose-collapsing-app-bar:1.2.0'
}