使用 MVVM Android 在 Switch 组件上使用自定义属性的双向数据绑定
Two-way data binding using custom attributes on a Switch component using MVVM Android
我正在尝试使用 MVVM 和 LiveData 在 Switch 组件上创建双向数据绑定,但我总是收到
错误:找不到符号
在 DataBinderMapperImpl.java,我还有:
执行时出现故障org.jetbrains.kotlin.gradle.internal.KaptExecution
有什么帮助吗?谢谢
ViewModel中的属性
private val _numbers = MutableLiveData<Boolean>()
val numbers: LiveData<Boolean>
get() = _numbers
绑定适配器
@BindingAdapter("numberSwitchAttrChanged")
fun setListeners(view: CompoundButton, attrChange: InverseBindingListener) {
view.setOnCheckedChangeListener { _, _ -> attrChange.onChange() }
}
@BindingAdapter("numbersSwitch")
fun setNumbersSwitch(view: CompoundButton, isChecked: Boolean) {
if (view.isChecked != isChecked) {
view.isChecked = isChecked
}
}
@InverseBindingAdapter(attribute = "numbersSwitch")
fun getNumbersSwitch(view: CompoundButton): Boolean {
return view.isChecked
}
XML
<Switch
android:id="@+id/numbersSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
app:numbersSwitch="@={viewModel.numbers}"
app:layout_constraintEnd_toStartOf="@+id/guideline3"
app:layout_constraintLeft_toRightOf="@+id/lengthText"
app:layout_constraintTop_toBottomOf="@+id/lengthText" />
我决定在 XML
上添加 onCheckedChanged 属性
<Switch
android:id="@+id/numbersSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:checked="@{viewModel.numbersSwitch}"
android:onCheckedChanged="@{(button, on)-> viewModel.setNumbersSwitch(on)}"
app:layout_constraintEnd_toStartOf="@+id/guideline3"
app:layout_constraintLeft_toRightOf="@+id/lengthText"
app:layout_constraintTop_toBottomOf="@+id/lengthText" />
并在 ViewModel 中定义函数 setNumbersSwitch 并删除 BindingAdapter
fun setNumbersSwitch(isChecked: Boolean) {
when (isChecked) {
true -> _numbersSwitch.value = true
false -> _numbersSwitch.value = false
}
}
我正在尝试使用 MVVM 和 LiveData 在 Switch 组件上创建双向数据绑定,但我总是收到
错误:找不到符号
在 DataBinderMapperImpl.java,我还有:
执行时出现故障org.jetbrains.kotlin.gradle.internal.KaptExecution
有什么帮助吗?谢谢
ViewModel中的属性
private val _numbers = MutableLiveData<Boolean>()
val numbers: LiveData<Boolean>
get() = _numbers
绑定适配器
@BindingAdapter("numberSwitchAttrChanged")
fun setListeners(view: CompoundButton, attrChange: InverseBindingListener) {
view.setOnCheckedChangeListener { _, _ -> attrChange.onChange() }
}
@BindingAdapter("numbersSwitch")
fun setNumbersSwitch(view: CompoundButton, isChecked: Boolean) {
if (view.isChecked != isChecked) {
view.isChecked = isChecked
}
}
@InverseBindingAdapter(attribute = "numbersSwitch")
fun getNumbersSwitch(view: CompoundButton): Boolean {
return view.isChecked
}
XML
<Switch
android:id="@+id/numbersSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
app:numbersSwitch="@={viewModel.numbers}"
app:layout_constraintEnd_toStartOf="@+id/guideline3"
app:layout_constraintLeft_toRightOf="@+id/lengthText"
app:layout_constraintTop_toBottomOf="@+id/lengthText" />
我决定在 XML
上添加 onCheckedChanged 属性<Switch
android:id="@+id/numbersSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:checked="@{viewModel.numbersSwitch}"
android:onCheckedChanged="@{(button, on)-> viewModel.setNumbersSwitch(on)}"
app:layout_constraintEnd_toStartOf="@+id/guideline3"
app:layout_constraintLeft_toRightOf="@+id/lengthText"
app:layout_constraintTop_toBottomOf="@+id/lengthText" />
并在 ViewModel 中定义函数 setNumbersSwitch 并删除 BindingAdapter
fun setNumbersSwitch(isChecked: Boolean) {
when (isChecked) {
true -> _numbersSwitch.value = true
false -> _numbersSwitch.value = false
}
}