admin管理员组

文章数量:1122846

I'm facing an issue with MotionLayout animations when my views are initially GONE and become VISIBLE after data loading.

Current setup:

  1. All views are initially GONE in the XML layout

  2. After API response, views become VISIBLE

  3. 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:

  1. All views are initially GONE in the XML layout

  2. After API response, views become VISIBLE

  3. 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>
    
    
Share Improve this question asked Nov 22, 2024 at 9:06 osj0524osj0524 12 bronze badges
Add a comment  | 

2 Answers 2

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