android:MotionLayout子级的可见性更改

sco*_*yab 6 android 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>
Run Code Online (Sandbox Code Playgroud)

我有一个MotionScreen,它基于回收者视图设置了一个onswipe过渡,以减小HeaderBackground的高度并隐藏一些图像/文本视图(例如,折叠工具栏)。但是,如果出现一些致命错误,我想在RecyclerView的顶部/前面显示CustomViewGroup1,但是将RecyclerView的可见性切换为“消失”,而将CustomViewGroup1切换为可见,则不会显示CustomViewGroup1。

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

我正在使用 androidx.constraintlayout:constraintlayout:2.0.0-beta2

cut*_*iko 33

我的方法是从运动布局中排除视图,忽略场景中的可见性,这样它就可以编程并继承最终约束集中的约束。

文件提到的app:applyMotionScene="boolean",但它并没有在那里告诉你。它必须在一个PropertySet

visibilityMode只有内部的工作对我来说PropertySet

<?xml version="1.0" encoding="utf-8"?>
<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:motion="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <Transition
        ...
    </Transition>

    <ConstraintSet android:id="@+id/start">

        <Constraint android:id="@id/viewId">
            <PropertySet
                app:applyMotionScene="false"
                app:visibilityMode="ignore" />
        </Constraint>

    </ConstraintSet>

    <ConstraintSet
        android:id="@+id/end"
        motion:deriveConstraintsFrom="@id/start">
        ...
    </ConstraintSet>

</MotionScene>
Run Code Online (Sandbox Code Playgroud)

小心从运动场景自动导入应用程序名称空间,这是错误的,您必须使用与布局相同的名称。

这样做的好处是:

  • 场景中没有宽度、高度或限制
  • 无需重复属性,因为是从开始约束集派生的


sco*_*yab 7

原来这是我对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" />
Run Code Online (Sandbox Code Playgroud)

而这个被定义在同一个 collasped和支出ConstraintSets,因为我不希望在回收视图滚动它移动/动画。

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

  • 谢谢。经过几天的搜索修复后,这是我的救星。 (4认同)
  • 运动:visibilityMode =“忽略”对我有用 (2认同)

Alb*_*ert 5

以编程方式

View clickableArea = motionLayout.findViewById(R.id.video_overlay_thumbnail);    
motionLayout.getConstraintSet(R.id.start).getConstraint(clickableArea.getId()).propertySet.mVisibilityMode = 1; // 1 - ignore or 0 - normal
clickableArea.setVisibility(GONE);
Run Code Online (Sandbox Code Playgroud)