本文主要是介绍android 列表单选 /多选/全选删除,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
模拟点假数据对应的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@Overridepublic RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType ) {MyRecycleHolder myRecycleHolder = new MyRecycleHolder(LayoutInflater.from(context).inflate(R.layout.item_profile, parent, false));return myRecycleHolder;}@Overridepublic 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() {@Overridepublic void onCheckedChanged(CompoundButton compoundButton, boolean b) {//点击了勾选设置成选中truelist.get(position).setaBoolean(b);}});}@Overridepublic 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@Overridepublic FragmentProfileBinding provideViewBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup container) {return FragmentProfileBinding.inflate(inflater, container, false);}@Overrideprotected 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() {@Overridepublic 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() {@Overridepublic 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单选
这篇关于android 列表单选 /多选/全选删除的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!