Android(安卓)带checkbox的listView 实现多选,全选,反选
16lz
2021-01-26
摘抄至:http://www.cnblogs.com/tianshidechibang234/p/3441106.html
Android 带checkbox的listView 实现多选,全选,反选
由于listview的一些特性,刚开始写这种需求的功能的时候都会碰到一些问题,重点就是存储每个checkbox的状态值,在这里分享出了完美解决方法:
布局文件:
[html] view plain copy- <?xmlversion="1.0"encoding="utf-8"?>
- <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="horizontal">
- <TextView
- android:id="@+id/tv"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_gravity="center_vertical"/>
- <LinearLayout
- android:id="@+id/line"
- android:layout_width="fill_parent"
- android:layout_height="50dp"
- android:layout_below="@+id/tv"
- android:orientation="horizontal">
- <Button
- android:id="@+id/bt_selectall"
- android:layout_width="80dp"
- android:layout_height="fill_parent"
- android:text="全选"/>
- <Button
- android:id="@+id/bt_cancleselectall"
- android:layout_width="80dp"
- android:layout_height="fill_parent"
- android:text="反选"/>
- <Button
- android:id="@+id/bt_deselectall"
- android:layout_width="80dp"
- android:layout_height="fill_parent"
- android:text="取消选择"/>
- </LinearLayout>
- <ListView
- android:id="@+id/lv"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_below="@+id/line"/>
- </RelativeLayout>
listView 的item布局文件:
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="horizontal">
- <TextView
- android:id="@+id/item_tv"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_gravity="center_vertical"
- android:layout_weight="1"/>
- <CheckBox
- android:id="@+id/item_cb"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:clickable="false"
- android:focusable="false"
- android:focusableInTouchMode="false"
- android:gravity="center_vertical"/>
- </LinearLayout>
Activity:
- publicclassEx_checkboxActivityextendsActivity{
- privateListViewlv;
- privateMyAdaptermAdapter;
- privateArrayList<String>list;
- privateButtonbt_selectall;
- privateButtonbt_cancel;
- privateButtonbt_deselectall;
- privateintcheckNum;//记录选中的条目数量
- privateTextViewtv_show;//用于显示选中的条目数量
- /**Calledwhentheactivityisfirstcreated.*/
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- /*实例化各个控件*/
- lv=(ListView)findViewById(R.id.lv);
- bt_selectall=(Button)findViewById(R.id.bt_selectall);
- bt_cancel=(Button)findViewById(R.id.bt_cancelselectall);
- bt_deselectall=(Button)findViewById(R.id.bt_deselectall);
- tv_show=(TextView)findViewById(R.id.tv);
- list=newArrayList<String>();
- //为Adapter准备数据
- initDate();
- //实例化自定义的MyAdapter
- mAdapter=newMyAdapter(list,this);
- //绑定Adapter
- lv.setAdapter(mAdapter);
- //全选按钮的回调接口
- bt_selectall.setOnClickListener(newOnClickListener(){
- @Override
- publicvoidonClick(Viewv){
- //遍历list的长度,将MyAdapter中的map值全部设为true
- for(inti=0;i<list.size();i++){
- MyAdapter.getIsSelected().put(i,true);
- }
- //数量设为list的长度
- checkNum=list.size();
- //刷新listview和TextView的显示
- dataChanged();
- }
- });
- //反选按钮的回调接口
- bt_cancel.setOnClickListener(newOnClickListener(){
- @Override
- publicvoidonClick(Viewv){
- //遍历list的长度,将已选的设为未选,未选的设为已选
- for(inti=0;i<list.size();i++){
- if(MyAdapter.getIsSelected().get(i)){
- MyAdapter.getIsSelected().put(i,false);
- checkNum--;
- }else{
- MyAdapter.getIsSelected().put(i,true);
- checkNum++;
- }
- }
- //刷新listview和TextView的显示
- dataChanged();
- }
- });
- //取消按钮的回调接口
- bt_deselectall.setOnClickListener(newOnClickListener(){
- @Override
- publicvoidonClick(Viewv){
- //遍历list的长度,将已选的按钮设为未选
- for(inti=0;i<list.size();i++){
- if(MyAdapter.getIsSelected().get(i)){
- MyAdapter.getIsSelected().put(i,false);
- checkNum--;//数量减1
- }
- }
- //刷新listview和TextView的显示
- dataChanged();
- }
- });
- //绑定listView的监听器
- lv.setOnItemClickListener(newOnItemClickListener(){
- @Override
- publicvoidonItemClick(AdapterView<?>arg0,Viewarg1,intarg2,
- longarg3){
- //取得ViewHolder对象,这样就省去了通过层层的findViewById去实例化我们需要的cb实例的步骤
- ViewHolderholder=(ViewHolder)arg1.getTag();
- //改变CheckBox的状态
- holder.cb.toggle();
- //将CheckBox的选中状况记录下来
- MyAdapter.getIsSelected().put(arg2,holder.cb.isChecked());
- //调整选定条目
- if(holder.cb.isChecked()==true){
- checkNum++;
- }else{
- checkNum--;
- }
- //用TextView显示
- tv_show.setText("已选中"+checkNum+"项");
- }
- });
- }
- //初始化数据
- privatevoidinitDate(){
- for(inti=0;i<15;i++){
- list.add("data"+""+i);
- }
- ;
- }
- //刷新listview和TextView的显示
- privatevoiddataChanged(){
- //通知listView刷新
- mAdapter.notifyDataSetChanged();
- //TextView显示最新的选中数目
- tv_show.setText("已选中"+checkNum+"项");
- };
- }
列表适配器:
- packagecom.notice.listcheck;
- importjava.util.ArrayList;
- importjava.util.HashMap;
- importandroid.content.Context;
- importandroid.view.LayoutInflater;
- importandroid.view.View;
- importandroid.view.ViewGroup;
- importandroid.widget.BaseAdapter;
- importandroid.widget.CheckBox;
- importandroid.widget.TextView;
- publicclassMyAdapterextendsBaseAdapter{
- //填充数据的list
- privateArrayList<String>list;
- //用来控制CheckBox的选中状况
- privatestaticHashMap<Integer,Boolean>isSelected;
- //上下文
- privateContextcontext;
- //用来导入布局
- privateLayoutInflaterinflater=null;
- //构造器
- publicMyAdapter(ArrayList<String>list,Contextcontext){
- this.context=context;
- this.list=list;
- inflater=LayoutInflater.from(context);
- isSelected=newHashMap<Integer,Boolean>();
- //初始化数据
- initDate();
- }
- //初始化isSelected的数据
- privatevoidinitDate(){
- for(inti=0;i<list.size();i++){
- getIsSelected().put(i,false);
- }
- }
- @Override
- publicintgetCount(){
- returnlist.size();
- }
- @Override
- publicObjectgetItem(intposition){
- returnlist.get(position);
- }
- @Override
- publiclonggetItemId(intposition){
- returnposition;
- }
- @Override
- publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
- ViewHolderholder=null;
- if(convertView==null){
- //获得ViewHolder对象
- holder=newViewHolder();
- //导入布局并赋值给convertview
- convertView=inflater.inflate(R.layout.listviewitem,null);
- holder.tv=(TextView)convertView.findViewById(R.id.item_tv);
- holder.cb=(CheckBox)convertView.findViewById(R.id.item_cb);
- //为view设置标签
- convertView.setTag(holder);
- }else{
- //取出holder
- holder=(ViewHolder)convertView.getTag();
- }
- //设置list中TextView的显示
- holder.tv.setText(list.get(position));
- //根据isSelected来设置checkbox的选中状况
- holder.cb.setChecked(getIsSelected().get(position));
- returnconvertView;
- }
- publicstaticHashMap<Integer,Boolean>getIsSelected(){
- returnisSelected;
- }
- publicstaticvoidsetIsSelected(HashMap<Integer,Boolean>isSelected){
- MyAdapter.isSelected=isSelected;
- }
- publicstaticclassViewHolder{
- TextViewtv;
- CheckBoxcb;
- }
- }
更多相关文章
- android 反编译心得
- android 获取网络数据,回传到本地用TextView显示乱码问题解决方法
- Android(安卓)ListView动画(逐行显示动画效果)
- 使用Html在EditText中任意位置插入图片并正确显示
- LinearLayout不能显示全部内容
- android编程常见问题-程序在模拟器中不显示
- Android(安卓)之Toast
- Android(安卓)Studio 中Copyright 设置
- [Android开发] 代码code设置9.png/9-patch 图片背景后,此view中的