I would like to create ChipGroup 2-way binding adapter. I have copied default RadioGroup binding adapter with some changes but it doesn't work for both ways. In case of setting data to observable programmatically, ChipGroup retrieve changes from it. But manually Chip selection doesn't set changes to observable.
Here is my adapter
@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()
}
}
}
}
}
Layout file:
<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>
And observable:
val checkedBtnObs = ObservableInt(R.id.second_chip)