gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 13:40:55 26 4
gpt4 key购买 nike

我正在尝试实现两个应该像 Radiogroup 一样的 MaterialCardViews。因此,如果我单击一个,则应取消选中另一个。我正在使用 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()
}
}
我感谢每一个帮助,非常感谢!
P.S:如果有更好更简单的方法来实现这一点,例如从 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()
}
如果有人有任何问题,请写在评论中,我会提供帮助。

关于java - 数据绑定(bind),MaterialCardView 应该像 Radiogroup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63567889/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com