android - ViewModel 值更改时 UI 未更新

标签 android android-databinding android-livedata android-viewmodel

我已经创建了示例应用来演示我的问题:

一个TextView和一个Button, TextView 可见性绑定(bind)到viewModel.bar

我希望按钮在单击时切换 viewModel.bar 的值,并且 UI 也得到更新。

然而,这并没有发生。值已更改,但 UI 未更新。

布局文件:

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

    <data>
        <import type="android.view.View"/>
        <variable name="viewModel"
                  type="com.example.bindingone.MainViewModel"/>
    </data>

    <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MainActivity">

        <TextView
                android:id="@+id/text_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hello World"
                android:visibility="@{viewModel.bar ? View.VISIBLE : View.GONE}"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"/>

        <Button
                android:text="Update UI"
                android:onClick="clicked"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

    </android.support.constraint.ConstraintLayout>

</layout>

MainActivity 文件:

class MainActivity : AppCompatActivity() {

    private val viewModel = MainViewModel()

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
        binding.viewModel = viewModel
    }

    fun clicked(v: View) {
        viewModel.bar.value = viewModel.bar.value?.not() ?: false
    }
}

MainViewModel 文件:

class MainViewModel : ViewModel() {
    var bar = MutableLiveData<Boolean>()
}

最佳答案

创建绑定(bind)后,添加此行 binding.setLifecycleOwner(this)

/**
 * Sets the {@link LifecycleOwner} that should be used for observing changes of
 * LiveData in this binding. If a {@link LiveData} is in one of the binding expressions
 * and no LifecycleOwner is set, the LiveData will not be observed and updates to it
 * will not be propagated to the UI.
 *
 * @param lifecycleOwner The LifecycleOwner that should be used for observing changes of
 *                       LiveData in this binding.
 */
@MainThread
public void setLifecycleOwner(@Nullable LifecycleOwner lifecycleOwner)

关于android - ViewModel 值更改时 UI 未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53986644/

相关文章:

可绘制对象上的 Android 数据绑定(bind)

android - 在 Kotlin 中使用多个参数绑定(bind)适配器

android - 如何将暂停函数的结果传递给非暂停函数?

android - 结合 android ViewModel 和数据绑定(bind)的最佳实践

android - 无法使用生命周期版本 2.2.0-alpha01 功能

安卓 : Live Data skipping values

android - textView 中的多行省略号 - 这是一个已知错误吗?

android - Firebase 崩溃在启动期间产生 ANR

android - AppBarLayout 在滚动时在屏幕底部留下一条白带

android - 在 Android 中覆盖 setOnCheckedChangeListener