在 BottomSheetFragment 上重复 ConstraintSet 的过渡动画问题

Posted

技术标签:

【中文标题】在 BottomSheetFragment 上重复 ConstraintSet 的过渡动画问题【英文标题】:Repeating ConstraintSet's transition animation issue on BottomSheetFragment 【发布时间】:2019-04-27 19:51:56 【问题描述】:

我想在单击时为 BottomSheetFragment 上的布局更改设置动画。我实现了 ConstraintSet.applyTo 和 TransitionManager.beginDelayedTransition 将初始场景更改为最终场景。但是当过渡发生时,动画似乎在重复。底部的示例输出。

BottomSheetFragment.java

ConstraintSet constraintSet;
boolean isSelected = false;

@BindView(R.id.container_root) ConstraintLayout layoutRoot;
@BindView(R.id.ic_leave_date)  ImageView ivLeaveDate;
@BindView(R.id.ic_date_filed)  ImageView ivDateFiled;

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) 
    super.onViewCreated(view, savedInstanceState);
    LayoutTransition transition = new LayoutTransition();
    transition.setAnimateParentHierarchy(false);
    layoutRoot.setLayoutTransition(transition);


@OnClick(R.id.layout1)
public void openLeaveDate()
    TransitionDrawable bgTransition = (TransitionDrawable) layoutLeaveDate.getBackground();
    TransitionDrawable ivTransition = (TransitionDrawable) ivLeaveDate.getBackground();

    if(isSelected)
        isSelected = false;
        bgTransition.reverseTransition(300);
        ivTransition.resetTransition();
        constraintSet = new ConstraintSet();
        constraintSet.clone(getActivity(), R.layout.bottom_sheet_filter_leave);

        TransitionManager.beginDelayedTransition(layoutRoot);
        constraintSet.applyTo(layoutRoot);
    else
        isSelected = true;
        bgTransition.startTransition(300);
        ivTransition.startTransition(300);
        constraintSet = new ConstraintSet();

        constraintSet.clone(getActivity(), R.layout.layout_bottom_date_filed);
        TransitionManager.beginDelayedTransition(layoutRoot);
        constraintSet.applyTo(layoutRoot);
    

