如何在Android中动态添加新的Android芯片? [英] How can I add the new android chips dynamically in Android?
问题描述
我有一个名为Question的类,其中包含标签的String数组.我正在尝试使用Kotlin在Recyclerview中显示每个问题,并在新芯片中显示每个标签.这些芯片将包含在ChipGroup中.
I have a class called Question which contains a String array of tags. I'm trying to show every question in a Recyclerview using Kotlin and every tag in a new chip. These chips will be included inside a ChipGroup.
我的问题是:
如何将阵列的每个标签元素添加到新芯片中? 我正在尝试执行此操作,但显然不起作用.
How can I add every tag element of the array into a new Chip? I'm trying to do this but It's obviously not working.
if (tags != null) {
for (tag in tags) {
val chip = Chip(itemView.context)
}
}
谢谢大家!
推荐答案
您可以像添加其他任何ViewGroup
一样添加Chip
,如下所示:
You can add Chip
s the same way as any other ViewGroup
like so:
for (index in tags.indices) {
val chip = Chip(chipGroup.context)
chip.text= "Item ${tags[index]}"
// necessary to get single selection working
chip.isClickable = true
chip.isCheckable = true
chipGroup.addView(chip)
}
对于singleSelection,请不要忘记添加到您的chipGroup中:
for singleSelection don't forget to add to your chipGroup:
chipGroup.isSingleSelection = true
或xml
app:singleSelection="true"
祝你好运,编码愉快!
这篇关于如何在Android中动态添加新的Android芯片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!