checkbox的listView实现多选,全选,反选

来源:互联网 发布:seo怎么优化 开发 编辑:程序博客网 时间:2024/04/28 01:54

简单的布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context=".MainActivity" >    <TextView        android:id="@+id/tv_check"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:textColor="#f00"        android:textSize="25sp"        android:text="共选中:" />    <LinearLayout         android:layout_width="match_parent"    android:layout_height="wrap_content"        >        <Button         android:id="@+id/bt_all_select"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_weight="1"        android:text="全选"        />        <Button         android:id="@+id/bt_invert_select"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_weight="1"        android:text="反选"        />        <Button         android:id="@+id/bt_cancel_select"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_weight="1"        android:text="取消选择"        />    </LinearLayout>    <ListView         android:id="@+id/lv_listview"        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1"        ></ListView></LinearLayout>

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <TextView         android:id="@+id/tv_intro_name"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerVertical="true"        android:text="aaaa"        android:textSize="20sp"        android:layout_marginLeft="10dp"        />    <CheckBox         android:id="@+id/cb_intro_check"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:clickable="false"        android:focusable="false"        android:focusableInTouchMode="false"        android:layout_centerInParent="true"        />    </RelativeLayout>

ListView的适配器:

package com.baidu.adapter;import java.util.ArrayList;import java.util.HashMap;import com.baidu.checkbox_all.R;import android.annotation.SuppressLint;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.CheckBox;import android.widget.TextView;public class CheckAdapter extends BaseAdapter{private Context context;private ArrayList<String> list;private static HashMap<Integer, Boolean> isSelected;@SuppressLint("UseSparseArrays")public CheckAdapter(Context context, ArrayList<String> list) {super();this.context = context;this.list = list;isSelected=new HashMap<Integer, Boolean>();//初始化数据initData();}private void initData() {for(int i=0;i<list.size();i++){getIsSelected().put(i,false);}}@Overridepublic int getCount() {return list.size();}@Overridepublic Object getItem(int position) {return list.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ViewHolder vh=null;if(convertView==null){vh=new ViewHolder();convertView=LayoutInflater.from(context).inflate(R.layout.listview_intro, null);vh.tv_intro_name=(TextView) convertView.findViewById(R.id.tv_intro_name);vh.cb_intro_check=(CheckBox) convertView.findViewById(R.id.cb_intro_check);convertView.setTag(vh);}else{vh=(ViewHolder) convertView.getTag();}vh.tv_intro_name.setText(list.get(position));vh.cb_intro_check.setChecked(getIsSelected().get(position));return convertView;}public static HashMap<Integer, Boolean> getIsSelected(){return isSelected;}public static void isSelected(HashMap<Integer, Boolean> isSelected){CheckAdapter.isSelected=isSelected;}public static class ViewHolder{public TextView tv_intro_name;public CheckBox cb_intro_check;}}
在Activity中的代码:

package com.baidu.checkbox_all;import java.util.ArrayList;import com.baidu.adapter.CheckAdapter;import com.baidu.adapter.CheckAdapter.ViewHolder;import android.os.Bundle;import android.app.Activity;import android.view.View;import android.view.View.OnClickListener;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.Button;import android.widget.ListView;import android.widget.TextView;public class MainActivity extends Activity implements OnClickListener {private TextView tv_check;private Button bt_all_select;private Button bt_invert_select;private Button bt_cancel_select;private ListView lv_listview;private CheckAdapter adapter;private int checkNum;private ArrayList<String> list=new ArrayList<String>();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);init();}private void init() {/* * 实例控件对象 */tv_check=(TextView)findViewById(R.id.tv_check);bt_all_select=(Button)findViewById(R.id.bt_all_select);bt_invert_select=(Button)findViewById(R.id.bt_invert_select);bt_cancel_select=(Button)findViewById(R.id.bt_cancel_select);lv_listview=(ListView)findViewById(R.id.lv_listview);//准备数据initData();//适配器adapter=new CheckAdapter(this, list);lv_listview.setAdapter(adapter);//按钮的监听bt_all_select.setOnClickListener(this);bt_invert_select.setOnClickListener(this);bt_cancel_select.setOnClickListener(this);//listview的监听器lv_listview.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1, int position,long id) {//取得viewholder对象ViewHolder vh=(ViewHolder) arg1.getTag();//改变checkbox的状态vh.cb_intro_check.toggle();//将CheckBox的选中状况记录下来  CheckAdapter.getIsSelected().put(position, vh.cb_intro_check.isChecked());//调整选中的项目if(vh.cb_intro_check.isChecked()==true){checkNum++;}else{checkNum--;}//显示选中的条数tv_check.setText("已选中"+checkNum+"项!");}});}private void initData() {for(int i=0;i<20;i++){list.add("商品"+""+i);}}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.bt_all_select://遍历listview的长度,将map中的值设为truefor(int i=0;i<list.size();i++){CheckAdapter.getIsSelected().put(i, true);}//数量设为list的长度checkNum=list.size();//刷新listview和textview的显示dataChanged();break;case R.id.bt_invert_select://遍历list的长度,将已选的设为未选,未选的设为已选  for(int i=0;i<list.size();i++){if(CheckAdapter.getIsSelected().get(i)){CheckAdapter.getIsSelected().put(i, false);checkNum--;}else{CheckAdapter.getIsSelected().put(i, true);checkNum++;}}//刷新listview和textview的显示dataChanged();break;case R.id.bt_cancel_select://遍历list的长度,将已选的按钮设为未选  for(int i=0;i<list.size();i++){if(CheckAdapter.getIsSelected().get(i)){CheckAdapter.getIsSelected().put(i, false);checkNum--;}}//刷新listview和textview的显示dataChanged();break;default:break;}}//刷新listview和textview的显示private void dataChanged(){//通知listview刷新adapter.notifyDataSetChanged();//textview显示最新的选中项目tv_check.setText("已选中"+checkNum+"项!");}}




0 0