gpt4 book ai didi

即使设置了lifecycleOwner,Android Fragment ViewModel数据绑定(bind)也不会更新UI

转载 作者:行者123 更新时间:2023-12-05 00:14:52 25 4
gpt4 key购买 nike

我想使用带有数据绑定(bind)的 ViewModel 在单击按钮时禁用复选框,但 UI 不会更新,除非 fragment 被销毁并重新创建。

似乎有很多类似的问题,但最相似的问题似乎都可以通过设置 binding.lifecycleOwner 来解决,我已经完成了。

fragment_checkbox_databinding.xml:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".CheckboxDatabindingFragment">

<data>
<variable
name="viewModel"
type=".CheckboxDatabindingFragmentViewModel" />
</data>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<CheckBox
android:id="@+id/my_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="@{viewModel.checkboxEnabled}" />

<Button
android:id="@+id/my_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="@{() -> viewModel.startClicked()}" />

</LinearLayout>
</layout>

CheckboxDatabindingFragment.kt:

class CheckboxDatabindingFragment() : Fragment() {
private lateinit var viewModel: CheckboxDatabindingFragmentViewModel

private var _binding: FragmentCheckboxDatabindingBinding? = null
private val binding get() = _binding!!

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
viewModel = ViewModelProvider(this).get(CheckboxDatabindingFragmentViewModel::class.java )

_binding = FragmentCheckboxDatabindingBinding.inflate(inflater, container, false)
binding.lifecycleOwner = viewLifecycleOwner // androidx.fragment.app.FragmentViewLifecycleOwner
binding.viewModel = viewModel

return binding.root
}
...
}

CheckboxDatabindingFragmentViewModel.kt:

class CheckboxDatabindingFragmentViewModel(application: Application) : AndroidViewModel(application) {
var checkboxEnabled = true

fun startClicked() {
checkboxEnabled = false
}
}

我知道我可以通过手动获取 CheckboxDatabindingFragment.kt 中的复选框 View 并设置 isEnabled 属性来轻松执行此功能,但这违背了 DataBinding 的目的.

我需要 fragment 而不是 Activity 作为所有者,那么如何更新 fragment UI?

最佳答案

问题:

var checkboxEnabled = true

顶部语句创建一个不可观察 bool 值,因此无法观察到该变量的任何更改,也无法通过生命周期所有者(在您的情况下是 fragment )将其提交到布局。

But, as per documentation:

Sets the LifecycleOwner that should be used for observing changes ofLiveData in this binding. If a LiveData is in one of the bindingexpressions and no LifecycleOwner is set, the LiveData will not beobserved and updates to it will not be propagated to the UI.

因此,单独使用 binding.lifecycleOwner = viewLifecycleOwner 不足以通过数据绑定(bind)发布对布局的更改,而且您还必须使用可被 lifeCycleOwner 观察到的可观察对象,并最终通过数据绑定(bind)将它们的任何更改发布到布局。

解决方案:

要解决此问题,您需要将 checkboxEnabled 更改为可观察的:

选项 1:使用 ObservableBoolean :

class CheckboxDatabindingFragmentViewModel(application: Application) : AndroidViewModel(application) {

val checkboxEnabled = ObservableBoolean(true)

fun startClicked() {
checkboxEnabled.set(false)
}
}

选项 2:使用 MutableLiveData :

class CheckboxDatabindingFragmentViewModel(application: Application) : AndroidViewModel(application) {

val checkboxEnabled = MutableLiveData(true)

fun startClicked() {
checkboxEnabled.value = false
}
}

关于即使设置了lifecycleOwner,Android Fragment ViewModel数据绑定(bind)也不会更新UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69484485/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com