Kir*_*ari 1 checkbox android android-recyclerview
在我的代码中,我创建了具有复选框的recyclerview,并且已经选择了默认的一个项目.现在我想要选择其他项目复选框,所以取消选择所有其他项目意味着一个项目选择时间.
我的适配器代码:
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());
}
}
}
Run Code Online (Sandbox Code Playgroud)
第二个问题是当我滚动循环视图时为什么选中的项目未选中.请帮我.
an_*_*dev 14
1-您可以在Adapter类中创建一个可用于保存所选位置的变量:
private int selectedPosition = -1;// no selection by default
2-内部onBindViewHolder设置检查状态:
holder.checkBox.setChecked(selectedPosition == position);
3-在setOnCheckedChangeListener里面你必须更新位置
this.selectedPosition = holder.getAdapterPosition();
4-刷新适配器
adapter.notifyDatasetChanged();
编辑
将为所有位置调用onBindViewHolder,
因此方法setChecked将调用所有CheckBoxes,此方法具有布尔输入.
在上面的例子中我写setChecked(selectedPosition == position).为了更具可读性,我可以写:
if(selectedPosition == position){
holder.checkBox.setChecked(true);
}
else{
holder.checkBox.setChecked(false)
}
Run Code Online (Sandbox Code Playgroud)
这是最好的代码!注意BindViewHolder
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);
}
}
Run Code Online (Sandbox Code Playgroud)
}
| 归档时间: |
|
| 查看次数: |
13344 次 |
| 最近记录: |