双向数据绑定在Viewholder中无法正常工作

时间:2018-11-01 17:18:39

标签: android android-databinding

所以我有一个带复选框的viewHolder 这是我的viewModel

@Bindable
var itemIsSelected: Boolean = isSelected
    set(value) {
        if (field != value) {
            field = value
            notifyPropertyChanged(BR.itemIsSelected) // this doesn't work
            notifyChange() // this one works
        }
    }

这是我的viewHolder类

inner class SpecialityItemViewHolder(val binding: ItemSpecialityFilterBinding): RecyclerView.ViewHolder(binding.root) {
        fun bind(specialityItemViewModel: SpecialityItemViewModel) {
            binding.viewModel = specialityItemViewModel
            binding.executePendingBindings()
            this.itemView.setOnClickListener {
                binding.viewModel?.let {
                    it.itemIsSelected = !it.itemIsSelected // this doesn't trigger ui changes
                }
            }
        }
    }

xml

<?xml version="1.0" encoding="utf-8"?>
<layout>
    <data>
        <variable
            name="viewModel"
            type="packagename.ItemViewModel" />
    </data>

    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/vertical_margin_small"
        android:paddingBottom="@dimen/vertical_margin_small"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools">

        <Checkbox
            android:id="@+id/checkbox"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:checked="@={viewModel.itemIsSelected}"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"/>

    </android.support.constraint.ConstraintLayout>
</layout>

所以发生的是该设置正常工作,因为当我按下复选框时,它将后备字段设置为相应的值 但是当我设置背景字段(bind函数中的通知代码)时,它不会触发用户界面更改,我知道调用binding.executePendingBindings()可以解决问题,但我的理解是notifyPropertyChanged(BR.itemIsSelected)不需要executePendingBindings调用,实际上,如果我调用notifyChange,那么一切都会正常工作(但是我认为这里存在性能问题,因为它会通知所有属性更改)

2 个答案:

答案 0 :(得分:0)

您的ViewModel类必须扩展BaseObservable类,而使用kotlin则必须使用@get:Bindable注释。如果您不想将BaseObservable用作父类,请使用ObservableField<Boolean>()。您可以在https://developer.android.com/topic/libraries/data-binding/observability#kotlin

中找到更多信息。

答案 1 :(得分:0)

视图模型需要Kotlin注释,否则注释处理器将忽略它:

class ViewModel : BaseObservable() {

    @get:Bindable
    var isSelected: Boolean
        set(value) {
            if (isSelected != value) {
                isSelected = value
                notifyPropertyChanged(BR.isSelected)
            }
        }
}

it.isSelectedit.itemIsSelected更容易阅读。