material-components-android icon indicating copy to clipboard operation
material-components-android copied to clipboard

[BottomSheet] Going from STATE_HIDDEN to STATE_COLLAPSED makes strange animation

Open Sternbach-Software opened this issue 3 years ago • 7 comments
trafficstars

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

Sternbach-Software avatar Oct 13 '22 03:10 Sternbach-Software

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.

Sternbach-Software avatar Oct 30 '22 16:10 Sternbach-Software

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.

OxygenCobalt avatar Nov 03 '22 15:11 OxygenCobalt

Any updates after a month?

Sternbach-Software avatar Nov 16 '22 04:11 Sternbach-Software

@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.

Sternbach-Software avatar Dec 07 '22 05:12 Sternbach-Software

Is this still happening in latest version?

avladgh avatar Jun 05 '24 19:06 avladgh

Yes.

Sternbach-Software avatar Jun 06 '24 07:06 Sternbach-Software

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.

avladgh avatar Jun 06 '24 14:06 avladgh

Hunter can you help triage as I'm no longer actively working on Views?

drchen avatar Feb 10 '25 16:02 drchen