android - 当手动将进度设置为 MotionLayout 时,它会清除所有约束

标签 android android-layout android-constraintlayout

我有带有两个小部件的 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>

然后在 Activity 中我设置进度:

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

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

On the other hand, let’s say that you have a layout with a dozen widgets, but only one that you are animating — the ConstraintSet in MotionScene only need to contain the Constraint for that one widget.

库版本:

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's answer我找到了解决方案:

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

最佳答案

这很奇怪,布局检查器显示 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>

关于android - 当手动将进度设置为 MotionLayout 时,它会清除所有约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51929153/

相关文章:

android - 以编程方式更改 MotionLayout 运动场景中的 ConstraintSet 属性

java - 测试中找不到Textview

android - 如何拒绝 onSharedPreferenceChanged() 监听器中的更改

android - 可以创建没有文本的 ToggleButton 吗?

Android:滚动到 View 底部

android - 如何获取适用于 Android 的最新版本 ConstraintLayout?

android - 用包含 viewpager 的 fragment 替换 fragment 时崩溃

android - 如何在android中自动增加动态创建的TextView的高度?

java - 布局小不被识别/不工作

android - 有没有更好的方法来进行这种布局?也许使用约束布局?