3

这是我根据列表设置芯片组和芯片的代码:

val chipGroup = region_list
                val inflator = LayoutInflater.from(chipGroup.context)
                val children = categoryList.map { categoryName ->

                    val chip = inflator.inflate(R.layout.region, chipGroup, false) as Chip
                    chip.text = categoryName.replace('_', ' ')
                    chip.tag = categoryName
                    chip.setOnCheckedChangeListener { button, isChecked ->
                        val s = viewModel.updateFilter(button.tag as String, isChecked)

//s is a string = {"change", "keep"}
//if s is keep that means that the same chip that was selected was pressed again, and in normal chip behaviour in android that chip would then be deselected, I don't want that
                    }

                }

s 是一个字符串 = {"change", "keep"} 如果 s 是 keep 这意味着再次按下选择的同一芯片,并且在 android 的正常芯片行为中,该芯片将被取消选择,我不想要那。我不敢相信 android 团队从未想过有人需要一个无法取消选择项目的芯片组,芯片的意义何在,将其用作某种过滤器来更新下面的视图。

4

1 回答 1

0

试试这个逻辑:

您的活动

    int i=0;
    String chipArr[]={"First","Second","Third","Fourth"};
    int selectedChipId=0;

    for (final String chipName : chipArr) {
         Chip chip = (Chip) layoutInflater.inflate(R.layout.cat_chip_group_item_filter, chipGroup, false);
            chip.setText(chipName);
            chip.setId(i);
    }

    for (final String chipName : fileNameChip) {
         LayoutInflater layoutInflater = getLayoutInflater();
         Chip chip = (Chip) layoutInflater.inflate(R.layout.cat_chip_group_item_filter, chipGroup, false);
         chip.setText(chipName);
         chip.setId(i);

        if (i == 0) chip.setSelected(true);

     chip.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                   selectedChipId=v.getId();
             }
             });
        chipGroup.addView(chip);
        i++;
    }

     chipGroup.setOnCheckedChangeListener(new ChipGroup.OnCheckedChangeListener() {
              @Override
              public void onCheckedChanged(ChipGroup group, int checkedId) {
                     Log.d(TAG, "onCheckedChanged: " + checkedId);

                     if (checkedId == selectedChipId) {
                              chip.setSelected(true);
                     }
               }
    });

活动布局:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".activity.ShowCodeActivity">

    <HorizontalScrollView
        android:id="@+id/horScrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:scrollbars="none">

        <com.google.android.material.chip.ChipGroup
            android:id="@+id/chip_group_code_files"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/margin_4dp"
            android:layout_marginEnd="@dimen/margin_4dp"
            app:singleLine="true"
            app:singleSelection="true" />

    </HorizontalScrollView>


</RelativeLayout>

您的cat_chip_group_item_filter .xml:

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

让我知道它是否对你有用,因为我没有测试过!!

于 2019-09-01T16:42:36.140 回答