模拟点假数据对应的bean类 添加一个boolean值默认是flase 默认不选中,根据自己的需求
public class ProFileBean {
private String pic;
private String title;
private boolean aBoolean;
public boolean isaBoolean() {
return aBoolean;
}
public void setaBoolean(boolean aBoolean) {
this.aBoolean = aBoolean;
}
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
下面就是适配器adapert 我的选框是小方框 控件CheckBox
public class ProFileAdapert extends RecyclerView.Adapter {
private Context context;
ArrayList<ProFileBean> list;
public ProFileAdapert(Context context, ArrayList<ProFileBean> list) {
this.context = context;
this.list = list;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType ) {
MyRecycleHolder myRecycleHolder = new MyRecycleHolder(LayoutInflater.from(context).inflate(R.layout.item_profile, parent, false));
return myRecycleHolder;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
MyRecycleHolder holder1 = (MyRecycleHolder) holder;
holder1.title.setText("" + list.get(position).getTitle());
//设置checkbox的选中状态
holder1.itmeCheck.setChecked(list.get(position).isaBoolean());
holder1.itmeCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
//点击了勾选设置成选中true
list.get(position).setaBoolean(b);
}
});
}
@Override
public int getItemCount() {
return list == null ? 0 : list.size();
}
class MyRecycleHolder extends RecyclerView.ViewHolder {
private ImageView imgPic;
private TextView title;
private CheckBox itmeCheck;
public MyRecycleHolder(@NonNull View itemView) {
super(itemView);
imgPic = (ImageView) itemView.findViewById(R.id.imgPic);
title = (TextView) itemView.findViewById(R.id.fileTitle);
itmeCheck = (CheckBox) itemView.findViewById(R.id.itmeCheck);
}
}
}
下面是对应的 主页面
public class ProFileFragment extends BaseDJFragment<FragmentProfileBinding> {
private RecyclerView recyList;
private ArrayList<ProFileBean> list = new ArrayList<>();
private ProFileAdapert proFileAdapert;
private TextView deleteFile;
private CheckBox checkAll;
boolean isnoCheckAll = false;
ProFileBean proFileBean = new ProFileBean();
ProFileBean proFileBean2 = new ProFileBean();
ProFileBean proFileBean3 = new ProFileBean();
ProFileBean proFileBean4 = new ProFileBean();
ProFileBean proFileBean5 = new ProFileBean();
@NonNull
@Override
public FragmentProfileBinding provideViewBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup container) {
return FragmentProfileBinding.inflate(inflater, container, false);
}
@Override
protected void setupView(@NonNull View view, @Nullable Bundle savedInstanceState) {
deleteFile = view.findViewById(R.id.deleteFile);
checkAll = view.findViewById(R.id.checkAll);
recyList = view.findViewById(R.id.recy_list);
proFileBean.setTitle("11111111");
proFileBean2.setTitle("222222");
proFileBean3.setTitle("33333333");
proFileBean4.setTitle("444444");
proFileBean5.setTitle("55555");
list.add(proFileBean);
list.add(proFileBean2);
list.add(proFileBean3);
list.add(proFileBean4);
list.add(proFileBean5);
recyList.setLayoutManager(new GridLayoutManager(getActivity(), 4));
recyList.setAdapter(proFileAdapert = new ProFileAdapert(getActivity(), list));
//全选
checkAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
isnoCheckAll = true;
for (int i = 0; i < proFileAdapert.list.size(); i++) {
proFileAdapert.notifyDataSetChanged();
list.get(i).setaBoolean(true);
}
} else {
for (int i = 0; i < proFileAdapert.list.size(); i++) {
list.get(i).setaBoolean(false);
}
isnoCheckAll = false;
}
proFileAdapert.notifyDataSetChanged();
}
});
//删除
deleteFile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (isnoCheckAll) {
proFileAdapert.list.clear();
proFileAdapert.notifyDataSetChanged();
} else {
for (int i = 0; i < list.size(); i++) {
if (list.get(i).isaBoolean() == true) {
list.remove(i);
i--;
//由于删除了一个元素,所以需要将i-1
}
}
proFileAdapert.notifyDataSetChanged();
}
}
});
}
}
主布局就是一个 CheckBox 全选 一个textview删除 一个对应的recycleview
适配器子布局 就是一个textview文字 左右一个 CheckBox单选