【问题标题】:Android filterable recyclerview with checkboxes带有复选框的Android可过滤recyclerview
【发布时间】:2023-03-28 21:51:01
【问题描述】:

我正在开发一个应用程序,其中有 RecyclerView 带有复选框和搜索视图。对于从编辑文本中搜索,我添加了

search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

afterTextChanged() 中,我正在更改列表并致电notifydatasetChanged()

但复选框的状态在搜索后会丢失。有什么方法可以保持复选框的状态吗?

【问题讨论】:

  • 可以分享源代码吗?

标签: android android-layout search checkbox android-recyclerview


【解决方案1】:

是的,您可以创建 ArrayList 并将所有选定的项目保存在该数组中。在通知列表之后,您可以检查 id`s ,如果它(包含)在该数组列表中可用,那么您只需使用 setChecked(true) 选中该复选框;

例如:

// Creating and adding data in arraylist
ArrayList<String> selectedCat = new ArrayList<String>();
selectedCat.add('Your-selected-id');

//Checked array list item and placed in binder method
if(selectedCat.contains("Your-current-ids")){
  your-checkbox-object.setChecked(true);
}else{
  your-checkbox-object.setChecked(false);
}

【讨论】:

  • 完美回答。谢谢。
【解决方案2】:

首先在适配器中创建布尔数组,值为假。如果用户检查数组,则使数组的位置为真,取消选中使其为假。稍后总是从该布尔数组中设置复选框值。

更多请看这篇

http://android-pratap.blogspot.in/2015/01/recyclerview-with-checkbox-example.html

Onscroll selected checkbox getting unchecked using listiview

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 2011-06-28
    • 1970-01-01
    • 1970-01-01
    • 2016-07-31
    • 2023-03-14
    • 2019-05-15
    相关资源
    最近更新 更多