實現在ListView的每一條List中帶圖片和CheckBox。
(1)第一步,先上布局文件:main.xml和list.xml
main.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="vertical" >
-
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello" />
- <ListView
- android:id="@+id/lv"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"/>
-
- </LinearLayout>
list.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/img"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_vertical"/>
- <TextView
- android:id="@+id/title"
- android:textSize="18dip"
- android:layout_weight="1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
- <CheckBox
- android:id="@+id/cb"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:focusable="false"
- android:focusableInTouchMode="false"
- android:clickable="false"
- android:checkMark="?android:attr/listChoiceIndicatorMultiple"/>
- </LinearLayout>
<1>TextView中android:layout_weight="1"
這一句可以使中間的textview權重增大,從而後面的checkbok可以居右顯示。
<2>下面三句很重要,如果不加就會出現錯誤。
由於checkbox的點擊事件優先級比listview的高,所以要在checkbox中添加android:focusable="false",使得checkbox初始的時候沒有獲取焦點。
- android:focusable="false"
- android:focusableInTouchMode="false"
- android:clickable="false"
(2)為給ListView匹配數據,新建一個MyAdapter類,繼承自BaseAdapter。在MyAdapter中實現對數據的綁定,在這個測試中,我把數據寫死了。
注意getView方法中對數據的處理。
MyAdapter.java
- package com.tgb.ys.test;
-
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
-
- import android.content.Context;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.CheckBox;
- import android.widget.ImageView;
- import android.widget.TextView;
-
- public class MyAdapter extends BaseAdapter {
-
- private LayoutInflater mInflater;
- private List<Map<String, Object>> mData;
- public static Map<Integer, Boolean> isSelected;
-
- public MyAdapter(Context context) {
- mInflater = LayoutInflater.from(context);
- init();
- }
- //初始化
- private void init() {
- mData = new ArrayList<Map<String,Object>>();
- for (int i=0; i<5; i++) {
- Map<String, Object> map = new HashMap<String, Object>();
- map.put("img", R.drawable.leaf);
- map.put("title", "第" + (i+1) + "行的標題");
- mData.add(map);
- }
- //定義isSelected這個map是記錄每個listItem的狀態,初始狀態全部為false
- isSelected = new HashMap<Integer, Boolean>();
- for (int i=0; i<mData.size(); i++) {
- isSelected.put(i, false);
- }
- }
-
- public int getCount() {
- // TODO Auto-generated method stub
- return mData.size();
- }
-
- public Object getItem(int position) {
- // TODO Auto-generated method stub
- return null;
- }
-
- public long getItemId(int position) {
- // TODO Auto-generated method stub
- return 0;
- }
-
- public View getView(int position, View convertView, ViewGroup parent) {
- ViewHolder holder = null;
- //convertView為null的時候初始化convertView
- if (convertView == null) {
- holder = new ViewHolder();
- convertView = mInflater.inflate(R.layout.vlist, null);
- holder.img = (ImageView)convertView.findViewById(R.id.img);
- holder.title = (TextView)convertView.findViewById(R.id.title);
- holder.cBox = (CheckBox)convertView.findViewById(R.id.cb);
- convertView.setTag(holder);
- } else {
- holder = (ViewHolder)convertView.getTag();
- }
- holder.img.setBackgroundResource((Integer)mData.get(position).get("img"));
- holder.title.setText(mData.get(position).get("title").toString());
- holder.cBox.setChecked(isSelected.get(position));
- return convertView;
- }
-
- public final class ViewHolder {
- public ImageView img;
- public TextView title;
- public CheckBox cBox;
- }
- }
(3)在Activity中調用:
TestCheckBoxActivity.java
- package com.tgb.ys.test;
-
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.ListView;
-
- import com.tgb.ys.test.MyAdapter.ViewHolder;
-
- /**
- * 總結:
- * 1、數據可以在Activity中獲取,在初始化Adapter的時候當做參數傳遞過去,這樣就可以是list編程動態的。
- * 2、對MyAdapter中getview的理解不夠,尤其是convertView.setTag(holder),
- * @author Administrator
- */
- public class TestCheckBoxActivity extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- ListView list = (ListView)findViewById(R.id.lv);
- MyAdapter adapter = new MyAdapter(this);
- list.setAdapter(adapter);
- list.setItemsCanFocus(false);
- list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
- list.setOnItemClickListener(new OnItemClickListener() {
-
- public void onItemClick(AdapterView<?> parent, View view, int position,
- long id) {
- // TODO Auto-generated method stub
- ViewHolder vHolder = (ViewHolder)view.getTag();
- //在每次獲取點擊的item時將對應的checkBox狀態改變,同時修改map的值
- vHolder.cBox.toggle();
- MyAdapter.isSelected.put(position, vHolder.cBox.isChecked());
- }
- });
- }
- }
(4)上結果圖: