android - 更改 LiveData 时 DataBinding 不会更新值

标签 android kotlin data-binding android-livedata android-databinding

当我按下按钮并将其写入 textview 时,我希望该值增加。 但是,下面的代码将增加的值显示为 Log 但是,在屏幕上,textview 中的值不会改变。 我不确定出了什么问题。如果您知道,请告诉我。

fragment .kt

class SettingFragment : Fragment(), View.OnClickListener {

    companion object {
        fun newInstance() = SettingFragment()
        private val TAG = "SettingFragment"
    }

    private lateinit var viewModel: SettingViewModel
    private lateinit var binding: FragmentSettingBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_setting, container, false)
        return binding.root
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        viewModel = ViewModelProvider(this).get(SettingViewModel::class.java)
        binding.lifecycleOwner = this
        binding.button.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        viewModel.increase()
        Log.d(TAG, "Log Data" + viewModel.testInt.value)
    }

}

fragment View 模型

class SettingViewModel : ViewModel() {


    val testInt: MutableLiveData<Int> = MutableLiveData()

    init {
        testInt.value = 0
    }

    fun increase() {
        testInt.value = testInt.value?.plus(1)
    }
}

fragment .xml

<?xml version="1.0" encoding="utf-8"?>
<layout>
    <data>
        <variable
            name="viewModel"
            type="com.example.ui.setting.SettingViewModel" />
    </data>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".ui.setting.SettingFragment">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@{String.valueOf(viewModel.testInt)}" />

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
    </LinearLayout>
</layout>

最佳答案

    <variable
        name="viewModel"
        type="com.example.ui.setting.SettingViewModel" />

viewModel 变量未以编程方式绑定(bind)到某个变量。因此,当值增加时,它不会反射(reflect)到布局中。

要解决此问题,请将 fragment 的 viewModel 设置为此值:

    viewModel = ViewModelProvider(this).get(SettingViewModel::class.java)
    binding.viewModel = viewModel // <<<<< Here is the change
    binding.lifecycleOwner = this
    binding.button.setOnClickListener(this)

旁注:

你不应该使用 onActivityCreated() 因为它是 deprecated as of API level 28 ;您通常可以将其部分代码添加到onCreateView()。你可以check here用于其他替代方案。

关于android - 更改 LiveData 时 DataBinding 不会更新值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68493518/

相关文章:

c# - 如何设置依赖属性值?

javascript - 有没有办法判断哪部分JS代码最耗电?

android - 按下状态波纹效果+正常状态透明

android - 包名称与正则表达式不匹配

java - 如何在 GridLayoutManager 中改变每条偶数行的方向?

collections - 具有非空值的 Kotlin 映射

android - 错误 :- Could not find databinding-compiler. jar

.net - 错误模板绑定(bind)错误wpf

java - 如何在 .java 文件中使用 @string/xyz?

generics - 复制 Kotlin 数组减去特定索引处的元素