【问题标题】:Android Material Chip - ChipGroup with singleSelection doesn't work adding chips dynamicallyAndroid Material Chip - 具有 singleSelection 的 ChipGroup 无法动态添加芯片
【发布时间】:2020-03-04 01:43:05
【问题描述】:

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

我已经静态尝试过,我已将row_chip_view.xml 的视图粘贴为 ChipGroup 的子主 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.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>

但我希望它是动态的。

更新:新场景

首先,我在ChipGroup 中添加了四个 XML 芯片和 之后尝试在同一程序中添加另外三个芯片 ChipGroup。前四个筹码只允许选择一个但最后一个 三个筹码让我可以选择多个。很奇怪。

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

您的帮助将不胜感激。

【问题讨论】:

标签: android material-components material-components-android android-chips


【解决方案1】:

而不是 onClickListener 使用 setOnCheckedChangeListener 这对我有用,与 singleSelection="true" 结合使用

【讨论】:

    【解决方案2】:
    在您的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);
    }
    

    【讨论】:

    • 非常感谢。它愚蠢的未知错误,无法找出。
    • 我试过这个答案,但只有在我像这样动态添加芯片的 ID 后它才有效:chip.id = ViewCompat.generateViewId()
    【解决方案3】:

    在将 Chip 添加到 ChipGroupd 之前添加整数 id

        Chip chip = (Chip) getLayoutInflater().inflate(R.layout.custom_chip, chipGroup,
      false);
                chip.setId(i);
                chip.setText("str");
                chipGroup.addView(chip);
    

    【讨论】:

      猜你喜欢
      • 2022-07-06
      • 2019-11-29
      • 2020-09-02
      • 1970-01-01
      • 2019-12-22
      • 2021-11-08
      • 2020-02-02
      • 1970-01-01
      • 2018-12-18
      相关资源
      最近更新 更多