android使用CheckedTextView搭配listview完成选择列表
列表list_checktextview.xml
<?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="fill_parent" android:orientation="horizontal" > <ImageView android:id="@+id/dianjiceshi" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <CheckedTextView android:id="@+id/checktv_title" android:layout_width="match_parent" android:layout_height="?android:attr/listPreferredItemHeight" android:checkMark="?android:attr/listChoiceIndicatorMultiple" android:gravity="center_vertical" android:paddingLeft="6dip" android:paddingRight="6dip" android:textAppearance="?android:attr/textAppearanceLarge" /> </LinearLayout>
代码:
package com.example.myapp; import java.util.ArrayList; import java.util.HashMap; import java.util.Map.Entry; import java.util.Set; import android.app.ListActivity; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.CheckedTextView; import android.widget.ImageView; import android.widget.ListView; public class ListCheckedTextView extends ListActivity { private ArrayList<HashMap<String, String>> arrlist = new ArrayList<HashMap<String, String>>(); private ListView listView; private HashMap<Integer, Boolean> checkedMap = new HashMap<Integer, Boolean>(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getdata(); /* * 设置ListView的适配器 有两种方式加载含CheckedTextView的多选模式并响应事件 1. 但是这种方式,选择多选响应事件时 * 各个Item会出现乱弹的现象,不知道为什么 希望各位大虾帮帮忙 //适配器设置 setListAdapter(new * SimpleAdapter(this, arrlist, R.layout.list_checktextview, new * String[]{"title"}, new int[]{R.id.checktv_title})); //listview的监听事件 * listView.setOnItemClickListener(new OnItemClickListener() { * * @Override public void onItemClick(AdapterView<?> parent, View view, * int position, long id) { checktv = (CheckedTextView) * parent.getChildAt(position).findViewById(R.id.checktv_title); * if(checktv.isChecked()){ checktv.setChecked(false); }else{ * checktv.setChecked(true); } } }); 2. 通过自定义Adapter实现 如下, */ setListAdapter(new MyAdapter(this, arrlist)); listView = getListView(); listView.setItemsCanFocus(false); listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); // listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);//单选 listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { /* * 对于由position指定的项目,返回其是否被选中。 * 只有当选择模式已被设置为CHOICE_MODE_SINGLE或CHOICE_MODE_MULTIPLE时 ,结果才有效。 */ boolean checked = getListView().isItemChecked(position); checkedMap.put(position, checked); getListView().setItemChecked(position, checked); } }); } // 构造数据 private ArrayList<HashMap<String, String>> getdata() { HashMap<String, String> map = new HashMap<String, String>(); map.put("title", "aaaa"); arrlist.add(map); map = new HashMap<String, String>(); map.put("title", "bbbb"); arrlist.add(map); map = new HashMap<String, String>(); map.put("title", "cccc"); arrlist.add(map); return arrlist; } // 自定义Adapter,从checkMap中获取当前Item的状态 并设置 private class MyAdapter extends BaseAdapter { private LayoutInflater inflater; ArrayList<HashMap<String, String>> datalist; public MyAdapter(Context context, ArrayList<HashMap<String, String>> datalist) { super(); inflater = LayoutInflater.from(context); this.datalist = datalist; } @Override public int getCount() { return datalist.size(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { convertView = inflater.inflate(R.layout.list_checktextview, null); holder = new ViewHolder(); holder.checktv_title = (CheckedTextView) convertView .findViewById(R.id.checktv_title); holder.imageView=(ImageView)convertView.findViewById(R.id.dianjiceshi); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.imageView.setOnClickListener(new myClickimg()); holder.checktv_title.setText(datalist.get(position).get("title") .toString()); // 根据checkMap中position的状态设置是否被选中 if (checkedMap.get(position) != null && checkedMap.get(position) == true) { holder.checktv_title.setChecked(true); } else { holder.checktv_title.setChecked(false); } return convertView; } class ViewHolder { CheckedTextView checktv_title; ImageView imageView; } } class myClickimg implements View.OnClickListener{ @Override public void onClick(View arg0) { // TODO Auto-generated method stub allNo(); } } /* * 全否 * */ public void allNo(){ Set<Entry<Integer, Boolean>> re = checkedMap.entrySet(); for (Entry<Integer, Boolean> entry : re) { Integer mkey = entry.getKey(); //全部设置为false checkedMap.put(mkey, false); setListAdapter(new MyAdapter(getApplicationContext(), arrlist));//更新ui } } }