While playing with MotionLayouts in a RecyclerView I noticed that the MotionLayouts would not animate the wrapping around their children if these happened to change in height.
A simple way to reproduce that behaviour would be with the following layout :
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.constraint.motion.MotionLayout
android:id="@+id/motion_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
app:layoutDescription="@xml/scene">
<View
android:id="@+id/child"
android:layout_width="0dp"
android:layout_height="200dp"
android:background="@color/colorAccent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.motion.MotionLayout>
</FrameLayout>
And the associated MotionScene with the OnClick trigger :
<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:motion="http://schemas.android.com/apk/res-auto">
<Transition
motion:constraintSetEnd="@id/end"
motion:constraintSetStart="@id/start"
motion:duration="1000">
<OnClick
motion:target="@id/child"
motion:mode="toggle"/>
</Transition>
<ConstraintSet android:id="@+id/start">
<Constraint
android:id="@id/child"
android:layout_height="200dp"/>
</ConstraintSet>
<ConstraintSet android:id="@+id/end">
<Constraint
android:id="@id/child"
android:layout_height="400dp"/>
</ConstraintSet>
</MotionScene>
Which would give the following result :
As you can see, the MotionLayout's height is set to the final expected height as soon as the transition begins, making its blue background visible until the child view finishes its own height transition and fully overlaps it.
The same thing happens in a RecyclerView if you try to implement an expending item animation.
Would there be a way to make the MotionLayout fit exactly the height of it's children during transitions or would that just be too much cost efficient?
MotionLayout bridges the gap between layout transitions and complex motion handling, offering a mix of features between the property animation framework, TransitionManager , and CoordinatorLayout . In addition to describing transitions between layouts, MotionLayout lets you animate any layout properties, as well.
↳ androidx.constraintlayout.motion.widget.MotionLayout. A subclass of ConstraintLayout that supports animating between various states Added in 2.0. A MotionLayout is a subclass of ConstraintLayout which supports transitions between between various states ( ConstraintSet ) defined in MotionScene s.
Avoid resizing the boundaries of the MotionLayout. Instead, make it bigger and only animate the size of your child objects. You can make the empty area transparent and pass touch events to the underlying views.
Source and example: https://medium.com/vrt-digital-studio/picture-in-picture-video-overlay-with-motionlayout-a9404663b9e7
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