listview全选反选等

来源:互联网 发布:php websocket作用 编辑:程序博客网 时间:2024/05/16 01:59

忘了参考哪位大神的demo了,自己稍微整理了下。
首先,布局上很简单,
主布局:

<LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">        <Button            android:id="@+id/bt_selectall"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginLeft="10dp"            android:text="全选" />        <Button            android:id="@+id/bt_cancelselectall"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="取消" />        <Button            android:id="@+id/bt_deselectall"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="反选" />        <TextView            android:id="@+id/tv"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="已选0项" />    </LinearLayout>    <ListView        android:id="@+id/lv"        android:layout_width="match_parent"        android:layout_height="wrap_content" />

适配器中的布局:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="50dp"    android:padding="10dp"    android:gravity="center_vertical"    android:orientation="horizontal">    <TextView        android:id="@+id/item_tv"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_weight="1"        android:gravity="center_vertical" />    <CheckBox        android:id="@+id/item_cb"        android:layout_width="61dp"        android:layout_height="36dp"        android:background="@drawable/selector_checkbox"        android:button="@null"        android:clickable="false"        android:focusable="false"        android:focusableInTouchMode="false"        android:gravity="center_vertical" /></LinearLayout>

其中checkbox采用了自定义drawable,用来点击切换checkbox的显示,

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <!-- 点击之后的图标 -->    <item android:drawable="@drawable/cb_print_check" android:state_checked="true" />    <!-- 点击之前的图标 -->    <item android:drawable="@drawable/cb_print" android:state_checked="false" /></selector>

其次,是主界面的代码,在适配器中通过map集合记录item是否被选中的状态:

 public static HashMap<Integer, Boolean> getIsSelected() {        return isSelected;    }

主要说说几个按钮监听事件
1、全选按钮

// 全选按钮的回调接口        bt_selectall.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                // 遍历list的长度,将MyAdapter中的map值全部设为true                for (int i = 0; i < list.size(); i++) {                    MyAdapter.getIsSelected().put(i, true);                }                // 数量设为list的长度                checkNum = list.size();                // 刷新listview和TextView的显示                dataChanged();            }        });

全选时,将checkbox状态全部置为true。

2、取消按钮

 // 取消按钮的回调接口        bt_cancel.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                // 遍历list的长度,将已选的按钮设为未选                for (int i = 0; i < list.size(); i++) {                    if (MyAdapter.getIsSelected().get(i)) {                        MyAdapter.getIsSelected().put(i, false);                        checkNum--;// 数量减1                    }                }                // 刷新listview和TextView的显示                dataChanged();            }        });

取消时,将checkbox状态置为false。
3、反选时,一样的道理

 // 反选按钮的回调接口        bt_deselectall.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                // 遍历list的长度,将已选的设为未选,未选的设为已选                for (int i = 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();            }        });

另外,还有listview的单击事件

// 绑定listView的监听器        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,                                    long arg3) {                // 取得ViewHolder对象,这样就省去了通过层层的findViewById去实例化我们需要的cb实例的步骤                //                MyAdapter.ViewHolder holder = (MyAdapter.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 + "项");            }        });

具体代码稍后上传。

原创粉丝点击