bottom_sheet_filter_leave.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container_root"
    android:layout_
    android:layout_
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior">


    <android.support.constraint.ConstraintLayout
       android:id="@+id/layout_leave_date"
       android:layout_
       android:layout_
       android:background="@transition/bg_transition"
       android:clickable="true"
       android:focusable="true"
       android:paddingStart="24dp">

       <ImageView
           android:id="@+id/ic_leave_date"
           android:layout_
           android:layout_
           android:src="@transition/ic_leave_date_transition"/>

       <TextView
           android:id="@+id/label_leave_date"
           style="@style/TextAppearance.Content.Bold.Black"
           android:layout_
           android:layout_
           android:layout_marginStart="24dp"
           android:text="@string/leave_date"
           app:layout_constraintBottom_toTopOf="@+id/guide_leave_date"
           app:layout_constraintStart_toEndOf="@+id/ic_leave_date"/>

       <TextView
           android:layout_
           android:layout_
           android:layout_marginStart="24dp"
           android:text="All"
           app:layout_constraintStart_toEndOf="@+id/ic_leave_date"
           app:layout_constraintTop_toBottomOf="@+id/guide_leave_date"/>

       <android.support.constraint.Guideline
           android:id="@+id/guide_leave_date"
           android:layout_
           android:layout_
           android:orientation="horizontal"
           app:layout_constraintGuide_percent="0.5"/>
    </android.support.constraint.ConstraintLayout>


    <android.support.constraint.ConstraintLayout
       android:id="@+id/layout1"
       android:layout_
       android:layout_
       android:background="@transition/bg_transition"
       android:paddingEnd="24dp"
       android:paddingStart="24dp"
       android:visibility="visible"
       app:layout_constraintTop_toBottomOf="@+id/layout_leave_date">

       <ImageView
           android:id="@+id/ic_date_filed"
           android:layout_
           android:layout_
           android:background="@transition/ic_date_filed_transition"
           app:layout_constraintTop_toTopOf="parent"
           app:layout_constraintBottom_toBottomOf="parent"/>

       <TextView
           android:id="@+id/label_date_filed"
           style="@style/TextAppearance.Content.Bold.Black"
           android:layout_
           android:layout_
           android:layout_marginStart="24dp"
           android:text="@string/date_filled"
           app:layout_constraintBottom_toTopOf="@+id/guide_date_filed"
           app:layout_constraintStart_toEndOf="@+id/ic_date_filed"/>

       <TextView
           android:layout_
           android:layout_
           android:layout_marginStart="24dp"
           android:text="All"
           app:layout_constraintStart_toEndOf="@+id/ic_date_filed"
           app:layout_constraintTop_toBottomOf="@+id/guide_date_filed"/>

       <android.support.constraint.Guideline
           android:id="@+id/guide_date_filed"
           android:layout_
           android:layout_
           android:orientation="horizontal"
           app:layout_constraintGuide_percent="0.5"/>
    </android.support.constraint.ConstraintLayout>



    <android.support.constraint.ConstraintLayout
       android:id="@+id/layout_all_date_filed"
       android:layout_
       android:layout_
       android:clickable="true"
       android:focusable="true"
       android:paddingEnd="0dp"
       android:paddingStart="12dp"
       android:visibility="gone"
       app:layout_constraintTop_toBottomOf="@+id/layout1">

       <RadioButton
           android:id="@+id/rb_all_date_filed"
           android:layout_
           android:layout_
           app:layout_constraintBottom_toBottomOf="parent"
           app:layout_constraintTop_toTopOf="parent"/>

       <TextView
           android:layout_
           android:layout_
           android:layout_marginStart="12dp"
           android:text="@string/all"
           app:layout_constraintBottom_toBottomOf="parent"
           app:layout_constraintStart_toEndOf="@id/rb_all_date_filed"
           app:layout_constraintTop_toTopOf="parent"/>

    </android.support.constraint.ConstraintLayout>

    <android.support.constraint.ConstraintLayout
       android:id="@+id/layout_input_date_filed"
       android:layout_
       android:layout_
       android:clickable="true"
       android:focusable="true"
       android:paddingEnd="0dp"
       android:paddingStart="12dp"
       android:visibility="gone"
       app:layout_constraintTop_toBottomOf="@+id/layout_all_date_filed">

       <RadioButton
           android:id="@+id/rb_input_date_filed"
           android:layout_
           android:layout_
           app:layout_constraintBottom_toBottomOf="parent"
           app:layout_constraintTop_toTopOf="parent"/>

       <EditText
           android:id="@+id/txt_date_filed_start"
             style="@style/ThemeOverlay.MaterialComponents.TextInputEditText.OutlinedBox.Dense"
           android:layout_
           android:layout_
           android:layout_marginStart="12dp"
           android:drawableEnd="@drawable/ic_calendar_orange"
           android:hint="@string/mm_dd_yy"
           android:singleLine="true"
           android:textSize="13sp"
           app:layout_constraintBottom_toBottomOf="parent"
           app:layout_constraintStart_toEndOf="@+id/rb_input_date_filed"
           app:layout_constraintTop_toTopOf="parent" />

       <View
           android:id="@+id/view_divider_date_filed"
           android:layout_
           android:layout_
           android:layout_marginStart="15dp"
           android:background="@color/gray"
           app:layout_constraintBottom_toBottomOf="parent"
           app:layout_constraintStart_toEndOf="@+id/txt_date_filed_start"
           app:layout_constraintTop_toTopOf="parent"/>

       <EditText
           android:id="@+id/txt_date_filed_end"
        style="@style/ThemeOverlay.MaterialComponents.TextInputEditText.OutlinedBox.Dense"
           android:layout_
           android:layout_
           android:layout_marginStart="15dp"
           android:hint="@string/mm_dd_yy"
           android:singleLine="true"
           android:drawableEnd="@drawable/ic_calendar_orange"
           android:textSize="13sp"
           app:layout_constraintBottom_toBottomOf="parent"
           app:layout_constraintStart_toEndOf="@+id/view_divider_date_filed"
           app:layout_constraintTop_toTopOf="parent" />

      </android.support.constraint.ConstraintLayout>

