admin管理员组文章数量:1122846
I'm facing an issue with MotionLayout animations when my views are initially GONE and become VISIBLE after data loading.
Current setup:
All views are initially GONE in the XML layout
After API response, views become VISIBLE
Using MotionLayout for slide animation when a button is clicked
My layout:
<androidx.constraintlayout.motion.widget.MotionLayout android:id="@+id/mlSpecContainer" android:layout_width="match_parent" android:layout_height="match_parent" app:layoutDescription="@xml/view_motion"> <!-- Header (Always visible) --> <ConstraintLayout android:id="@+id/headerLayout" android:layout_width="0dp" android:layout_height="wrap_content" android:visibility="visible" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"> <!-- Header content --> </ConstraintLayout> <!-- Content (Initially GONE) --> <ConstraintLayout android:id="@+id/contentLayout" android:layout_width="0dp" android:layout_height="wrap_content" android:visibility="gone" app:layout_constraintTop_toBottomOf="@id/headerLayout" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"> <!-- Content that should be animated --> </ConstraintLayout> </androidx.constraintlayout.motion.widget.MotionLayout> > <MotionScene> > <Transition > motion:constraintSetStart="@+id/expanded" > motion:constraintSetEnd="@+id/collapsed" > motion:duration="300"> > > <OnClick > motion:targetId="@id/button" > motion:clickAction="toggle"/> > </Transition> > > <ConstraintSet android:id="@+id/expanded"> > <Constraint > android:id="@+id/contentLayout" > android:layout_width="0dp" > android:layout_height="wrap_content" > motion:layout_constraintTop_toBottomOf="@id/headerLayout" > motion:layout_constraintStart_toStartOf="parent" > motion:layout_constraintEnd_toEndOf="parent" > android:translationY="0dp"/> > </ConstraintSet> > > <ConstraintSet android:id="@+id/collapsed"> > <Constraint > android:id="@+id/contentLayout" > android:layout_width="0dp" > android:layout_height="wrap_content" > motion:layout_constraintTop_toBottomOf="@id/headerLayout" > motion:layout_constraintStart_toStartOf="parent" > motion:layout_constraintEnd_toEndOf="parent" > android:translationY="-1000dp"/> > </ConstraintSet> </MotionScene>
I'm facing an issue with MotionLayout animations when my views are initially GONE and become VISIBLE after data loading.
Current setup:
All views are initially GONE in the XML layout
After API response, views become VISIBLE
Using MotionLayout for slide animation when a button is clicked
My layout:
<androidx.constraintlayout.motion.widget.MotionLayout android:id="@+id/mlSpecContainer" android:layout_width="match_parent" android:layout_height="match_parent" app:layoutDescription="@xml/view_motion"> <!-- Header (Always visible) --> <ConstraintLayout android:id="@+id/headerLayout" android:layout_width="0dp" android:layout_height="wrap_content" android:visibility="visible" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"> <!-- Header content --> </ConstraintLayout> <!-- Content (Initially GONE) --> <ConstraintLayout android:id="@+id/contentLayout" android:layout_width="0dp" android:layout_height="wrap_content" android:visibility="gone" app:layout_constraintTop_toBottomOf="@id/headerLayout" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"> <!-- Content that should be animated --> </ConstraintLayout> </androidx.constraintlayout.motion.widget.MotionLayout> > <MotionScene> > <Transition > motion:constraintSetStart="@+id/expanded" > motion:constraintSetEnd="@+id/collapsed" > motion:duration="300"> > > <OnClick > motion:targetId="@id/button" > motion:clickAction="toggle"/> > </Transition> > > <ConstraintSet android:id="@+id/expanded"> > <Constraint > android:id="@+id/contentLayout" > android:layout_width="0dp" > android:layout_height="wrap_content" > motion:layout_constraintTop_toBottomOf="@id/headerLayout" > motion:layout_constraintStart_toStartOf="parent" > motion:layout_constraintEnd_toEndOf="parent" > android:translationY="0dp"/> > </ConstraintSet> > > <ConstraintSet android:id="@+id/collapsed"> > <Constraint > android:id="@+id/contentLayout" > android:layout_width="0dp" > android:layout_height="wrap_content" > motion:layout_constraintTop_toBottomOf="@id/headerLayout" > motion:layout_constraintStart_toStartOf="parent" > motion:layout_constraintEnd_toEndOf="parent" > android:translationY="-1000dp"/> > </ConstraintSet> </MotionScene>
2 Answers
Reset to default 0<ConstraintLayout
android:id="@+id/contentLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:visibility="invisible"
app:layout_constraintTop_toBottomOf="@id/headerLayout"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
<!-- Content that should be animated -->
I assume you are having problem in contraints as the view would be set to gone. so you could use a view as a reference or use a Barrier as follows.
<androidx.constraintlayout.motion.widget.MotionLayout
android:id="@+id/mlSpecContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutDescription="@xml/view_motion">
<!-- Header (Always visible) -->
<ConstraintLayout
android:id="@+id/headerLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:visibility="visible"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
<!-- Header content -->
</ConstraintLayout>
<!-- Content (Initially GONE) -->
<ConstraintLayout
android:id="@+id/contentLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:visibility="gone"
app:layout_constraintTop_toBottomOf="@id/headerLayout"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
<androidx.constraintlayout.widget.Barrier
android:id="@+id/barrier"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="bottom"
app:constraint_referenced_ids="contentLayout,headerLayout" />
<!-- Content that should be animated -->
<!--for example below is a textview to manage, you can use
your layout instead of this.-->
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/barrier"
app:layout_constraintBottom_toBottomOf="parent"
android:visibility="gone"
tools:text="05:00"
/>
</ConstraintLayout>
</androidx.constraintlayout.motion.widget.MotionLayout>
Here I have passed 2 reference id to the barrier, the barrier would be at the bottom of the first reference id, if the first view is hidden the it will automatically align at the bottom of the second reference id.
本文标签: Android MotionLayout How to handle views with initial GONE visibility in animationStack Overflow
版权声明:本文标题:Android MotionLayout: How to handle views with initial GONE visibility in animation - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736304948a1932416.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论