如何在 Recyclerview 中仅选择一个复选框并更改通知数据集 [英] How can I select only one checkbox in Recyclerview and notifydataset changed
本文介绍了如何在 Recyclerview 中仅选择一个复选框并更改通知数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的代码中,我创建了带有复选框的 recyclerview,并且默认已经选择了一项.现在我想在选择其他项目复选框时取消选择所有其他项目意味着一次选择一个项目.
In my code I have create recyclerview with check box and default one item selected already. now I want when select other item checkbox so deselect all other items mean one item select at time.
我的适配器代码:
public class SupportSchoolIdAdapter extends RecyclerView.Adapter<SupportSchoolIdAdapter.ViewHolder> {
ArrayList<SupportSchoolIdModel> supportSchoolIdModels;
DataPref mDataPref;
String supportSchoolId;
public SupportSchoolIdAdapter(List<SupportSchoolIdModel> supportSchoolIdModels) {
this.supportSchoolIdModels = new ArrayList<>(supportSchoolIdModels);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
mDataPref = DataPref.getInstance(context);
supportSchoolId = mDataPref.getSupportSchoolId();
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.support_school_item, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
final SupportSchoolIdModel event = supportSchoolIdModels.get(position);
holder.bindData(supportSchoolIdModels.get(position));
//in some cases, it will prevent unwanted situations
holder.checkbox.setOnCheckedChangeListener(null);
//if true, your checkbox will be selected, else unselected
holder.checkbox.setChecked(supportSchoolIdModels.get(position).isSelected());
holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
supportSchoolIdModels.get(holder.getAdapterPosition()).setSelected(isChecked);
}
});
if (supportSchoolIdModels.get(position).getPkSchoolId().equalsIgnoreCase(supportSchoolId)) {
holder.checkbox.setChecked(true);
} else {
holder.checkbox.setChecked(false);
}
}
@Override
public int getItemCount() {
return supportSchoolIdModels.size();
}
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
private TextView schoolIdTxt;
private TextView schoolNameTxt;
private CheckBox checkbox;
public ViewHolder(View v) {
super(v);
schoolIdTxt = (TextView) v.findViewById(R.id.schoolIdTxt);
schoolNameTxt = (TextView) v.findViewById(R.id.schoolNameTxt);
checkbox = (CheckBox) v.findViewById(R.id.checkbox);
}
public void bindData(SupportSchoolIdModel supportSchoolIdModel) {
schoolIdTxt.setText(supportSchoolIdModel.getPkSchoolId());
schoolNameTxt.setText(supportSchoolIdModel.getSchoolName());
}
}
}
第二个问题是当我滚动回收视图时为什么未选中所选项目.请帮帮我.
And Second problem is when I scroll recycle view why selected item unchecked. please help me.
推荐答案
这是最好的代码!关于BindViewHolder的通知
this is the best code! notice onBindViewHolder
public class SupportSchoolIdAdapter extends RecyclerView.Adapter<SupportSchoolIdAdapter.ViewHolder > {
private Context context;
ArrayList<SupportSchoolIdModel> supportSchoolIdModels;
private int selectedPosition = 0;
private ArrayList<Integer> selectCheck = new ArrayList<>();
public SupportSchoolIdAdapter (Context context, ArrayList<SupportSchoolIdModel> supportSchoolIdModels) {
this.context = context;
this.supportSchoolIdModels = supportSchoolIdModels;
//initilize selectCheck
for (int i = 0; i < supportSchoolIdModels.size(); i++) {
selectCheck.add(0);
}
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.support_school_item, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
if (selectCheck.get(position) == 1) {
holder.checkbox.setChecked(true);
} else {
holder.checkbox.setChecked(false);
}
holder.checkbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for(int k=0; k<selectCheck.size(); k++) {
if(k==position) {
selectCheck.set(k,1);
} else {
selectCheck.set(k,0);
}
}
notifyDataSetChanged();
}
});
holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked==true){
//Do whatever you want to do with selected value
}
}
});
}
@Override
public int getItemCount() {
return supportSchoolIdModels== null? 0: supportSchoolIdModels.size();
}
class ViewHolder extends RecyclerView.ViewHolde {
CheckBox checkbox;
public ViewHolder(View v) {
super(v);
checkbox = (CheckBox) v.findViewById(R.id.checkbox);
}
}
}
这篇关于如何在 Recyclerview 中仅选择一个复选框并更改通知数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文