首页 > 解决方案 > 以编程方式生成的芯片不响应在 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...

标签: javaandroid

解决方案


设法通过改变我制作它们的方式来使芯片做出响应

LayoutInflater layoutInflater = getLayoutInflater();
Chip chip = (Chip)layoutInflater.inflate(R.layout.my_filter_chip,ingredientsChipGroup, false);

推荐阅读