ListView中的CheckedTextView 多选
2012-06-26 15:28:02| 分类: android 代码片段 | 标签:checkedtextview |举报|字号 订阅
实现ListView中多选按纽选中或取消 有很多种方式 本实例根据查看ApiDemos 通过自定义Adaper实现的。
如图:
![ListView中的CheckedTextView 多选 - stay_rui - 嘟嘟](http://img9.ph.126.net/Lh9huKRwJ9RLGSRDq6Ln6g==/1090997009748001979.jpg)
<?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:src="@drawable/ic_launcher" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <CheckedTextView android:id="@+id/checktv_title" android:layout_width="match_parent" android:layout_height="?android:attr/listPreferredItemHeight" android:textAppearance="?android:attr/textAppearanceLarge" android:gravity="center_vertical" android:checkMark="?android:attr/listChoiceIndicatorMultiple" android:paddingLeft="6dip" android:paddingRight="6dip" /> <!-- 单选 android:checkMark="?android:attr/listChoiceIndicatorSingle" --> </LinearLayout>
2. Activity中的实现
package com.test.activity;
import java.util.ArrayList; import java.util.HashMap;
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.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); } }); } //构造数据 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); convertView.setTag(holder); }else{ holder = (ViewHolder) convertView.getTag(); } 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; } }
}