Android RecycleView使用(CheckBox全选、反选、单选)
本文实例为大家分享了CheckBox全选、反选、单选的具体代码,供大家参考,具体内容如下
MainActiivity
packagecom.bwie.day06; importandroid.support.v7.app.AppCompatActivity; importandroid.os.Bundle; importandroid.support.v7.widget.LinearLayoutManager; importandroid.support.v7.widget.RecyclerView; importandroid.view.View; importcom.bwie.day06.myadapter.MyAdapter; publicclassMainActivityextendsAppCompatActivity{ privateMyAdaptermyAdapter; privateLinearLayoutManagerlinearLayoutManager; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化控件 RecyclerViewrecyclerView=(RecyclerView)findViewById(R.id.recyclerview); //在加载数据之前配置 linearLayoutManager=newLinearLayoutManager(this); recyclerView.setLayoutManager(linearLayoutManager); //创建一个适配器 myAdapter=newMyAdapter(); recyclerView.setAdapter(myAdapter); } publicvoidbtnAll(Viewview){ myAdapter.All(); } publicvoidbtnner(Viewview){ myAdapter.neverall(); } }
Adapter
packagecom.bwie.day06.myadapter; importandroid.support.v7.widget.RecyclerView; importandroid.view.LayoutInflater; importandroid.view.View; importandroid.view.ViewGroup; importandroid.widget.CheckBox; importandroid.widget.TextView; importcom.bwie.day06.R; importjava.util.ArrayList; importjava.util.HashMap; importjava.util.Map; importjava.util.Set; publicclassMyAdapterextendsRecyclerView.Adapter{ //这个是checkbox的Hashmap集合 privatefinalHashMap map; //这个是数据集合 privatefinalArrayList list; publicMyAdapter(){ map=newHashMap<>(); list=newArrayList<>(); for(inti=0;i<30;i++){ //添加30条数据 list.add("这是条目"+i); map.put(i,false); } } /** *全选 */ publicvoidAll(){ Set >entries=map.entrySet(); booleanshouldall=false; for(Map.Entry entry:entries){ Booleanvalue=entry.getValue(); if(!value){ shouldall=true; break; } } for(Map.Entry entry:entries){ entry.setValue(shouldall); } notifyDataSetChanged(); } /** *反选 */ publicvoidneverall(){ Set >entries=map.entrySet(); for(Map.Entry entry:entries){ entry.setValue(!entry.getValue()); } notifyDataSetChanged(); } /** *单选 * *@parampostion */ publicvoidsinglesel(intpostion){ Set >entries=map.entrySet(); for(Map.Entry entry:entries){ entry.setValue(false); } map.put(postion,true); notifyDataSetChanged(); } //这里主要初始化布局控件 @Override publicMyViewHolderonCreateViewHolder(ViewGroupparent,intviewType){ RecyclerView.LayoutManagerlayoutManager=((RecyclerView)parent).getLayoutManager(); //初始化布局文件 Viewinflate=LayoutInflater.from(parent.getContext()).inflate(R.layout.check,parent,false); returnnewMyViewHolder(inflate); } @Override publicvoidonBindViewHolder(finalMyViewHolderholder,finalintposition){ //放入集合中的值 holder.txt.setText(list.get(position)); holder.checkBox.setChecked(map.get(position)); holder.checkBox.setOnClickListener(newView.OnClickListener(){ @Override publicvoidonClick(Viewv){ map.put(position,!map.get(position)); //刷新适配器 notifyDataSetChanged(); //单选 //singlesel(position); } }); } @Override publicintgetItemCount(){ returnlist.size(); } publicclassMyViewHolderextendsRecyclerView.ViewHolder{ ViewitemView; privateTextViewtxt; privateCheckBoxcheckBox; //初始化控件 publicMyViewHolder(ViewitemView){ super(itemView); this.itemView=itemView; txt=(TextView)itemView.findViewById(R.id.txt); checkBox=(CheckBox)itemView.findViewById(R.id.cbox); } } }
main.xml
Check.xml
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。