实现ListView控件的多选和全选功能
主程序代码 MainActivity.java
- package yy.test;
- import java.util.ArrayList;
- import android.app.Activity;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.SubMenu;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.CheckBox;
- import android.widget.ListAdapter;
- import android.widget.ListView;
- import android.widget.TextView;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- private ListView listView;
- private MyAdapter adapter;
- private ArrayList<String> items; //模拟存储信息的集合
- private ArrayList<String> checked; //该集合存储被选中的列表项中的TextView中所显示的字符串
- private boolean isMultiple = false; // 记录是否是多选状态,true为是,false不是
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- listView = (ListView) findViewById(R.id.listview);
- items = new ArrayList<String>();
- items.add("00000");
- items.add("11111");
- items.add("22222");
- items.add("33333");
- items.add("44444");
- items.add("55555");
- items.add("66666");
- items.add("77777");
- items.add("88888");
- items.add("99999");
- items.add("aaaaa");
- items.add("bbbbb");
- items.add("ccccc");
- items.add("ddddd");
- adapter = new MyAdapter(items,this); //new出自定义的MyAdapter对象
- listView.setAdapter(adapter);
- listView.setOnItemClickListener(new OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> arg0, View view, int position,
- long arg3) {
- if(isMultiple){
- CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkbox);
- TextView textView = (TextView) view.findViewById(R.id.text);
- if(checkBox.isChecked()){
- checkBox.setChecked(false);
- adapter.checkedMap.put(position, false);
- textView.setTextColor(Color.WHITE);
- adapter.colorMap.put(position, Color.WHITE);
- int index = checked.indexOf(textView.getText()+"");
- checked.remove(index);
- }else{
- checkBox.setChecked(true);
- adapter.checkedMap.put(position, true);
- textView.setTextColor(Color.RED);
- adapter.colorMap.put(position, Color.RED);
- checked.add(textView.getText()+"");
- }
- }
- }
- });
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- SubMenu subMenu = menu.addSubMenu("操作");
- subMenu.add(0, 1, 0, "多选");
- subMenu.add(0, 2, 0, "删除");
- subMenu.add(0, 3, 0, "全选");
- subMenu.add(0, 4, 0, "取消全选");
- return super.onCreateOptionsMenu(menu);
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- if(item.getItemId()==1){
- isMultiple = true;
- checked = new ArrayList<String>();
- int index = adapter.getCount();
- for(int i=0;i<index;i++){
- adapter.visibleMap.put(i, CheckBox.VISIBLE);
- }
- adapter.notifyDataSetChanged();
- }else if(item.getItemId()==2){
- for(String text : checked){
- int index = items.indexOf(text);
- items.remove(index);
- }
- isMultiple = false;
- adapter = new MyAdapter(items,MainActivity.this);
- listView.setAdapter(adapter);
- }else if(item.getItemId()==3){
- isMultiple = true;
- checked = new ArrayList<String>();
- int index = adapter.getCount();
- for(int i=0;i<index;i++){
- adapter.checkedMap.put(i, true);
- adapter.colorMap.put(i, Color.RED);
- adapter.visibleMap.put(i, CheckBox.VISIBLE);
- View view1 = adapter.getView(i, null, null);
- TextView textView = (TextView) view1.findViewById(R.id.text);
- textView.setTextColor(Color.RED);
- /*CheckBox checkBox = (CheckBox) view1.findViewById(R.id.checkbox);
- checkBox.setVisibility(CheckBox.VISIBLE);*/
- adapter.notifyDataSetChanged();
- checked.add(textView.getText()+"");
- }
- }else if(item.getItemId()==4){
- isMultiple = false;
- checked = null;
- int index = adapter.getCount();
- for(int i=0;i<index;i++){
- adapter.checkedMap.put(i, false);
- adapter.colorMap.put(i, Color.WHITE);
- adapter.visibleMap.put(i, CheckBox.INVISIBLE);
- /*View view1 = adapter.getView(i, null, null);
- TextView textView = (TextView) view1.findViewById(R.id.text);
- textView.setTextColor(Color.WHITE);
- CheckBox checkBox = (CheckBox) view1.findViewById(R.id.checkbox);
- checkBox.setVisibility(CheckBox.INVISIBLE);*/
- adapter.notifyDataSetChanged();
- }
- }
- return super.onOptionsItemSelected(item);
- }
- }
- 继承自BaseAdapter的MyAdapter
- <pre class="java" name="code">package yy.test;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Map;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Color;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.CheckBox;
- import android.widget.ImageView;
- import android.widget.TextView;
- public class MyAdapter extends BaseAdapter {
- private LayoutInflater inflater;
- private ArrayList<String> items;
- private Bitmap icon;
- private Context context;
- public Map<Integer,Boolean> checkedMap; //保存checkbox是否被选中的状态
- public Map<Integer,Integer> colorMap; //保存textview中文字的状态
- public Map<Integer,Integer> visibleMap; //保存checkbox是否显示的状态
- public MyAdapter(ArrayList<String> items, Context context) {
- super();
- this.items = items;
- this.context = context;
- inflater = LayoutInflater.from(context);
- icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon);
- checkedMap = new HashMap<Integer, Boolean>();
- colorMap = new HashMap<Integer, Integer>();
- visibleMap = new HashMap<Integer, Integer>();
- for(int i=0;i<items.size();i++){
- checkedMap.put(i, false);
- colorMap.put(i, Color.WHITE);
- visibleMap.put(i, CheckBox.INVISIBLE);
- }
- }
- @Override
- public int getCount() {
- // TODO Auto-generated method stub
- return items.size();
- }
- @Override
- public Object getItem(int arg0) {
- // TODO Auto-generated method stub
- return arg0;
- }
- @Override
- public long getItemId(int arg0) {
- // TODO Auto-generated method stub
- return arg0;
- }
- @Override
- public View getView(int position, View view, ViewGroup arg2) {
- view = inflater.inflate(R.layout.file_row, null);
- ImageView image = (ImageView) view.findViewById(R.id.icon);
- TextView text = (TextView) view.findViewById(R.id.text);
- CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkbox);
- checkBox.setVisibility(visibleMap.get(position));
- checkBox.setChecked(checkedMap.get(position));
- image.setImageBitmap(icon);
- text.setText(items.get(position));
- text.setTextColor(colorMap.get(position));
- return view;
- }
- }
- </pre><br>
- <pre></pre>
- <pre class="java" name="code">主界面的布局文件</pre><pre class="java" name="code"><?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <ListView
- android:id="@+id/listview"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- </LinearLayout></pre><pre class="java" name="code"> </pre>
- <p>MyAdapter中所使用的布局文件</p>
- <pre class="java" name="code"><?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_gravity="center_vertical"
- >
- <ImageView
- android:id="@+id/icon"
- android:layout_width="30dp"
- android:layout_height="30dp"
- android:layout_gravity="center_vertical"
- />
- <TextView
- android:id="@+id/text"
- android:layout_width="80dp"
- android:layout_height="wrap_content"
- android:layout_gravity="center_vertical"
- android:paddingLeft="5dp"
- />
- <CheckBox
- android:id="@+id/checkbox"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:focusable="false"
- android:clickable="false"
- android:focusableInTouchMode="false"
- />
- </LinearLayout>
- </pre>
- <p> </p>
- <p>下面来分析为什么要用map来保存checkbox以及textview的状态。<br>
- <br>
- 这个与ListView的刷新机制有关,当你的listview对象很多的时候,每次你拖动listview上下滚动,listview都会刷新一次。怎么刷新呢?比如一个屏幕它最多只显示七条listview,如果你有十条数据,当你想看第八条时,第一条数据理所当然的要被隐藏掉,而第八条数据会被显示,这时listview就刷新了。如果你不保存你所选的checkbox的状态,这时如果你选的是第一条的checkbox的状态为true,当你把余下的第八、第九、第十条数据显示出来时,第十条的checkbox的状态会显示为true,但是它的状态没有被保存,只是你看到它是被选中了而已,其实你选的还是第一条数据。这个问题很操蛋。还有一个更离奇的状态,你让checkbox的状态为true,数据一定要大于十条,你不停的上下拖动屏幕,你会看见checkbox的显示状态会乱跳,但是你实际上选择的还是第一条数据,只是会让你的用户感觉很不爽罢了。<span style="font-family:Courier New; background-color:#f0f0f0"></span></p>
- <pre class="java" name="code"></pre><p> </p>