首页 > 解决方案 > 当手动将进度设置为 MotionLayout 时,它会清除所有约束

问题描述

我有带有两个小部件的 MotionLayout,一个在 MotionLayout 中描述,第二个在场景文件中。

布局文件:

<android.support.constraint.motion.MotionLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/motion_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layoutDescription="@xml/scene_test">

    <View
        android:id="@+id/test_view_static"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:background="#ff0000"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:id="@+id/test_view_dynamic"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:background="#00ff11" />

</android.support.constraint.motion.MotionLayout>

场景文件:

<MotionScene xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <Transition
        app:constraintSetStart="@+id/start"
        app:constraintSetEnd="@+id/end"/>

    <ConstraintSet android:id="@id/start">

        <Constraint
            android:id="@+id/test_view_dynamic"
            android:layout_width="40dp"
            android:layout_height="40dp"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </ConstraintSet>

    <ConstraintSet android:id="@id/end">

        <Constraint
            android:id="@id/test_view_dynamic"
            android:layout_width="40dp"
            android:layout_height="40dp"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintBottom_toBottomOf="parent" />

    </ConstraintSet>

</MotionScene>

然后在活动中我设置进度:

//onCreate, etc..
MotionLayout motionLayout = findViewById(R.id.motion_layout);
motionLayout.setProgress(0.5f);

动态视图在正确的位置,但静态视图失去了所有约束,但不受场景影响,如此所述,只有场景文件中描述的小部件会受到影响。

另一方面,假设您有一个包含十几个小部件的布局,但只有一个小部件正在制作动画 - MotionScene 中的 ConstraintSet 只需要包含该小部件的约束。

库版本:

implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:2.0.0-alpha2'

此处的示例项目:https ://github.com/Anrimian/MotionLayoutIssueTest

更新:感谢Kélian的回答我找到了解决方案:

public static void setProgress(MotionLayout motionLayout, float progress) {
    if (ViewCompat.isLaidOut(motionLayout)) {
        motionLayout.setProgress(progress);
    } else {
        motionLayout.post(() -> motionLayout.setProgress(progress));
    }
}

标签: androidandroid-layoutandroid-constraintlayout

解决方案


这很奇怪,布局检查器显示test_view_static大小:宽度 = 0 和高度 = 0。

我更新了一点你的代码:在 onCreate 添加一个点击监听test_view_static器来执行进度,它工作正常。

我尝试了另一件事:在 onStart() 方法中我输入了:

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    public void run() {
        MotionLayout motionLayout = findViewById(R.id.motion_layout);
        motionLayout.setProgress(0.5f);
    }
}, 1000);

并且 MotionLayout 的行为符合预期。就像您必须等待 MotionLayout 初始化才能更新它的进度。

事实上,您希望有另一个您在场景中描述的开始布局。您可以在不更新进度的情况下执行此操作:

<ConstraintSet android:id="@id/start">
    <Constraint
        android:id="@+id/test_view_dynamic"
        android:layout_width="40dp"
        android:layout_height="40dp"
        motion:layout_constraintRight_toRightOf="parent"
        motion:layout_constraintTop_toTopOf="parent"
        motion:layout_constraintBottom_toBottomOf="parent" />
</ConstraintSet>

推荐阅读