Android:实现带图片和CheckBox的ListView

openkk 12年前

实现在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)上结果图:

Android:实现带图片和CheckBox的ListView

转自:http://blog.csdn.net/linjingj/article/details/7815986