安卓材料设计芯片-单选ChipGroup动态添加芯片不起作用

5

我第一次使用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

我尝试过的静态方法是,在主xml中将row_chip_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

但我希望它是动态的。

更新:新场景

首先,我已经在XML中的ChipGroup中添加了四个芯片, 然后尝试在同一ChipGroup中以编程方式添加另外三个芯片。 前四个芯片只允许选择一个,但最后三个芯片允许我选择多个。非常奇怪。

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

感谢您的帮助。


请查看此链接,可能会对您有所帮助:https://dev59.com/kFQJ5IYBdhLWcg3wnHPm#53518090 - AskNilesh
@NileshRathod,似乎与我想要的每次只选择一个芯片略有不同。 - Pratik Butani
然后您需要在ChipGroup中使用app:singleSelection="true"setSingleSelection() - AskNilesh
你确定这行代码 binding.categoryChipsView.addView(child); 是正确的吗?你应该将 chip 添加到 chipGroup 中,而不是添加一个视图(child 不是 chip)。 - Gabriele Mariotti
尝试使用以下代码:Chip chip = (Chip) getLayoutInflater().inflate(R.layout.row_chip_view, chipGroup, false); //.... chipGpRow.addView(chip) - Gabriele Mariotti
显示剩余3条评论
3个回答

19

移除在你的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);
}

1
非常感谢。这是一个愚蠢的未知错误,无法找出原因。 - Pratik Butani
2
我尝试了这个答案,但只有在我动态添加芯片的ID后才起作用,像这样:chip.id = ViewCompat.generateViewId() - akhris

0

不要使用onClickListener,而是使用setOnCheckedChangeListener。在singleSelection="true"的情况下,这对我很有效。


0
在将芯片添加到芯片组之前,请先添加一个整数ID。
    Chip chip = (Chip) getLayoutInflater().inflate(R.layout.custom_chip, chipGroup,
  false);
            chip.setId(i);
            chip.setText("str");
            chipGroup.addView(chip);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接