android - ChipGroup - 防止芯片被取消选择

标签 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 团队从未想过有人需要一个无法取消选择项目的芯片组,芯片的意义是什么,将它用作某种过滤器来更新下面的 View 。

最佳答案

试试这个逻辑:

您的 Activity :

    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);
                     }
               }
    });

Activity 布局:

<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"/>

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

关于android - ChipGroup - 防止芯片被取消选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57747334/

相关文章:

java - 转换为 Dalvik 格式失败,错误 1 ​​- 重复,但无法解析

java - Android TabListener : onTabSelected - add fragment to back stack

android - float 操作按钮锚定 View 创建时的更改

Angular 2 Material Design 具有分层缩进的多选下拉菜单

android - CoordinatorLayout 使用 ViewPager 的 RecyclerView

android - 编辑文本中的密码可见性切换无法正常工作

android - 如何在 ViewPager fragment 中应用共享元素动画?

java - 如何键入字符串内容 :encoded = "Hello"; in java?

android - 操作栏上的共享按钮?

android - 如何将 fragment 添加到自定义抽屉导航模板?