数据绑定(bind),MaterialCardView应该像Radiogroup

2020年4月20日 19点热度 0条评论

我试图实现两个MaterialCardViews,它们应该像Radiogroup一样工作。因此,如果我单击其中一个,则应取消选中另一个。我正在使用viewModel,liveData和自定义双向数据绑定(bind)来保存这些值以备后用(每封电子邮件发送)。
我成功编写了.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>

View 模型

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()
    }
}

感谢您的帮助,非常感谢!

附注:如果有更好,更轻松的方式来实现这一目标,例如从 View 中告诉viewModel保存isChecked,请通知我。 MaterialCardView默认情况下实现了“isChecked”,但没有逻辑。

解决方案如下:

好的,我已经解决了问题:
首先,更改绑定(bind)适配器
我实际上看不到任何使用双向数据绑定(bind)来实现上述书面案例的方法。这是新的绑定(bind)适配器

// 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布局,因为我们不再使用双向数据绑定(bind)

<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
    }
}

第四,将clickListener添加到XML或Fragment(此处为Fragment)

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

如果有人有任何疑问,请将其写在评论中,我会帮忙。