如何让ChipGroup
像RadioGroup
一样始终至少有一个选定的项目?设置setSingleSelection(true)
也会使得在单击Chip
两次时可以不选择任何项。
如何让ChipGroup
像RadioGroup
一样始终至少有一个选定的项目?设置setSingleSelection(true)
也会使得在单击Chip
两次时可以不选择任何项。
setSelectionRequired
:chipGroup.setSelectionRequired(true)
您还可以在布局中使用app:selectionRequired
属性进行定义:
<com.google.android.material.chip.ChipGroup
app:singleSelection="true"
app:selectionRequired="true"
app:checkedChip="@id/..."
..>
注意:此要求最低版本为1.2.0
从版本1.2.0-alpha02
开始,旧的hacky解决方案不再需要!
可以使用属性app:selectionRequired="true"
<com.google.android.material.chip.ChipGroup
android:id="@+id/group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:selectionRequired="true"
app:singleSelection="true">
(...)
</com.google.android.material.chip.ChipGroup>
或者在代码中
// Kotlin
group.isSelectionRequired = true
// Java
group.setSelectionRequired(true);
需要两个步骤才能实现此功能。
我们已经内置了此支持,只需确保将app:singleSelection="true"
添加到您的ChipGroup
中,例如:
XML
<com.google.android.material.chip.ChipGroup
android:id="@+id/group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:singleSelection="true">
<com.google.android.material.chip.Chip
android:id="@+id/option_1"
style="@style/Widget.MaterialComponents.Chip.Choice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 1" />
<com.google.android.material.chip.Chip
android:id="@+id/option_2"
style="@style/Widget.MaterialComponents.Chip.Choice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 2" />
</com.google.android.material.chip.ChipGroup>
代码
// Kotlin
group.isSingleSelection = true
// Java
group.setSingleSelection(true);
现在要实现类似于单选框组的功能:
var lastCheckedId = View.NO_ID
chipGroup.setOnCheckedChangeListener { group, checkedId ->
if(checkedId == View.NO_ID) {
// User tried to uncheck, make sure to keep the chip checked
group.check(lastCheckedId)
return@setOnCheckedChangeListener
}
lastCheckedId = checkedId
// New selection happened, do your logic here.
(...)
}
根据文档:
ChipGroup 还支持一组芯片的多重排除范围。 当你设置 app:singleSelection 属性时,选择属于芯片组的一个芯片会取消选中同一组内之前选中的任何芯片。 行为类似于 RadioGroup。
group.check(lastCheckId)
中仍有一个拼写错误。 - AlgoRyangroup.check()
不会导致这个监听器被多次冗余地调用吗?由于 ChipGroup
中的一些守卫代码,只会多出一个调用,但仍然存在。 - AlgoRyanreturn@setOnCheckedChangeListener
的返回类型是什么吗? - Agapito Gallart Bernat@ setOnCheckedChangeListener
是 Kotlin 语法,它指的是你想要在哪个语句处返回,而在这种情况下,是运行监听器 lambda 的逻辑。 - Joaquim LeychipGroup.setOnCheckedChangeListener((chipGroup, id) -> {
Chip chip = ((Chip) chipGroup.getChildAt(chipGroup.getCheckedChipId()));
if (chip != null) {
for (int i = 0; i < chipGroup.getChildCount(); ++i) {
chipGroup.getChildAt(i).setClickable(true);
}
chip.setClickable(false);
}
});
getChildAt()
接受一个索引。for (i in 0 until group.childCount) {
val chip = group.getChildAt(i)
chip.isClickable = chip.id != group.checkedChipId
}
以下是更完整的 `setOnCheckedChangeListener`,以供参考:
intervalChipGroup.setOnCheckedChangeListener { group, checkedId ->
for (i in 0 until group.childCount) {
val chip = group.getChildAt(i)
chip.isClickable = chip.id != group.checkedChipId
}
when (checkedId) {
R.id.intervalWeek -> {
view.findViewById<Chip>(R.id.intervalWeek).chipStrokeWidth = 1F
view.findViewById<Chip>(R.id.intervalMonth).chipStrokeWidth = 0F
view.findViewById<Chip>(R.id.intervalYear).chipStrokeWidth = 0F
currentIntervalSelected = weekInterval
populateGraph(weekInterval)
}
R.id.intervalMonth -> {
view.findViewById<Chip>(R.id.intervalWeek).chipStrokeWidth = 0F
view.findViewById<Chip>(R.id.intervalMonth).chipStrokeWidth = 1F
view.findViewById<Chip>(R.id.intervalYear).chipStrokeWidth = 0F
currentIntervalSelected = monthInterval
populateGraph(monthInterval)
}
R.id.intervalYear -> {
view.findViewById<Chip>(R.id.intervalWeek).chipStrokeWidth = 0F
view.findViewById<Chip>(R.id.intervalMonth).chipStrokeWidth = 0F
view.findViewById<Chip>(R.id.intervalYear).chipStrokeWidth = 1F
currentIntervalSelected = yearInterval
populateGraph(yearInterval)
}
}
}
大多数的回答对我来说都非常好并且很有帮助。对于@adriennoir和@Todd DeLand提出的一个小修改,以防止在setSingleSelection(true)
ChipGroup中取消已经选中的chip,这是我的解决方案:
for (i in 0 until chipGroup.childCount) {
val chip = chipGroup.getChildAt(i) as Chip
chip.isCheckable = chip.id != chipGroup.checkedChipId
chip.isChecked = chip.id == chipGroup.checkedChipId
}
val chip = inflater.inflate(
R.layout.item_crypto_currency_category_chip,
binding.chipGroupCryptoCurrencyCategory,
false) as Chip
chip.id = ViewCompat.generateViewId()
binding.chipGroupCryptoCurrencyCategory.addView(chip)
//Set default value with index 0 when ChipGroup created.
if (index == 0) binding.chipGroupCryptoCurrencyCategory.check(chip.id)
item_crypto_currency_category_chip.xml
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.chip.Chip xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/chip_smart_contract"
style="@style/Widget.Signal.Chip"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
item_crypto_currency_tag_category.xml
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/spacing_6x"
android:scrollbars="none"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.chip.ChipGroup
android:id="@+id/chip_group_crypto_currency_category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:singleSelection="true"
app:singleLine="true"
/>
</HorizontalScrollView>
Result:
var previousSelection: Int = default_selection_id
chipGroup.setOnCheckedChangeListener { chipGroup, id ->
if (id == -1) //nothing is selected.
chipGroup.check(previousSelection)
else
previousSelection = id
chipGroup.check()
来进行单个重复调用onCheckedChange()
。 - AlgoRyan这是我的工作解决方案
mChipGroup.setOnCheckedChangeListener((group, checkedId) -> {
for (int i = 0; i < mChipGroup.getChildCount(); i++) {
Chip chip = (Chip) mChipGroup.getChildAt(i);
if (chip != null) {
chip.setClickable(!(chip.getId() == mChipGroup.getCheckedChipId()));
}
}
});