6

我的应用中有一个用于滑动过渡的 MotionLayout。目前我刚刚更新2.0.0-alpha32.0.0-alpha4发布,有些事情不像以前那样工作。有“myView”,这是一个应该从屏幕底部扩展到标题视图的布局。它对我需要以编程方式控制的指南有一个限制(基于一些用户操作)。

代码中没有其他更改,因此降级到 alpha3 使一切按预期工作。

代码非常基本:

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

    <Transition
        motion:constraintSetEnd="@id/end"
        motion:constraintSetStart="@id/start"
        motion:duration="1000"
        motion:interpolator="linear">
        <!-- I used motion:motionInterpolator="linear" for alpha4-->

        <OnSwipe
            motion:dragDirection="dragUp"
            motion:touchAnchorId="@id/myView"
            motion:touchAnchorSide="top" />

    </Transition>

    <ConstraintSet android:id="@+id/start">
        <Constraint
            android:id="@id/myView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toTopOf="@id/myGuideline" />
    </ConstraintSet>

    <ConstraintSet android:id="@+id/end">
        <Constraint
            android:id="@id/myView"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toBottomOf="@id/someHeaderView" />
    </ConstraintSet>
</MotionScene>

问题是在 alpha3 版本中,我可以使用这样的东西(并且它有效):

myMotionLayout.getConstraintSet(R.id.start)?.setGuidelinePercent(R.id.myGuideline, guidelinePercentage)

现在我升级到2.0.0-alpha4,这不再起作用,我不明白为什么。它总是从 xml 中获取默认值,并且在我尝试动态执行时永远不会更改。

我要做的是根据一些动态数据(在片段开头或用户返回时可用)更改过渡的起点。如果您对如何使用 MotionLayout 实现这一点有更好的了解,请给我一个提示。

摇篮实现:

implementation "com.android.support.constraint:constraint-layout:2.0.0-alpha4"
4

1 回答 1

1

我注意到在 alpha4 版本中引入了类似的回归。通过反复试验和以下错误报告,我能够以编程方式在 MotionLayout(beta1 版本)中应用我的 ConstraintSet 更改。看来克隆方法不适用于 MotionLayout,而应该使用 getConstraintSet(正如您所做的那样)。看起来您缺少的步骤是constraintSet.applyTo(motion_layout). 最后我还需要重新应用 MotionLayout 描述 xml。

实现“androidx.constraintlayout:constraintlayout:2.0.0-beta1”

脚步:

val constraintSet = motion_layout.getConstraintSet(R.id.motion_layout)
constraintSet.clear(R.id.txt_view, ConstraintSet.BOTTOM)
constraintSet.applyTo(motion_layout)
// If using a MotionLayout description xml file, re-apply it
motion_explore_article.loadLayoutDescription(R.xml.slide_over_image)

错误报告: https ://issuetracker.google.com/issues/131874827

于 2019-05-28T04:54:24.480 回答