首页 > 解决方案 > 如何更改 animateLayoutChanges 顺序?

问题描述

我在布局中使用了 animateLayoutChanges,我有 3 个孩子,第二个可见性消失了,所以第三个在下面的第一个。当我单击第一个孩子时,我将第二个可见性更改为可见,反之亦然。

当我看到动画顺序看起来很糟糕时,我希望动画在显示和下拉或隐藏和上拉时并行运行。

有gif。 动画 Gif

我想要的只是隐藏和下拉或显示和上拉并行运行的动画。

有我的xml

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/space_medium"
android:animateLayoutChanges="true"
android:orientation="vertical">

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/top"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/bg_primary_rounded_top"
    android:paddingLeft="@dimen/space_medium"
    android:paddingTop="@dimen/space_small"
    android:paddingRight="@dimen/space_medium"
    android:paddingBottom="@dimen/space_small">

    <TextView
        android:id="@+id/username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/username"
        android:textColor="@color/white" />

    <com.mikepenz.iconics.view.IconicsImageView
        android:id="@+id/detail"
        android:layout_width="16dp"
        android:layout_height="16dp"
        app:iiv_color="@color/divider"
        app:iiv_icon="@string/gmd_keyboard_arrow_down"
        app:layout_constraintBottom_toBottomOf="@+id/username"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@id/username" />

</androidx.constraintlayout.widget.ConstraintLayout>

<androidx.constraintlayout.widget.ConstraintLayout
    android:visibility="gone"
    android:id="@+id/center"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/secondaryDarkColor"
    app:layout_constraintTop_toBottomOf="@id/top">

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/white"/>

      <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="@dimen/space_medium"
        android:paddingTop="@dimen/space_small"
        android:paddingRight="@dimen/space_medium"
        android:paddingBottom="@dimen/space_small"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/city"
                android:textColor="@color/white"
                android:gravity="left"
                android:textSize="5pt" />

            <TextView
                android:id="@+id/city"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Jakarta"
                android:textAlignment="textEnd"
                android:textColor="@color/white"
                android:textSize="5pt"
                android:gravity="end" />

        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/divider"
            android:layout_marginTop="@dimen/space_small"
            android:layout_marginBottom="@dimen/space_small"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/status"
                android:textColor="@color/white"
                android:textSize="5pt" />

            <TextView
                android:id="@+id/status"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Started"
                android:textAlignment="textEnd"
                android:textColor="@color/white"
                android:textSize="5pt"
                android:gravity="end" />

        </LinearLayout>

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/bottom"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/bg_primary_rounded_bottom"
    android:paddingLeft="@dimen/space_medium"
    android:paddingTop="@dimen/space_small"
    android:paddingRight="@dimen/space_medium"
    android:paddingBottom="@dimen/space_small"
    app:layout_constraintTop_toBottomOf="@+id/center">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="3"
        app:layout_constraintLeft_toLeftOf="parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1">

            <com.mikepenz.iconics.view.IconicsImageView
                android:id="@+id/start"
                android:layout_width="24dp"
                android:layout_height="24dp"
                app:iiv_color="@color/white"
                app:iiv_icon="@string/faw_play"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/space_small"
                android:enabled="true"
                android:text="@string/start"
                android:textAlignment="center"
                android:textColor="@color/text_button"
                android:textSize="5pt"
                app:layout_constraintTop_toBottomOf="@+id/start" />

        </androidx.constraintlayout.widget.ConstraintLayout>

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1">

            <com.mikepenz.iconics.view.IconicsImageView
                android:id="@+id/pause"
                android:layout_width="24dp"
                android:layout_height="24dp"
                app:iiv_color="@color/grey"
                app:iiv_icon="@string/faw_pause"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/space_small"
                android:enabled="false"
                android:text="@string/pause"
                android:textAlignment="center"
                android:textColor="@color/text_button"
                android:textSize="5pt"
                app:layout_constraintTop_toBottomOf="@+id/pause" />

        </androidx.constraintlayout.widget.ConstraintLayout>

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1">

            <com.mikepenz.iconics.view.IconicsImageView
                android:id="@+id/stop"
                android:layout_width="24dp"
                android:layout_height="24dp"
                app:iiv_color="@color/grey"
                app:iiv_icon="@string/faw_stop"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/space_small"
                android:enabled="false"
                android:text="@string/stop"
                android:textAlignment="center"
                android:textColor="@color/text_button"
                android:textSize="5pt"
                app:layout_constraintTop_toBottomOf="@+id/stop" />

        </androidx.constraintlayout.widget.ConstraintLayout>

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

还有显示隐藏代码

if (detailExpanded) {
    detail.setIcon(
            new IconicsDrawable(getContext())
                    .icon(GoogleMaterial.Icon.gmd_keyboard_arrow_down)
                    .color(getResources().getColor(R.color.divider))
                    .sizeDp(16));
    detailExpanded = false;
    detailGroup.setVisibility(View.GONE);
} else {
    detail.setIcon(
            new IconicsDrawable(getContext())
                    .icon(GoogleMaterial.Icon.gmd_keyboard_arrow_up)
                    .color(getResources().getColor(R.color.divider))
                    .sizeDp(16));
    detailExpanded = true;
    detailGroup.setVisibility(View.VISIBLE);
}

标签: androidandroid-animation

解决方案


推荐阅读