我创建了一组芯片,当我尝试单击下面的芯片时,监听器没有调用。我想根据芯片选择更新代码。
chipGroup.setOnCheckedStateChangeListener
下面是我正在使用的代码。你能帮我看看下面的代码中缺少什么吗?
class MainFragment : Fragment() {
private var _binding: FragmentLayoutBinding? = null
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentLayoutBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
binding.chipGroup.setOnCheckedStateChangeListener { _, checkedIds ->
val chip: Chip? = view.findViewById(checkedIds[checkedIds.size - 1])
chip?.let {
when (chip.id) {
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<HorizontalScrollView
android:id="@+id/chips_list"
android:layout_width="match_parent"
android:layout_below="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_marginLeft="20.dp"
android:scrollbars="none">
<com.google.android.material.chip.ChipGroup
android:id="@+id/chip_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:singleLine="true"
app:singleSelection="true">
<com.google.android.material.chip.Chip
android:id="@+id/chip_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="One" />
<com.google.android.material.chip.Chip
android:id="@+id/chip_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Two" />
</com.google.android.material.chip.ChipGroup>
</HorizontalScrollView>
</RelativeLayout>
您遇到问题的原因。这是因为您使用了可选芯片。
您可以使用特殊的
style
来解决。
themes.xml(您可以更改颜色值。)
<style name="StatsWidget.MaterialComponents.Chip.Choice" parent="Widget.MaterialComponents.Chip.Choice">
<item name="chipBackgroundColor">@android:color/darker_gray</item>
<item name="android:textColor">@color/white</item>
</style>
您的布局(已添加定义的样式值。)
...
<com.google.android.material.chip.ChipGroup
android:id="@+id/chip_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:singleLine="true"
app:singleSelection="true">
<com.google.android.material.chip.Chip
android:id="@+id/chip_one"
style="@style/StatsWidget.MaterialComponents.Chip.Choice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="One" />
<com.google.android.material.chip.Chip
android:id="@+id/chip_two"
style="@style/StatsWidget.MaterialComponents.Chip.Choice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Two" />
</com.google.android.material.chip.ChipGroup>
...
用例 setOnCheckedStateChangeListener ( 您可以使用自己的结构。)
val chipGroup = findViewById<ChipGroup>(R.id.chip_group)
chipGroup.setOnCheckedStateChangeListener { group, checkedIds ->
checkedIds.forEach { checkedId ->
val checkedChip = group.findViewById<Chip>(checkedId)
Log.e("TAG","Clicked chip ${checkedChip.id}")
}
}