数据绑定,MaterialCardView 应该像 Radiogroup

Databinding, MaterialCardView should act like Radiogroup

我正在尝试实现两个应该像 Radiogroup 一样工作的 MaterialCardView。因此,如果我单击一个,则应取消选中另一个。我正在使用 viewModel、liveData 和自定义双向数据绑定来保存这些值以备后用(通过电子邮件发送)。

我成功编写了 .xml 并实现了检查逻辑,但我很难实现取消检查逻辑。

XML,为了更好的可见性而缩短的版本

<layout
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>
        <variable
            name="vm"
            type="com.example.app.data.viewmodel.EmailViewModel" />
    </data>

        <com.google.android.material.card.MaterialCardView
            android:id="@+id/cardViewOne"
            android:checkable="true"
            android:clickable="true"
            android:focusable="true"
            <!-- Custom Two way databinding -->
            app:state_checked="@={vm.cardOptionOneChecked}"
        </com.google.android.material.card.MaterialCardView>


        <com.google.android.material.card.MaterialCardView
            android:id="@+id/cardViewTwo"
            android:checkable="true"
            android:clickable="true"
            android:focusable="true"
            <!-- Custom Two way databinding -->
            app:state_checked="@={vm.cardOptionTwoChecked}">

        </com.google.android.material.card.MaterialCardView>
</layout>

视图模型

class EmailViewModel @ViewModelInject constructor(
    @Assisted private val savedStateHandle: SavedStateHandle
) : ViewModel() {

    // Variable for Id = cardViewOne
    val cardOptionOneChecked = MutableLiveData<Boolean>()
    
    // Variable for Id = cardViewTwo
    val cardOptionTwoChecked = MutableLiveData<Boolean>()
}

CardViewAdapter.kt

@BindingAdapter("state_checked")
fun setStateChecked(view: MaterialCardView, liveData: MutableLiveData<Boolean>) {
    if (view.isChecked != liveData.value) {
        liveData.value = view.isChecked
    }
}

@InverseBindingAdapter(attribute = "state_checked")
fun getStateChecked(view: MaterialCardView,): Boolean {
    return view.isChecked
}

// I don't know what logic belongs here to make it work! 
// Current approach just checks the current view and does nothing more. How can I save the last
// checked value?
@BindingAdapter("state_checkedAttrChanged")
fun setCheckedAttrListener(
    view: MaterialCardView,
    attrChange: InverseBindingListener,
) {
    view.apply {
        setOnClickListener { view.isChecked = true }
        setOnCheckedChangeListener { card, isChecked ->
            if (card.isChecked && card != view) {
                card.isChecked = false
            }
        }
        attrChange.onChange()
    }
}

感谢大家的帮助,非常感谢!

P.S: 如果有更好更简单的方法来实现这个,例如从视图中告诉 viewModel 保存 isChecked,请通知我。 MaterialCardView 默认实现了“isChecked”但没有逻辑。

好的,我已经解决了问题:

首先,更改绑定适配器

我其实没看到有什么方法可以用two-way数据绑定来实现上面写的案例。这是新的绑定适配器

// View = Clicked MaterialCard, liveData = value in viewModel
@BindingAdapter("state_checked")
fun setStateChecked(view: MaterialCardView, liveData: MutableLiveData<Boolean>) {
    if (view.isChecked != liveData.value) {
        if (liveData.value != null) {
            view.isChecked = liveData.value!!
        }
    }
}

其次,更改XML布局,因为我们不再使用two-way数据绑定

<layout
        xmlns:app="http://schemas.android.com/apk/res-auto">
    
        <data>
            <variable
                name="vm"
                type="com.example.app.data.viewmodel.EmailViewModel" />
        </data>
    
            <com.google.android.material.card.MaterialCardView
                android:id="@+id/cardViewOne"
                android:checkable="true"
                android:clickable="true"
                android:focusable="true"
                <!-- Deleted "=" -->
                app:state_checked="@{vm.cardOptionOneChecked}"
            </com.google.android.material.card.MaterialCardView>
    

            <com.google.android.material.card.MaterialCardView
                android:id="@+id/cardViewTwo"
                android:checkable="true"
                android:clickable="true"
                android:focusable="true"
                <!-- Deleted "=" -->
                app:state_checked="@{vm.cardOptionTwoChecked}">
    
            </com.google.android.material.card.MaterialCardView>
    </layout>

三、更改viewmodel

class EmailViewModel @ViewModelInject constructor(
    @ApplicationContext context: Context,
    @Assisted private val savedStateHandle: SavedStateHandle
) : ViewModel() {

    val cardOptionOneChecked = MutableLiveData<Boolean>()
    
    val cardOptionTwoChecked = MutableLiveData<Boolean>()

    // Added
     fun firstCardClicked() {
        cardOneChecked.value = true
        cardTwoChecked.value = false
    }

    fun secondCardClicked() {
        cardOneChecked.value = false
        cardTwoChecked.value = true
    }
}

四、给XML或Fragment(这里是Fragment)添加clickListener

cardViewOne.setOnClickListener {
    viewModel.firstCardClicked()
}
cardViewTwo.setOnClickListener {
    viewModel.secondCardClicked()
}

如果有人有任何问题,请在评论中提出,我会提供帮助。