Android RecycleView使用(CheckBox全选、反选、单选)
程序员文章站
2023-12-16 14:39:04
本文实例为大家分享了checkbox全选、反选、单选的具体代码,供大家参考,具体内容如下
mainactiivity
package com.bwie.da...
本文实例为大家分享了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>> entries = map.entryset(); for (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" />
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读