首页 > 解决方案 > 使用复选框在 recyclerview 上搜索。在搜索我的搜索复选框后选中,但默认选中该位置的复选框的原始列表

问题描述

我已经使用复选框搜索了 recyclerview 上的任何项目 - 搜索后默认原始列表位置绑定

标签: checkboxandroid-recyclerview

解决方案


您可以在此处使用所选项目方法。在您的模型类中使用项目选择器布尔值,如下所示:

public class ItemModel {
    boolean isSelcted;

    public boolean isSelcted() {
        return isSelcted;
    }

    public void setSelcted(boolean selcted) {
        isSelcted = selcted;
    }
}

现在,当用户选择任何复选框时,将所选项目的布尔值设置为 true,然后像这样刷新适配器。

checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    for (ItemModel model : itemList) {
                        model.setSelcted(false);
                    }
                    model.setSelcted(true);
                    notifyDataSetChanged();
                }
            }
        });

现在在您的搜索框中搜索任何内容。如果您在 searchList 中传递相同的列表,那么它将按预期显示选中和未选中的复选框。


推荐阅读