material-components-android
material-components-android copied to clipboard
[BottomSheet] Going from STATE_HIDDEN to STATE_COLLAPSED makes strange animation
Description:
In simple apps, calling bottomSheetBehavior.state = BottomSheetBehavior.STATE_COLLAPSED when it is STATE_HIDDEN starts the animation from the bottom of the screen, and eases up into the peek height.
For some reason, in my fork of UAMP, when showing the bottom sheet for the first time by setting the state to collapsed from hidden, the animation starts at about 25% of the way down from the top of the screen (more than half expanded) and within several milliseconds rests at STATE_SETTLED, creating a jarring visual experience.
To reproduce the error, in the sample app, click a card to start the audio player and show the bottom sheet. This only happens the first time a card is clicked after a cold startup.
Expected behavior: Bottom sheet appears in an easing animation animating upwards from the bottom of the screen until it reaches its peek height. What actually happens: bottom sheet appears more than half expanded and animates downwards until it reaches its peek height.
Source code:
private fun showBottomSheetIfNotVisible() {
if (!bottomSheetIsVisible) {
bottomSheetIsVisible = true
behavior.state = BottomSheetBehavior.STATE_COLLAPSED
val params = fragmentContainer.layoutParams as ConstraintLayout.LayoutParams
params.bottomMargin = resources.getDimension(R.dimen.bottomsheet_header_height).toInt()
fragmentContainer.layoutParams = params
}
}
Minimal sample app repro: https://github.com/shmueldabomb441/BottomSheetErrorReportingReproduction (Note: there are a few classes related to the media player and unrelated to the bottom sheet error - I tried to cut away as much as I could while retaining the bug; as far as I can tell, all of the relevant code is in MainActivity.)
Android API version: 32
Material Library version: 1.8.0-alpha01
Device: OnePlus 8T
Any updates? I think the progress bar loading prevents the bottom sheet from closing, because once the progress bar disappears (because the audio starts playing), the bottom sheet closes.
I had a very similar issue. If I un-hid the bottom sheet fast enough on startup, it would snap to the top and then animate back to the collapsed state. If I un-hid it later, that would not occur.
Then again, I personally found that it was caused by turning off hideability before changing the state, which doesn't seem to be occuring on your end. My bottom sheet is also much heavier than yours (Two nested sheets with a bunch of views and animations), so I imagine it was just general instability caused by me overloading the component.
Any updates after a month?
@drchen what happened? Is my sample repo too big? Still getting it with the newest API and the most up to date versions of every dependency.
Is this still happening in latest version?
Yes.
Yes.
You may try to fix it by running the layout params change in a post on fragmentContainer - fragmentContainer.post{ // change layout code }. This should wait for collapse's operations to finish. It may not work for any scenario, but I had luck with this approach for my scenario.
Hunter can you help triage as I'm no longer actively working on Views?