本文实例为大家分享了CheckBox全选、反选、单选的具体代码,供大家参考,具体内容如下
MainActiivity
package com.bwIE.day06;import androID.support.v7.app.AppCompatActivity;import androID.os.Bundle;import androID.support.v7.Widget.linearlayoutmanager;import androID.support.v7.Widget.RecyclerVIEw;import androID.vIEw.VIEw;import com.bwIE.day06.myadapter.MyAdapter;public class MainActivity extends AppCompatActivity { private MyAdapter myAdapter; private linearlayoutmanager linearlayoutmanager; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); //初始化控件 RecyclerVIEw recyclerVIEw = (RecyclerVIEw) findVIEwByID(R.ID.recyclervIEw); //在加载数据之前配置 linearlayoutmanager = new linearlayoutmanager(this); recyclerVIEw.setLayoutManager(linearlayoutmanager); //创建一个适配器 myAdapter = new MyAdapter(); recyclerVIEw.setAdapter(myAdapter); } public voID btnAll(VIEw vIEw) { myAdapter.All(); } public voID btnner(VIEw vIEw) { myAdapter.neverall(); }}
Adapter
package com.bwIE.day06.myadapter;import androID.support.v7.Widget.RecyclerVIEw;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.CheckBox;import androID.Widget.TextVIEw;import com.bwIE.day06.R;import java.util.ArrayList;import java.util.HashMap;import java.util.Map;import java.util.Set;public class MyAdapter extends RecyclerVIEw.Adapter<MyAdapter.MyVIEwHolder> { //这个是checkBox的Hashmap集合 private final HashMap<Integer,Boolean> map; //这个是数据集合 private final ArrayList<String> List; public MyAdapter() { map = new HashMap<>(); List = new ArrayList<>(); for (int i = 0; i < 30; i++) { //添加30条数据 List.add("这是条目" + i); map.put(i,false); } } /** * 全选 */ public voID All() { Set<Map.Entry<Integer,Boolean>> entrIEs = map.entrySet(); boolean shouldall = false; for (Map.Entry<Integer,Boolean> entry : entrIEs) { Boolean value = entry.getValue(); if (!value) { shouldall = true; break; } } for (Map.Entry<Integer,Boolean> entry : entrIEs) { entry.setValue(shouldall); } notifyDataSetChanged(); } /** * 反选 */ public voID neverall() { Set<Map.Entry<Integer,Boolean>> entrIEs = map.entrySet(); for (Map.Entry<Integer,Boolean> entry : entrIEs) { entry.setValue(!entry.getValue()); } notifyDataSetChanged(); } /** * 单选 * * @param postion */ public voID singlesel(int postion) { Set<Map.Entry<Integer,Boolean> entry : entrIEs) { entry.setValue(false); } map.put(postion,true); notifyDataSetChanged(); } //这里主要初始化布局控件 @OverrIDe public MyVIEwHolder onCreateVIEwHolder(VIEwGroup parent,int vIEwType) { RecyclerVIEw.LayoutManager layoutManager = ((RecyclerVIEw) parent).getLayoutManager(); //初始化布局文件 VIEw inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.check,parent,false); return new MyVIEwHolder(inflate); } @OverrIDe public voID onBindVIEwHolder(final MyVIEwHolder holder,final int position) { //放入集合中的值 holder.txt.setText(List.get(position)); holder.checkBox.setChecked(map.get(position)); holder.checkBox.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { map.put(position,!map.get(position)); //刷新适配器 notifyDataSetChanged(); //单选// singlesel(position); } }); } @OverrIDe public int getItemCount() { return List.size(); } public class MyVIEwHolder extends RecyclerVIEw.VIEwHolder { VIEw itemVIEw; private TextVIEw txt; private CheckBox checkBox; //初始化控件 public MyVIEwHolder(VIEw itemVIEw) { super(itemVIEw); this.itemVIEw = itemVIEw; txt = (TextVIEw) itemVIEw.findVIEwByID(R.ID.txt); checkBox = (CheckBox) itemVIEw.findVIEwByID(R.ID.cBox); } }}
main.xml
<linearLayout androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:orIEntation="horizontal"> <button androID:onClick="btnAll" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_weight="1" androID:text="全选" /> <button androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_weight="1" androID:text="反选" androID:onClick="btnner"/></linearLayout><androID.support.v7.Widget.RecyclerVIEw androID:ID="@+ID/recyclervIEw" androID:layout_wIDth="match_parent" androID:layout_height="match_parent"></androID.support.v7.Widget.RecyclerVIEw>
Check.xml
<CheckBox androID:ID="@+ID/cBox" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" /><TextVIEw androID:text="dfdfdfdf" androID:ID="@+ID/txt" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:textSize="20sp" />
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android RecycleView使用(CheckBox全选、反选、单选)全部内容,希望文章能够帮你解决Android RecycleView使用(CheckBox全选、反选、单选)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)