【问题标题】:Using MotionLayout and setting visibility using Databinding fails [duplicate]使用 MotionLayout 并使用数据绑定设置可见性失败 [重复]
【发布时间】:2019-09-01 17:41:21
【问题描述】:

我正在使用implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta1',并启用了数据绑定。

当我的观点

<ImageView
            android:id="@+id/im_lightning"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_marginTop="24dp"
            android:layout_marginEnd="4dp"
            android:contentDescription="@string/charging"
            android:src="@drawable/ic_lightning"
            android:visibility="@{batteryViewModel.liveData.charging ? View.VISIBLE : View.GONE}"
            app:layout_constraintBottom_toBottomOf="@id/tv_percent"
            app:layout_constraintRight_toLeftOf="@id/tv_percent"
            app:layout_constraintTop_toTopOf="@id/tv_percent"
            app:layout_constraintVertical_bias="1.0" />

包裹在协调器布局周围,其可见性变化按预期发生。一旦我将它包装在 MotionLayout 中,可见性更改就不会像以前那样起作用。准确地说,视图在应有的时候是不可见的。它在触发事件后变为可见一秒钟,然后恢复为不可见。这是一个已知的错误吗?

需要时的代码:

协调器布局:

<androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorBackground">
        <ImageView (same as above) />
</androidx.constraintlayout.widget.ConstraintLayout>

运动布局:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".ui.MainActivity">

    <androidx.constraintlayout.motion.widget.MotionLayout
        android:id="@+id/motion_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorBackground"
        app:layoutDescription="@xml/home_scene_0"
        app:showPaths="true">

        <ImageView (same as above) />
    </androidx.constraintlayout.motion.widget.MotionLayout>
    <data>
        <import type="android.view.View" />
        <variable
            name="batteryViewModel"
            type="rish.crearo.minimalphone.viewmodels.BatteryViewModel" />
    </data>
</layout>

【问题讨论】:

  • @tim-castelijns 真正困扰我的是什么——以至于我不喜欢在这里发布问题——问题被标记为重复/离题 yada yada 没有解释或提供指向重复的问题。感谢您抽出宝贵时间来标记这个问题,但如果没有反馈,下次我在 StackOverflow 上发帖时,我将无法成为更好的提问者。另外,我又找了一遍,似乎在任何地方都找不到这个问题。
  • 除了“请查看链接的问题”之外,不确定您期望得到什么样的反馈。这个问题还不错,没必要改进。运动布局控制其子项的可见性。似乎这里的问题与那里的问题相同,因此关闭。如果您告诉我链接问题中的答案对您不起作用,我会很乐意重新打开您的问题
  • @tim-castelijns 我很抱歉。在问题顶部的链接和底部标记为重复的链接之间,我错过了看到链接。我开枪了!链接的问题对我有用。

标签: android android-coordinatorlayout android-motionlayout


【解决方案1】:

我遇到了同样的问题。 看来,如果您在数据绑定和运动场景中操作相同视图的属性(在本例中为可见性),则运动场景优于绑定值,因此您需要在场景上绑定值。

TL;DR 绑定视图的属性运动场景的约束集 (而不是 layout.xml)

我使用@BindingAdapter 来实现这一点。

@BindingAdapter("goneUnlessOnMotionLayout")
public static void goneUnlessOnMotionLayout(View view, boolean visible) {
  if (view.getParent() instanceof MotionLayout) {
        final MotionLayout layout = (MotionLayout) view.getParent();
        final int setToVisibility = visible ? View.VISIBLE : View.GONE;
        final float setToAlpha = visible ? 1f : 0f;

        for (int constraintId : layout.getConstraintSetIds()) {
            final ConstraintSet constraint = layout.getConstraintSet(constraintId);
            if (constraint != null) {
                constraint.setVisibility(view.getId(), setToVisibility);
                constraint.setAlpha(view.getId(), setToAlpha);
            }
        }
    }
}

注意不要在motionLayout正在进行时在视图模型中通知PropertyChanged,即motionLayout.progress!= 0或1,这将导致约束之间的自然运动中断

【讨论】:

  • 看起来很有希望。如果可行,我会尝试并标记为正确!
  • this SO question 的答案有效。我想它更正确,因为运动布局控制了其中的所有视图,因此您必须在约束中指定它。
  • 感谢@Idan 的解决方案。我在您的解决方案中添加了constraint.applyTo(layout),否则在我刷卡之前它没有得到应用的值。我也跳过了 setAlpha 部分。
  • 谢谢,该解决方案运行良好,首先它对我来说不能正常工作,但我发现我正在为开始/结束约束设置app:visibilityMode="ignore",这会阻止正确设置可见性.再次感谢。
  • 嗨@Idan,你如何在你的运动场景中使用goneUnlessOnMotionLayout?
猜你喜欢
  • 2017-11-09
  • 2015-01-10
  • 2014-03-12
  • 1970-01-01
  • 1970-01-01
  • 2011-04-03
  • 2022-09-27
  • 2015-08-27
  • 2016-10-22
相关资源
最近更新 更多