【问题标题】:MotionLayout as parent of CoordinatorLayout broke scrollingMotionLayout 作为 CoordinatorLayout 的父级打破了滚动
【发布时间】:2021-02-01 13:31:05
【问题描述】:

当在 MotionLayout 内部使用 CoordinatorLayout 并且屏幕滚动某些视图请求布局时(通过 requestLayout() 函数),其余的滚动将被破坏。

布局结构为:

<MotionLayout>
    <View/> <!-- Animated with motion scene -->
    <CoordinatorLayout>
        <AppBarLayout>
            <ImageView/> <!-- Requests layout -->
        </AppBarLayout>
        <RecyclerView/>
    </CoordinatorLayout>
<MotionLayout>

动态场景:

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

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

    <ConstraintSet android:id="@+id/start">
        <Constraint android:id="@id/background">
            <PropertySet android:alpha="0" />
        </Constraint>
    </ConstraintSet>

    <ConstraintSet android:id="@+id/end">
        <Constraint android:id="@id/background">
            <PropertySet android:alpha="1" />
        </Constraint>
    </ConstraintSet>

</MotionScene>

动画进度是这样计算的:

appBarLayout.addOnOffsetChangedListener(OnOffsetChangedListener { _, verticalOffset ->
    motionLayout.progress = -verticalOffset / appBarLayout.totalScrollRange.toFloat()
})

接下来是重点:

  1. AppBarLayout(作为协调器布局的一部分)通知(通过侦听器)有关滚动位置的更改。
  2. MotionLayout 根据上面提到的滚动位置改变它的进度。
  3. 在滚动某些视图时应该调用requestLayout() - 在我的示例中它发生在 ImageView 点击 之后,任何后续滚动都将中断,布局看起来无效。

为了确定根本原因,我尝试删除带有MotionLayout.setProgress() 的行,其余代码保持不变。之后滚动按预期工作。在此之后我决定放回MotionLayout.setProgress(),这次我删除了View.requestLayout()。这次滚动也按预期工作。但是当我试图将MotionLayout.setProgress()View.requestLayout() 都放回去时,滚动中断了。

我还向 Google 问题跟踪器提交了一个错误:https://issuetracker.google.com/u/1/issues/178976381

【问题讨论】:

    标签: android-coordinatorlayout android-motionlayout


    【解决方案1】:

    默认情况下,MotionLayout 在转换期间会忽略 requestLayout。这样做是为了性能。要在您的标签中启用,请添加

    layoutDuringTransition="honorRequest"

    【讨论】:

    • 是的,我已将 app:layoutDuringTransition="honorRequest" 添加到动态场景中的 中,现在滚动按预期工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-08
    • 1970-01-01
    • 2019-09-29
    • 2018-05-11
    • 1970-01-01
    相关资源
    最近更新 更多