ListViewのCheckedTextViewマルチ選択/シングル
図:
Itemは2つのコントロールで構成されています.
コードクリップ:
1.itemのレイアウト:list_checktextview.xml
http://schemas.android.com/apk/res/android " android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal">
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
> arrlist = new ArrayList >(); private ListView listView; private HashMap checkedMap = new HashMap (); @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 > getdata(){ HashMap map = new HashMap (); map.put("title", "aaaa"); arrlist.add(map); map = new HashMap (); map.put("title", "bbbb"); arrlist.add(map); map = new HashMap (); map.put("title", "cccc"); arrlist.add(map); return arrlist; } //自定义Adapter,从checkMap中获取当前Item的状态 并设置 private class MyAdapter extends BaseAdapter{ private LayoutInflater inflater ; ArrayList > datalist; public MyAdapter(Context context , ArrayList > 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; } }
}