实现ListView中多选按纽选中或取消 有很多种方式 本实例根据查看ApiDemos 通过自定义Adaper实现的。
如图:

Item是由两个控件组成的。
代码片段:
1. item的布局:list_checktextview.xml
<p><span style="font-family: 'comic sans ms';"> <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="</span><a target=_blank href="http://schemas.android.com/apk/res/android" rel="nofollow" style="color: rgb(51, 102, 153); text-decoration: none;"><span style="font-family: 'comic sans ms'; color: rgb(0, 0, 0);">http://schemas.android.com/apk/res/android</span></a><span style="font-family: 'comic sans ms';">" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal"></span></p><p><span style="font-family: 'comic sans ms';"> <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></span></p>
2. Activity中的实现
<p><span style="font-family: 'comic sans ms'; color: rgb(0, 0, 0);">package com.test.activity;</span></p><p><span style="font-family: 'comic sans ms'; color: rgb(0, 0, 0);">import java.util.ArrayList;
import java.util.HashMap;</span></p><p><span style="font-family: 'comic sans ms'; color: rgb(0, 0, 0);">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;</span></p><p><span style="font-family: 'comic sans ms'; color: rgb(0, 0, 0);">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();
}</span></p><p><span style="font-family: 'comic sans ms'; color: rgb(0, 0, 0);"> @Override
public Object getItem(int position) {
return position;
}</span></p><p><span style="font-family: 'comic sans ms'; color: rgb(0, 0, 0);"> @Override
public long getItemId(int position) {
return position;
}</span></p><p><span style="font-family: 'comic sans ms'; color: rgb(0, 0, 0);"> @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;
}
}</span></p><p><span style="font-family: 'comic sans ms'; color: rgb(0, 0, 0);">} </span></p><div><span style="font-family: 'comic sans ms'; color: rgb(0, 0, 0);">
</span></div>
本文详细解析了如何使用自定义Adapter在ListView中实现多选功能,包括布局设计、数据加载、监听事件等关键步骤,并对比了两种不同实现方式的优缺点。
1110

被折叠的 条评论
为什么被折叠?



