【问题标题】:UI not updated when ViewModel value changedViewModel 值更改时 UI 未更新
【发布时间】:2018-12-31 19:14:42
【问题描述】:

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

一个TextView和一个Button,文本视图可见性绑定到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>()
}

【问题讨论】:

  • 确保您的视图模型在属性 getter 上设置了 @Bindable 注释,它会像 BR.notifyPropertyChanged(BR.prop) 一样通知 setter 中的视图
  • @VahidAmiri "@Bindable" 在使用基本 observable 时是必需的。他正在使用不需要注释的 LiveData。

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


【解决方案1】:

创建绑定后,添加这一行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)

【讨论】:

  • 为什么需要这个?? @杜威
  • @Anmol Databinding 需要在它和 ViewModel 之间建立一个钩子,以便它可以知道数据何时更改并且应该更新。这个方法就是钩子。
  • 这是一个世界奇迹,即使viewmodel 对象已分配给binding 对象,在预期/预期结果发生之前仍需要进行显式setLifecycleOwner() 调用。
猜你喜欢
  • 2023-04-03
  • 2014-01-01
  • 2013-09-22
  • 1970-01-01
  • 2019-10-10
  • 2015-11-11
  • 1970-01-01
  • 1970-01-01
  • 2014-04-16
相关资源
最近更新 更多