- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
TL;DR - 以编程方式选择默认选择 Chip 的正确方法是什么,它是 Android 中 ChipGroup 的子级?
--
在我的 android 应用程序中,我使用 com.google.android.material.chip.Chip
样式为 @style/Widget.MaterialComponents.Chip.Choice
表示用户可以为给定路线选择的 Activity 选择的组件(想想步行、自行车等)
因为一条路线可以有不同类型的 Activity ,所以我以编程方式将每种类型作为不同的芯片插入到 com.google.android.material.chip.ChipGroup
中。 .我还在 fragment 的 onViewCreated() 期间使用以下代码选择默认芯片作为插入列表中的第一个芯片
private fun setupTypeSelection(types: List<Type>) {
types.forEach { type ->
val chip = layoutInflater.inflate(R.layout.chip_type, viewBinding.typeChipGroup, false) as Chip
chip.tag = type
/* Init chip text and icon */
chip.setOnClickListener {
/* Update selected type */
}
if (currentType == null) {
chip.isSelected = true
currentType = type
}
viewBinding.typeChipGroup.addView(chip)
}
}
<com.google.android.material.chip.ChipGroup
android:id="@+id/type_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_medium"
app:chipSpacingHorizontal="@dimen/margin_medium"
app:selectionRequired="true"
app:singleLine="true"
app:singleSelection="true" />
<com.google.android.material.chip.Chip xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
style="@style/Widget.MaterialComponents.Chip.Choice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:chipIconEnabled="true" />
chip.isSelected = true
即使在用户通过 UI 交互选择了不同的选项后,它仍保持选中状态。
最佳答案
找到了我的答案。
View.generateViewId()
并将新 ID 分配给viewBinding.typeChipGroup.check(id)
查看芯片组private fun setupTypeSelection(types: List<Trail.Type>) {
types.forEach { type ->
val chip = layoutInflater.inflate(R.layout.chip_trail_type, viewBinding.typeContainer, false) as Chip
chip.id = View.generateViewId()
/* Set chip details as usual */
viewBinding.typeContainer.addView(chip)
if (currentType == null) viewBinding.typeChipGroup.check(chip.id)
}
}
关于android - 如何以编程方式选择 Android 芯片组中的默认芯片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61765154/
我正在使用 Angular 4 的芯片组和自动完成功能开发 stackoverflow 中的标记系统等功能。这是我写的一段代码。它不起作用
我正在尝试制作一个基于 ChipGroup 和 Chip 的 recyclerview 过滤器 我在我的应用程序上使用 fragment ,因此,包含 RecyclerView 的 fragment
因此,我创建了一个可组合的 Chip 并在 LazyRow 中使用它,如下所示: LazyRow( modifier = modifier, horizontalArr
我使用 Javacv code 在 Andorid 中制作了一个自定义相机.我没有包含所有的 .so 文件,而只包含了我的应用程序似乎需要的文件。如下面的屏幕截图所示: 它适用于 Samsumg Ga
我是一名优秀的程序员,十分优秀!