layout_bottom_date_filed.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_
    android:layout_
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior">


    <android.support.constraint.ConstraintLayout
       android:id="@+id/layout_leave_date"
       android:layout_
       android:layout_
       android:background="@transition/bg_transition"
       android:clickable="true"
       android:focusable="true"
       android:visibility="gone"
       android:paddingStart="24dp">

       <ImageView
           android:id="@+id/ic_leave_date"
           android:layout_
           android:layout_
           android:src="@transition/ic_leave_date_transition"/>

       <TextView
           android:id="@+id/label_leave_date"
           style="@style/TextAppearance.Content.Bold.Black"
           android:layout_
           android:layout_
           android:layout_marginStart="24dp"
           android:text="@string/leave_date"
           app:layout_constraintBottom_toTopOf="@+id/guide_leave_date"
           app:layout_constraintStart_toEndOf="@+id/ic_leave_date"/>

       <TextView
           android:layout_
           android:layout_
           android:layout_marginStart="24dp"
           android:text="All"
           app:layout_constraintStart_toEndOf="@+id/ic_leave_date"
           app:layout_constraintTop_toBottomOf="@+id/guide_leave_date"/>

       <android.support.constraint.Guideline
           android:id="@+id/guide_leave_date"
           android:layout_
           android:layout_
           android:orientation="horizontal"
           app:layout_constraintGuide_percent="0.5"/>
    </android.support.constraint.ConstraintLayout>


    <android.support.constraint.ConstraintLayout
       android:id="@+id/layout1"
       android:layout_
       android:layout_
       android:background="@transition/bg_transition"
       android:paddingEnd="24dp"
       android:paddingStart="24dp"
       android:visibility="visible"
       app:layout_constraintTop_toBottomOf="@+id/layout_leave_date">

       <ImageView
           android:id="@+id/ic_date_filed"
           android:layout_
           android:layout_
           android:background="@transition/ic_date_filed_transition"
           app:layout_constraintTop_toTopOf="parent"
           app:layout_constraintBottom_toBottomOf="parent"/>

       <TextView
           android:id="@+id/label_date_filed"
           style="@style/TextAppearance.Content.Bold.Black"
           android:layout_
           android:layout_
           android:layout_marginStart="24dp"
           android:text="@string/date_filled"
           app:layout_constraintBottom_toTopOf="@+id/guide_date_filed"
           app:layout_constraintStart_toEndOf="@+id/ic_date_filed"/>

       <TextView
           android:layout_
           android:layout_
           android:layout_marginStart="24dp"
           android:text="All"
           app:layout_constraintStart_toEndOf="@+id/ic_date_filed"
           app:layout_constraintTop_toBottomOf="@+id/guide_date_filed"/>

       <android.support.constraint.Guideline
           android:id="@+id/guide_date_filed"
           android:layout_
           android:layout_
           android:orientation="horizontal"
           app:layout_constraintGuide_percent="0.5"/>
    </android.support.constraint.ConstraintLayout>



    <android.support.constraint.ConstraintLayout
       android:id="@+id/layout_all_date_filed"
       android:layout_
       android:layout_
       android:clickable="true"
       android:focusable="true"
       android:paddingEnd="0dp"
       android:paddingStart="12dp"
       app:layout_constraintTop_toBottomOf="@+id/layout1">

       <RadioButton
           android:id="@+id/rb_all_date_filed"
           android:layout_
           android:layout_
           app:layout_constraintBottom_toBottomOf="parent"
           app:layout_constraintTop_toTopOf="parent"/>

       <TextView
           android:layout_
           android:layout_
           android:layout_marginStart="12dp"
           android:text="@string/all"
           app:layout_constraintBottom_toBottomOf="parent"
           app:layout_constraintStart_toEndOf="@id/rb_all_date_filed"
           app:layout_constraintTop_toTopOf="parent"/>

    </android.support.constraint.ConstraintLayout>

    <android.support.constraint.ConstraintLayout
       android:id="@+id/layout_input_date_filed"
       android:layout_
       android:layout_
       android:clickable="true"
       android:focusable="true"
       android:paddingEnd="0dp"
       android:paddingStart="12dp"
       app:layout_constraintTop_toBottomOf="@+id/layout_all_date_filed">

       <RadioButton
           android:id="@+id/rb_input_date_filed"
           android:layout_
           android:layout_
           app:layout_constraintBottom_toBottomOf="parent"
           app:layout_constraintTop_toTopOf="parent"/>

       <EditText
           android:id="@+id/txt_date_filed_start"
             style="@style/ThemeOverlay.MaterialComponents.TextInputEditText.OutlinedBox.Dense"
           android:layout_
           android:layout_
           android:layout_marginStart="12dp"
           android:drawableEnd="@drawable/ic_calendar_orange"
           android:hint="@string/mm_dd_yy"
           android:singleLine="true"
           android:textSize="13sp"
           app:layout_constraintBottom_toBottomOf="parent"
           app:layout_constraintStart_toEndOf="@+id/rb_input_date_filed"
           app:layout_constraintTop_toTopOf="parent" />

       <View
           android:id="@+id/view_divider_date_filed"
           android:layout_
           android:layout_
           android:layout_marginStart="15dp"
           android:background="@color/gray"
           app:layout_constraintBottom_toBottomOf="parent"
           app:layout_constraintStart_toEndOf="@+id/txt_date_filed_start"
           app:layout_constraintTop_toTopOf="parent"/>

       <EditText
           android:id="@+id/txt_date_filed_end"
        style="@style/ThemeOverlay.MaterialComponents.TextInputEditText.OutlinedBox.Dense"
           android:layout_
           android:layout_
           android:layout_marginStart="15dp"
           android:hint="@string/mm_dd_yy"
           android:singleLine="true"
           android:drawableEnd="@drawable/ic_calendar_orange"
           android:textSize="13sp"
           app:layout_constraintBottom_toBottomOf="parent"
           app:layout_constraintStart_toEndOf="@+id/view_divider_date_filed"
           app:layout_constraintTop_toTopOf="parent" />

      </android.support.constraint.ConstraintLayout>

如下所示:

更新:

我尝试将此设置应用于其他片段(不是 BottomSheetFragment),动画似乎正常工作。也许它与BottomSheetFragment有关?

【问题讨论】:

【参考方案1】:

我通过删除 OnViewCreated() 上的这些行来解决它:

LayoutTransition transition = new LayoutTransition();
transition.setAnimateParentHierarchy(false);
layoutRoot.setLayoutTransition(transition);

【讨论】:

以上是关于在 BottomSheetFragment 上重复 ConstraintSet 的过渡动画问题的主要内容,如果未能解决你的问题,请参考以下文章

Android BottomSheetFragment Compose 弹出键盘顶起布局

RecyclerView 在 BottomSheetFragment 中隐藏其下方的项目

从带有 Jetpack 导航结果的 BottomSheetFragment 导航,出现崩溃“无法从当前目的地找到操作”

如何在片段类而不是活动类中使用底页?

onDetach()不要求从一个片段移动到另一个片段

磁盘上重复的贴图在内存中也会重复存在