ChipGroup-防止取消选择芯片

时间:2019-09-01 15:35:58

标签: android material-design android-design-library android-chips

这是我用于基于列表设置芯片组和内部芯片的代码:

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保持不变,则意味着再次按下了所选的相同芯片,并且在android的正常芯片行为中,该芯片随后将被取消选择,我不希望那样。我不敢相信android团队从来没有想过有人会需要一个不能取消选择某个项目的芯片组(芯片的意义是什么),才能将其用作某种过滤器来更新以下视图。

1 个答案:

答案 0 :(得分: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"/>

让我知道它是否对您有用,因为我尚未对其进行测试!