MVVM BindingAdapters 不显示 ProgressBar

MVVM BindingAdapters not showing ProgressBar

我正在尝试使用数据绑定和 BindingAdapters 创建一个简单示例,以便 show/hide ProgressBar 取决于 TextView 是否为空。下面你可以看到我的代码。我做错了什么?

loading_state.xml

<?xml version="1.0" encoding="utf-8"?>
<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">
    <data>
        <variable
            name="textString"
            type="String"/>
    </data>
    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:visibleGone="@{textString==null}">
        <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{textString}"/>
    </android.support.constraint.ConstraintLayout>
</layout>

我的绑定适配器

object BindingAdapters {
    @JvmStatic
    @BindingAdapter("visibleGone")
    fun showHide(view: View, visible: Boolean){
        view.visibility = if (visible) View.VISIBLE else View.GONE
    }
}

我在第二个片段中包含布局以检查文本视图文本

<include layout="@layout/loading_state"
    app:textString="@{textView2.text.toString()}"/>

并且在我的 SecondFramgent class 中,我从 MainFragment class 中获取值(我正在使用新的导航组件)

override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)
    val txtFromMain = SecondFragmentArgs.fromBundle(arguments)
    textView2.text = txtFromMain.txtFromMain
}

我错过了什么?

非常感谢。

您没有为 TV 设置双向数据绑定,因此字符串不会在数据绑定中更新

<TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{textString}"/>

android:text="@{textString}"更改为android:text="@={textString}"

这是问题的初探,有帮助吗?

对于那些面临相同问题的人,您可以在下面找到与我的案例相匹配的解决方案:

我必须更改我的 BindingAdapter。

@BindingAdapter("visibleGone")
fun showHide(view: View, visible: String){
    view.visibility = if (visible.isEmpty()) View.VISIBLE else View.GONE
}