第一次写博客,不足的地方多多提出,会努力改进,谢谢!(ps:图片上传了不知道怎么看不见。。。)
1.布局文件activity_main.xml
android:layout_height="fill_parent"
android:orientation="vertical" >
android:layout_width="fill_parent"
android:layout_height="400dp"
android:cacheColorHint="#00000000"
android:clickable="true"
android:descendantFocusability="blocksDescendants"
android:divider="#f80"
android:dividerHeight="1.0dip"
android:fadingEdge="none"
android:fastScrollEnabled="true"
android:scrollingCache="false" />
2. listviewitem.xml
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="horizontal" >
android:layout_width="50dp"
android:layout_height="wrap_content"
android:textSize="16sp" />
android:layout_width="210dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textSize="14sp" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dp"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false" />
3.MainActivity.java
private ListView listView;//listView 控件
private Map
private List beSelectedData = new ArrayList(); //存放选中checkbox的集合
ListAdapter adapter;//适配器
private List cs = null;//显示listview数据
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) this.findViewById(R.id.lv_single_list);
cs = new ArrayList();
cs.add("aaaaaa");
cs.add("bbbbbb");
cs.add("cccccc");
cs.add("dddddd");
cs.add("eeeeee");
cs.add("ffffff");
initList();
}
void initList(){
if (cs == null || cs.size() == 0)
return;
if (isSelected != null)
isSelected = null;
isSelected = new HashMap
for (int i = 0; i < cs.size(); i++) {
isSelected.put(i, false);
}
// 清除已经选择的项
if (beSelectedData.size() > 0) {
beSelectedData.clear();
}
adapter = new ListAdapter(this, cs);
listView.setAdapter(adapter);
/**
* The list allows up to one choicelistview 选中样式
*/
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
adapter.notifyDataSetChanged();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView> parent, View view,
int position, long id) {
Log.i("map", cs.get(position).toString());
}
});
}
//适配器
class ListAdapter extends BaseAdapter {
private Context context;
private List cs;
private LayoutInflater inflater;//布局填充器
public ListAdapter(Context context, List data) {
this.context = context;
this.cs = data;
initLayoutInflater();
}
void initLayoutInflater() {
inflater = LayoutInflater.from(context);
}
public int getCount() {
return cs.size();
}
public Object getItem(int position) {
return cs.get(position);
}
public long getItemId(int position) {
return 0;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.listviewitem,null);
holder = new ViewHolder();
holder.checkBox = (CheckBox) convertView
.findViewById(R.id.item_cb_section);
holder.tv_sequence = (TextView) convertView
.findViewById(R.id.tv_zxing_section_sequence);
holder.tv_sectionname = (TextView) convertView
.findViewById(R.id.tv_zxing_sectionname);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.checkBox.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// 当前点击的CheckBox
boolean cu = !isSelected.get(position);
// 先将所有的CheckBox置为FALSE
for(Integer p : isSelected.keySet()) {
isSelected.put(p, false);
}
// 再将当前选择CB的实际状态
isSelected.put(position, cu);
//通知改变
ListAdapter.this.notifyDataSetChanged();
beSelectedData.clear();
if(cu)
beSelectedData.add(cs.get(position));
}
});
//控件内容
holder.tv_sequence.setText(String.valueOf(position + 1));
holder.tv_sectionname.setText(cs.get(position).toString());
holder.checkBox.setChecked(isSelected.get(position));
return convertView;
}
}
/**
* 就是一个持有者的类,他里面一般没有方法,只有属性,作用就是一个临时的储存器,
* 把你getView方法中每次返回的View存起来,可以下次再用。
* 这样做的好处就是不必每次都到布局文件中去拿到你的View,提高了效率
* @author Administrator
*
*/
class ViewHolder {
CheckBox checkBox;
TextView tv_sequence;
TextView tv_sectionname;
}
源码地址:http://download.csdn.net/detail/u010904027/9140483