【发布时间】: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()
})
接下来是重点:
- AppBarLayout(作为协调器布局的一部分)通知(通过侦听器)有关滚动位置的更改。
- MotionLayout 根据上面提到的滚动位置改变它的进度。
- 在滚动某些视图时应该调用
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