我想创建ChipGroup 2路绑定适配器。我已经复制了默认的RadioGroup绑定适配器,但进行了一些更改,但不适用于两种方式。如果以编程方式将数据设置为可观察,则ChipGroup将从中检索更改。但是手动选择芯片不会将更改设置为可观察。
这是我的适配器
@InverseBindingMethods(InverseBindingMethod(type = ChipGroup::class, attribute = "android:checkedButton", method = "getCheckedRadioButtonId"))
class ChipGroupBindingAdapter {
companion object {
@JvmStatic
@BindingAdapter("android:checkedButton")
fun setCheckedChip(view: ChipGroup?, id: Int) {
if (id != view?.checkedChipId) {
view?.check(id)
}
}
@JvmStatic
@BindingAdapter(value = ["android:onCheckedChanged", "android:checkedButtonAttrChanged"], requireAll = false)
fun setChipsListeners(view: ChipGroup?, listener: ChipGroup.OnCheckedChangeListener?,
attrChange: InverseBindingListener?) {
if (attrChange == null) {
view?.setOnCheckedChangeListener(listener)
} else {
view?.setOnCheckedChangeListener { group, checkedId ->
listener?.onCheckedChanged(group, checkedId)
attrChange.onChange()
}
}
}
}
}
布局文件:
<android.support.design.chip.ChipGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkedButton="@{viewModel.checkedBtnObs}"
app:singleSelection="true">
<android.support.design.chip.Chip
android:id="@+id/first_chip"
style="@style/Widget.MaterialComponents.Chip.Choice"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:checkable="true"
android:text="@string/month_12"
app:chipBackgroundColor="@drawable/chip_background_selector" />
<android.support.design.chip.Chip
android:id="@+id/second_chip"
style="@style/Widget.MaterialComponents.Chip.Choice"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:checkable="true"
android:text="@string/month_6"
android:textAlignment="center"
app:chipBackgroundColor="@drawable/chip_background_selector" />
<android.support.design.chip.Chip
android:id="@+id/third_chip"
style="@style/Widget.MaterialComponents.Chip.Choice"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:checkable="true"
android:text="@string/month_1"
app:chipBackgroundColor="@drawable/chip_background_selector" />
</android.support.design.chip.ChipGroup>
而且可以观察到:
val checkedBtnObs = ObservableInt(R.id.second_chip)
答案 0 :(得分:0)
if (attrChange == null) {
view?.setOnCheckedChangeListener(listener)
} else {
view?.setOnCheckedChangeListener { group, checkedId ->
listener?.onCheckedChanged(group, checkedId)
attrChange.onChange()
}
}
}
也许应该是
if (listener != null) {
view?.setOnCheckedChangeListener(listener)
} else {
view?.setOnCheckedChangeListener { group, checkedId ->
listener?.onCheckedChanged(group, checkedId)
attrChange.onChange()
}
}
}
答案 1 :(得分:0)
我得到了 def _employee_get(self):
return self.env['hr.employee'].search([('user_id', '=', uid)], limit=1)
和 ChipGroup
按预期工作并使用了 DataBinding
的推荐方式,如果有人想知道,这是我的实现:
InverseDataBinding
:
SomeBindingAdapters.kt
...
object PropertyTypeFilterBindingAdapters {
@BindingAdapter("propertyTypeFilter")
@JvmStatic
fun ChipGroup.bindPropertyTypeFilter(marsApiFilter: MarsApiFilter?) =
marsApiFilter?.let { filter ->
when (filter) {
MarsApiFilter.ALL -> check(R.id.filter_all_properties_chip)
MarsApiFilter.RENT -> check(R.id.filter_properties_for_rent_chip)
MarsApiFilter.BUY -> check(R.id.filter_properties_for_buy_chip)
}
}
@InverseBindingAdapter(attribute = "propertyTypeFilter")
@JvmStatic
fun ChipGroup.convertToMarsApiFilter(): MarsApiFilter = when (checkedChipId) {
R.id.filter_properties_for_rent_chip -> MarsApiFilter.RENT
R.id.filter_properties_for_buy_chip -> MarsApiFilter.BUY
else -> MarsApiFilter.ALL
}
@BindingAdapter("propertyTypeFilterAttrChanged")
@JvmStatic
fun ChipGroup.setListeners(attrChange: InverseBindingListener?) =
setOnCheckedChangeListener { _, _ -> attrChange?.onChange() }
}
...
是针对我的具体情况的枚举类。MarsApiFilter
:
some_layout.xml
...
<com.google.android.material.chip.ChipGroup
android:id="@+id/filter_property_type_chip_group"
...
bind:propertyTypeFilter="@={viewModel.propertyTypeFilter}"
>
<com.google.android.material.chip.Chip
android:id="@+id/filter_all_properties_chip"
...
/>
<com.google.android.material.chip.Chip
android:id="@+id/filter_properties_for_rent_chip"
...
/>
<com.google.android.material.chip.Chip
android:id="@+id/filter_properties_for_buy_chip"
...
/>
</com.google.android.material.chip.ChipGroup>
...
只是一个声明为常规命名空间的奇特命名空间:bind
xmlns:bind="http://schemas.android.com/apk/res-auto"
:
SomeViewModel.kt