java - 以编程方式生成的芯片不响应在 ChipGroup 中的点击
问题描述
我制作了一个芯片组,我想在 java 中动态生成它的内容,并用由字符串制成的过滤芯片填充它。我已经制作了芯片组,但尝试添加芯片时我注意到,无论我做什么,芯片都不会响应点击。我还尝试在设计编辑器中制作单个芯片并将其移动到同一个芯片组中,结果是设计编辑器创建的芯片按预期工作(选中和取消选中它),生成的芯片是静态的(在同一个组中)。
我用于单个工作的代码:
<com.google.android.material.chip.ChipGroup
android:id="@+id/iChipGroup"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:chipSpacing="8dp"
app:singleSelection="true" >
<com.google.android.material.chip.Chip
android:id="@+id/chip2"
style="@style/Widget.MaterialComponents.Chip.Filter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="cwae" />
</com.google.android.material.chip.ChipGroup>
我创建动态的方式
Chip chip = new Chip(iChipGroup.getContext());
ChipDrawable chipDrawable = ChipDrawable.createFromAttributes(getContext(), null, 0, R.style.Widget_MaterialComponents_Chip_Filter);
chip.setChipDrawable(chipDrawable);
chip.setText(myText);
iChipGroup.addView(chip);
我也尝试OnCheckedhangeListener
在单芯片上设置两者,但ChipGroup
一无所获。如果有帮助,我的层次结构是(在片段内):
frameLayout
-scrollView
--LinearLayout
---chipGroup
----chip...
解决方案
设法通过改变我制作它们的方式来使芯片做出响应
LayoutInflater layoutInflater = getLayoutInflater();
Chip chip = (Chip)layoutInflater.inflate(R.layout.my_filter_chip,ingredientsChipGroup, false);
推荐阅读
- javascript - Ajax javascript 映射到 Spring 控制器
- python - 如何在python中打印json文件的特定部分?
- sql - SQL:使用相等键和最近键连接(类似于 Pandas 的合并)
- algorithm - 转换后的数组中的第 K 个元素
- hashicorp-vault - 如何将 HashiCorp Vault Agent 与多个应用程序一起使用
- firebase - 如何删除集合中的所有文档,但保留模式的文档除外
- javascript - React Native 中出现此错误的原因是什么?
- android - 动画结束不平滑过渡到新图像
- amazon-web-services - AWS Sagemaker 中的用户 Docker Hub 注册表容器
- laravel - 通过 Laravgel API 使用请求验证类时,在 null 上对成员函数已验证()的错误调用