【问题标题】:RecyclerView checkbox deselect all?RecyclerView 复选框取消全选?
【发布时间】:2016-08-11 02:22:44
【问题描述】:

我想知道如何使用另一个类中的按钮来取消选择我的 3 个 recyclerviews(Tablayout,每个标签一个 recyclerview)中的所有复选框。我已将选中的值保存在共享首选项中,如下所示:

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

}

这里是我尝试清除共享首选项的地方,我认为这也会清除所有复选标记:

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 语句以查看是否可行,但现在它不加载所有已保存的复选标记,即使未单击按钮也是如此...大声笑

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

【问题讨论】:

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


    【解决方案1】:

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

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

    编辑:

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

    所以你需要一种方法来获取 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 中的复选框

    【讨论】:

    • 这种让我很困惑..我想一定有更简单的方法吧?我编辑了我的问题以尝试新的可能性
    • 对我来说似乎是最简单的解决方案:将单击侦听器绑定到“全选/取消全选”按钮,循环通过适配器的项目,同时将它们设置为选中/未选中,并通知适配器具有视图已更新。
    猜你喜欢
    • 2016-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多