I have a BottomSheetView
which has animateLayoutChanges="true"
. Initially it shows up fine. But if change the visibility
of a view (inside BottomSheetView
) from GONE
to VISIBLE
, the app messes up calculations and my BottomSheetView
moves to the top of the screen. i have tried setting layout_gravity=bottom
at the root of the BottomSheetView
layout. But no success.
Here I have the image of my BottomSheetView
before changing the visibility of any view. (Click image for full size)
After I change the visibility of a view (GONE
to VISIBLE
or VISIBLE
to GONE
), my BottomSheetView moves to the top. (Click image for full size)
I guess, Android is messing up while making calculations about the measurement of view width
and height
. Any way to solve this??
I also tried to make my BottomSheetView extend fully to match the parent view, but somehow that is making the height
of the BottomSheetView
longer than the phone screen and in-tun creating scrolling issues.
Expected solutions:
1> Prevent BottomSheetView
from changing its position even when the visibility
of a view is changed.
OR
2>Make the BottomSheetView
match parent so that it does not look bad after messing up with the calculations.
The BottomSheetBehavior does not work well with LayoutTransition
(animateLayoutChanges="true"
) for now. I will work on a fix.
For now, you can use Transition
instead. Something like this will fade the view inside and animate the size of the bottom sheet.
ViewGroup bottomSheet = ...;
View hidingView = ...;
TransitionManager.beginDelayedTransition(bottomSheet);
hidingView.setVisibility(View.GONE);
You can refer to Applying a Transition for more information including how to customize the animation.
I was running into the same issue and determined to find a fix. I was able to find the underlying cause but unfortunately I do not see a great fix at the moment.
The Cause:
The problem occurs between the bottomsheet behavior and the LayoutTransition. When the LayoutTransition is created, it creates a OnLayoutChangeListener on the view so that it can capture its endValues and setup an animator with the proper values. This OnLayoutChangeListener is triggered in the bottomSheetBehavior's onLayout()
call when it first calls parent.onLayout(child)
. The parent will layout the child as it normally would, ignoring any offsets that the behavior would change later. The problem lies here. The values of the view at this point are captured by the OnLayoutChangeListener and stored in the animator. When the animation runs, it will animate to these values, not to where your behavior defines. Unfortunately, the LayoutTransition class does not give us access to the animators to allow updating of the end values.
The Fix:
Currently, I don't see an elegant fix that involves LayoutTransitions. I am going to submit a bug for a way to access and update LayoutTransition animators. For now you can disable any layoutTransition on the parent container using layoutTransition.setAnimateParentHierarchy(false)
. Then you can animate the change yourself. I'll update my answer with a working example as soon as I can.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With