gpt4 book ai didi

Android Material Chip - 具有 singleSelection 的 ChipGroup 无法动态添加芯片

转载 作者:行者123 更新时间:2023-12-02 15:31:47 24 4
gpt4 key购买 nike

我正在使用Material Chip第一次。

问题:我使用以下代码动态添加芯片。请检查我写的 app:singleSelection="true" 这对我来说很重要。 即使它一次选择多个芯片。

我想一次仅选择一个带刻度线的芯片

XML 代码:

<com.google.android.material.chip.ChipGroup
android:id="@+id/categoryChipsView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
app:chipSpacing="10dp"
app:singleSelection="true"
app:itemSpacing="15dp"
app:singleLine="true">
</com.google.android.material.chip.ChipGroup>

Java 代码:

private void addChipView(String chipText) {
View child = getLayoutInflater().inflate(R.layout.row_chip_view, null);
Chip chip = child.findViewById(R.id.chip);
chip.setText(chipText);
chip.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext, ((Chip) v).getText(), Toast.LENGTH_SHORT).show();
}
});
// This is ChipGroup view
binding.categoryChipsView.addView(child);
}

row_chip_view.xml

<com.google.android.material.chip.Chip xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/chip"
style="@style/Widget.MaterialComponents.Chip.Filter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="@string/app_name"
android:textColor="@android:color/white"
app:checkedIcon="@drawable/ic_check"
app:chipBackgroundColor="@color/colorAccent"
app:chipEndPadding="8dp"
app:chipIconTint="@android:color/white"
app:chipStartPadding="8dp"
app:textEndPadding="5dp"
app:textStartPadding="5dp" />

enter image description here

我静态地尝试过,我已将 row_chip_view.xml 的 View 粘贴为主 xml 中 ChipGroup 的子级,并且工作正常。我一次只能选择一个芯片。

           <com.google.android.material.chip.ChipGroup
android:id="@+id/categoryChipsView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
app:chipSpacing="10dp"
app:singleSelection="true"
app:itemSpacing="15dp"
app:singleLine="true">

<com.google.android.material.chip.Chip
android:id="@+id/chip"
style="@style/Widget.MaterialComponents.Chip.Filter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="@string/app_name"
android:textColor="@android:color/white"
app:checkedIcon="@drawable/ic_check"
app:chipBackgroundColor="@color/colorAccent"
app:chipEndPadding="8dp"
app:chipIconTint="@android:color/white"
app:chipStartPadding="8dp"
app:textEndPadding="5dp"
app:textStartPadding="5dp" />

<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:layout_margin="10dp"
android:text="@string/app_name"
android:textColor="@android:color/white"
app:checkedIcon="@drawable/ic_check"
app:chipBackgroundColor="@color/colorAccent"
app:chipEndPadding="8dp"
app:chipIconTint="@android:color/white"
app:chipStartPadding="8dp"
app:textEndPadding="5dp"
app:textStartPadding="5dp" />

<com.google.android.material.chip.Chip
android:id="@+id/chip3"
style="@style/Widget.MaterialComponents.Chip.Filter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="@string/app_name"
android:textColor="@android:color/white"
app:checkedIcon="@drawable/ic_check"
app:chipBackgroundColor="@color/colorAccent"
app:chipEndPadding="8dp"
app:chipIconTint="@android:color/white"
app:chipStartPadding="8dp"
app:textEndPadding="5dp"
app:textStartPadding="5dp" />

</com.google.android.material.chip.ChipGroup>

enter image description here

但我想要动态的。

Update: New Scenario

First of all I have added four chips in XML within ChipGroup and after that tried to add another three chips PROGRAMATICALLY in same ChipGroup. The first four chips is allow to select only one but last three chips allow me to select multiple. Very Weird.

如果我遗漏了什么,请告诉我。

我们将不胜感激您的帮助。

最佳答案

删除row_chip_view.xml中的android:id属性。

<com.google.android.material.chip.Chip 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
style="@style/Widget.MaterialComponents.Chip.Filter"
.../>

并在addChipView中使用:

private void addChipView(String chipText) {
Chip chip = (Chip) getLayoutInflater().inflate(R.layout.row_chip_view, chipGroup, false);
chip.setText(chipText);
//...

// This is ChipGroup view
chipGroup.addView(chip);
}

关于Android Material Chip - 具有 singleSelection 的 ChipGroup 无法动态添加芯片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58749368/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com