RecyclerView复选框取消全选?

时间:2016-08-10 15:06:14

标签: android android-recyclerview android-adapter android-checkbox

我想知道我如何能够使用另一个班级中的按钮来取消选择3个recyclerviews中的所有复选框(Tablayout,每个标签一个回收站视图)。我已将选中的值保存在共享pref中,如下所示:

public class MyRecyclerAdapter extends RecyclerView.Adapter<MyViewHolder> {

Context mContext;
ArrayList<Workout> workout;
SharedPreferences prefs;
int firstSecondOrThird;
int colorResId = R.color.defaultcard;

public MyRecyclerAdapter(Context context, ArrayList<Workout> workout, int thePosition) {
    mContext = context;
    this.workout = workout;
    this.firstSecondOrThird = thePosition;
}
// INITIALIZE HOLDER
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.workout_item, null);
    MyViewHolder holder = new MyViewHolder(view);

    return holder;
}

//BIND DATA TO VIEWS
@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
    holder.exercise.setText(workout.get(position).getExercise());
    holder.percent.setText(workout.get(position).getPercent());
    holder.reps.setText(workout.get(position).getReps());
    holder.weight.setText(workout.get(position).getWeight());
    holder.check1.setOnCheckedChangeListener(null);

    final Workout isCheck = workout.get(position);

    prefs = mContext.getSharedPreferences("checkState", Context.MODE_PRIVATE);
       holder.check1.setChecked(prefs.getBoolean(firstSecondOrThird+"checkState"+position, false));
    isCheck.setCheck1(prefs.getBoolean(firstSecondOrThird+"checkState"+position, false));
holder.check1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            prefs.edit().putBoolean(firstSecondOrThird+"checkState"+position, isChecked).apply();
        }
    });

}

以下是我尝试清除共享pref的地方,我认为这也会清除所有选中标记:

mButton = (Button) findViewById(R.id.button2);

    mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            prefs.edit().clear();
            prefs.edit().apply();
            Toast.makeText(getApplicationContext(), "Checkboxes Cleared.", Toast.LENGTH_LONG).show();

        }
    });
}

不幸的是,点击按钮后,仍然会选中之前选中的所有复选框。提前谢谢!

编辑:我试图添加一个if语句来查看是否可行,但现在它没有加载已保存的复选标记,即使没有点击按钮... lol

prefs = mContext.getSharedPreferences("checkState", Context.MODE_PRIVATE);
    String restoredCheck = prefs.getString("checkState", null);
    if (restoredCheck != null) {
        holder.check1.setChecked(prefs.getBoolean(firstSecondOrThird+"checkState"+position, false));
    } else holder.check1.setChecked(false);

1 个答案:

答案 0 :(得分:1)

您需要更改适配器内的List,然后调用((MyRecyclerAdapter) mMyListView.getAdapter()).notifyDataSetChanged();

对于经验,有时根据所做的更改,适配器不会更新,在这种情况下,我创建一个新的适配器与新的数据列表,并设置为ListView,之后,您可能需要无论如何要调用notifyDataSetChanged()

编辑:

好吧,在您的适配器中使用“List”,必须在该List中完成状态更改,然后通知适配器数据已更改。

所以你需要一个方法来获取ArrayList并执行类似的操作:

    mButton = (Button) findViewById(R.id.button2);

        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ArrayList<Workout> list = ((MyRecyclerAdapter) MyListView.getAdapter()).getList();

    for(Workout workout : list){
workout.setCheck(true);

}

((MyRecyclerAdapter) MyListView.getAdapter()).notifyDataSetChanged();

            }
        });
    }

我现在无法测试,但我认为就像我说的那样,如果这不起作用,请使用新数据创建另一个适配器并将其设置为ListView。

聚苯乙烯。我认为您正确检查对象是否已检查并更改ViewHoler中的复选框