Skip to content

Instantly share code, notes, and snippets.

@B0yma
Created August 11, 2019 18:13
Show Gist options
  • Save B0yma/d187317c97b9fa8967f5f83aaedd89f0 to your computer and use it in GitHub Desktop.
Save B0yma/d187317c97b9fa8967f5f83aaedd89f0 to your computer and use it in GitHub Desktop.
BindingChipGroup
@BindingAdapter("list", "layout")
fun <T> chips(view: ChipGroup, items: List<T>, layoutId: Int) {
view.removeAllViews()
val inflater = view.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
items.forEachIndexed { index, model ->
val binding = DataBindingUtil.inflate<ViewDataBinding>(inflater, layoutId, view, false)
binding.setVariable(BR.model, model)
(binding.root as Chip).id = index
view.addView(binding.root)
binding.executePendingBindings()
}
}
>>>>>>>>>>>>>>chip_textcolor_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#00a1de" android:state_checked="true" />
<item android:color="#f7f9fa" android:state_checked="false" />
</selector>
>>>>>>>>>>>>>>chip_backcolor_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#ffffff" android:state_checked="true" />
<item android:color="#a8b4c4" android:state_checked="false" />
</selector>
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="model"
type="com.boyma.chipgroup.ChipModel"/>
</data>
<com.google.android.material.chip.Chip
android:id="@+id/statchip"
style="@style/ChipStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp"
android:text='@{model.text}'
android:checked="@={model.checked}"
tools:text="Март" />
</layout>
<com.google.android.material.chip.ChipGroup
android:id="@+id/chip_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:list="@{viewModel.itemlist}"
app:layout="@{@layout/item_chip}"
app:singleSelection="true">
...
binding!!.root.chip_group.setOnCheckedChangeListener { gr, id ->
val chip = gr.getChildAt(gr.checkedChipId)
if (chip != null){
gr.forEachIndexed { index, _ ->
gr.getChildAt(index).isClickable = true
}
chip.isClickable = false
}
}
...
class MainActivity2ViewModel : ViewModel() {
val itemlist = listOf(
ChipModel(
mutableLiveDataOf("asd"),
mutableLiveDataOf(false)
),
ChipModel(
mutableLiveDataOf("zxc"),
mutableLiveDataOf(false)
),
ChipModel(
mutableLiveDataOf("qwe"),
mutableLiveDataOf(true)
),
ChipModel(
mutableLiveDataOf("ghj"),
mutableLiveDataOf(false)
),
ChipModel(
mutableLiveDataOf("mol"),
mutableLiveDataOf(false)
)
,ChipModel(
mutableLiveDataOf("asd"),
mutableLiveDataOf(false)
),
ChipModel(
mutableLiveDataOf("zxc"),
mutableLiveDataOf(false)
),
ChipModel(
mutableLiveDataOf("qwe"),
mutableLiveDataOf(true)
),
ChipModel(
mutableLiveDataOf("ghj"),
mutableLiveDataOf(false)
),
ChipModel(
mutableLiveDataOf("mol"),
mutableLiveDataOf(false)
))
}
<!--ChipStyling-->
<style name="ChipStyle" parent="Widget.MaterialComponents.Chip.Entry">
<item name="android:textColor">@color/chip_textcolor_selector</item>
<item name="checkedIconVisible">false</item>
<item name="chipBackgroundColor">@color/chip_backcolor_selector</item>
<item name="chipSurfaceColor">@android:color/transparent</item>
<item name="closeIconVisible">false</item>
<item name="android:textAppearance">@style/ChipTextStyle</item>
<item name="rippleColor">#ffffff</item>
</style>
<style name="ChipTextStyle">
<item name="android:textSize">13dp</item>
</style>
<!--ChipStyling-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment