首页 > 解决方案 > android:对 MotionLayout 子项的可见性更改

问题描述

我一定错过了 android:visibility 在动作布局中的变化。这是我的布局的简化版本。

<MotionLayout>
 <ImageView android:id="@+id/HeaderBackground" />
Bunch of image views, Text views that are constrainted to the HeaderBackground. 
<RecyclerView android:visibility="visible">
<EditText android:visibility="visible">
<CustomViewGroup1 android:visibility="gone">
</MotionLayout>

我有一个motionScreen,它基于回收器视图设置一个Transition onswipe,以降低HeaderBackground 的高度并隐藏一些图像/文本视图(即折叠工具栏)。但是,如果有一些致命错误,我想在 RecyclerView 的顶部/前面显示 CustomViewGroup1,但是将 RecyclerView 的可见性切换为消失并且将 CustomViewGroup1 切换为可见不会使 CustomViewGroup1 显示。

我可以将 CustomViewGroup 组移出 MotionLayout 并添加一个框架布局作为活动的根并隐藏整个 MotionLayout。但这不太理想,因为我必须复制工具栏和图标。所以我想问题是关于可见性变化和潜在的 z 排序?

我在用着androidx.constraintlayout:constraintlayout:2.0.0-beta2

标签: androidandroid-motionlayout

解决方案


原来这是我对 MotionLayout 如何工作的缺乏经验。默认情况下,MotionLayout 控制其中所有视图的可见性。但是您可以通过使用app:visibilityMode="ignore"属性<ConstraintSet>

在我的情况下<CustomViewGroup1>看起来像这样......

<Constraint
      android:id="@id/CustomViewGroup1"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toBottomOf="@+id/HeaderBackground"
      app:layout_constraintVertical_weight="1"
      app:visibilityMode="ignore" />

这在折叠和展开的 ConstraintSets 中定义相同,因为我不希望它在滚动回收器视图时移动/动画。

感谢 John Hoford 在另一个频道中提供的建议。


推荐阅读