Open edharkhimich opened 2 years ago
I looked into the behavior of androidx's CollapsingToolbarLayout, looks like it collapses only if the height of a body layout is large enough to scroll which is different from the current behavior of my library.
One point I want to think of is when the height of the body layout is somewhere between body hole + collapsed toolbar
and body hole + expanded toolbar
. AndroidX leaves some space if the actual height of body layout is smaller than that of body hole, maybe I could give some options to choose this behavior?
Thank you for so quick response.
Yes, in CollapsingToolbarLayout it checks if the content height is more than the screen height and only then make it collapsable when scrolling. In your library it scrolls and collapse always.
Sorry but I didn't get your question:
AndroidX leaves some space if the actual height of body layout is smaller than that of body hole, maybe I could give some options to choose this behavior?
Could you please rephrase it ?
Is there any workaround for this case? For example disable vertical scroll if content fits to screen. But how to detect "if content fits"?
I meant the edge case where the body content's height is not tall enough to cover the whole screen when the toolbar is collapsed but it fills the screen when toolbar is expanded.
By the way, I didn't tested myself though did you try calculating available height with CollapsingToolbarState.maxHeight
as a workaround?
This workaround works for me:
@Composable
public fun CollapsingToolbarScaffold(
state: CollapsingToolbarScaffoldState,
modifier: Modifier = Modifier,
scrollStrategy: ScrollStrategy = ScrollStrategy.ExitUntilCollapsed,
enabled: Boolean = true,
toolbarModifier: Modifier = Modifier,
toolbar: @Composable CollapsingToolbarScope.() -> Unit,
content: @Composable BoxWithConstraintsScope.() -> Unit,
) {
var collapsable by remember { mutableStateOf(false) }
LaunchedEffect(collapsable) {
@OptIn(ExperimentalToolbarApi::class)
if (!collapsable) state.toolbarState.expand()
}
CollapsingToolbarScaffold(
modifier = modifier,
toolbarModifier = toolbarModifier,
state = state,
enabled = collapsable && enabled, // Force disable collapsing if it is not needed
scrollStrategy = scrollStrategy,
toolbar = toolbar,
body = {
var contentHeight by remember { mutableStateOf(0) }
BoxWithConstraints(Modifier.onGloballyPositioned { contentHeight = it.size.height }) {
collapsable = with(LocalDensity.current) {
val toolbarMaxOffset = state.toolbarState.maxHeight - state.toolbarState.minHeight
contentHeight > maxHeight.toPx() - toolbarMaxOffset
}
content()
}
},
)
}
UPD: Simplified workaround
Hi, I have already created PR for this: https://github.com/onebone/compose-collapsing-toolbar/pull/85
By the way, if you want to use this feature, I created a separated remote dependency, while this PR is under review:
Add it in your root build.gradle at the end of repositories:
all projects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Step 2. Add the dependency
dependencies {
implementation 'com.github.GIGAMOLE:ComposeCollapsingToolbar:latest-version'
}
Or you can simply download it from there:
https://github.com/GIGAMOLE/ComposeCollapsingToolbar/releases
The content is scrollable even when it's 1 item in the list and half of the screen is empty. How to detect if the content takes the whole screen and make it scrollable only then ?