如果像 flipkart 价格范围一样在 RecyclerView 中检查过,如何将复选框设置为 true

How to set check box true if once checked in RecyclerView like flipkart price range

你好,如果用户选中一次并应用与 flipkart 价格过滤器相同的过滤器,任何人都可以帮助我如何在 RecyclerView 中设置选中的 true 复选框

这是我的代码:-

    final ResultGetCategory datum = moviesList.get(position);
    holder.label_category_name.setText(datum.getName());
    //checkbox click event handling

    holder.checkbox.setChecked(datum.isSelected());
    holder.checkbox.setTag(moviesList.get(position));

    SharedPreferences prefsCheckBox = getContext().getSharedPreferences(String.valueOf(holder.checkbox.getTag()), Context.MODE_PRIVATE);
    final SharedPreferences.Editor editorCheckbox = prefsCheckBox.edit();

    holder.checkbox.setTag(moviesList.get(position));
    holder.checkbox.setChecked(
            prefsCheckBox.getBoolean(String.valueOf(holder.checkbox.getTag()), false));
    holder.checkbox.setOnCheckedChangeListener(new
                                                       CompoundButton.OnCheckedChangeListener() {
                                                           @Override
                                                           public void onCheckedChanged(CompoundButton buttonView,
                                                                                        boolean isChecked) {
                                                               //add your code here when set checked changed


                                                               editorCheckbox.putBoolean(String.valueOf(holder.checkbox.getTag()),isChecked);
                                                               editorCheckbox.apply();
                                                           }
                                                       });

尝试使用它而不是在点击时设置:

    holder.checkbox.setTag(moviesList.get(position));
    holder.checkBox.setChecked(
           prefs.getBoolean(
              String.valueOf(holder.checkbox.getTag(),false));
    holder.checkBox.setOnCheckedChangeListener(new 
         CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, 
                     boolean isChecked) {
            //add your code here when set checked changed
            editor.putBoolean(
                String.valueOf(holder.checkbox.getTag()),isChecked);
            editor.apply();
        }
    });

此代码适用于 ListView 中的多个复选框 每个复选框都有一个 sharedpreferences 布尔值,所有默认值为 false。

您的更新代码:

    final ResultGetCategory datum = moviesList.get(position);
    holder.label_category_name.setText(datum.getName());
    //checkbox click event handling

    holder.checkbox.setChecked(datum.isSelected());
    holder.checkbox.setTag(moviesList.get(position));
/*
  //move commented lines to viewholder class or in constructor of 
      adapter
  //exactly these codes and dont change them
    SharedPreferences prefsCheckBox = 
       getContext().getSharedPreferences("checkbox", 
           Context.MODE_PRIVATE);
final SharedPreferences.Editor editorCheckbox = prefsCheckBox.edit();
*/
holder.checkbox.setTag(moviesList.get(position));
holder.checkbox.setChecked(

prefsCheckBox.getBoolean(
        String.valueOf(holder.checkbox.getTag()), false));
holder.checkbox.setOnCheckedChangeListener(new                                                    
          CompoundButton.OnCheckedChangeListener() {
       @Override
      public void onCheckedChanged(CompoundButton buttonView,                                                                                   
                                     boolean isChecked) {
  //add your code here when set checked changed



   editorCheckbox.putBoolean(
      String.valueOf(holder.checkbox.getTag()),isChecked);

        editorCheckbox.apply();
        }
 });

在测试此代码之前,请从设备中删除您的应用并重新安装 不要忘记更改 prefs 初始化的位置,因为它会发生在每个单元格上,但事实并非如此。