Android里ListView里面添加CheckBox实现多选。

时间:2022-05-03 09:20:40

1. 我在网上找了很多关于这个方面的文章,因为现在Android里的ListView基本上都用到了ViewHolder,用到了convertView。

2. 网上有的文章也有用到convertView的,只是数据多了CheckBox的选择基本上是出错的了。

3.我想过用Adapter里面用Map<>来保存CheckBox是否选择。可是太麻烦了。我

4. 现在我用我的方法来做一个这样的。数据和界面分离开,这才是最好的。

listView的item的xml文件, item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <ImageView  android:id="@+id/item_icon"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="#000" />
    <LinearLayout  android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_marginLeft="10dp"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical">
        <TextView  android:id="@+id/item_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textSize="20dp" />
        <TextView  android:id="@+id/item_info"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textSize="16dp" />
    </LinearLayout>
    <CheckBox  android:id="@+id/item_cb"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false" />
</LinearLayout>
 
activity_main.xml
 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <ListView  android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
    </ListView>
</LinearLayout>
 
数据源格式实现:Bean.java
public class Bean
{
    private Drawable drawable;
    private String title;
    private String info;
    private boolean isChcked; //多了这个属性来保存CheckBox的选择状态

    public Bean(Drawable drawable, String title, String info, boolean isChcked)
    {
        this.drawable = drawable;
        this.title = title;
        this.info = info;
        this.isChcked = isChcked;
    }

    public void setIsChcked(boolean isChcked)
    {
        this.isChcked = isChcked;
    }

    public boolean getIsChcked()
    {
        return isChcked;
    }

    public String getInfo()
    {
        return info;
    }

    public String getTitle()
    {
        return title;
    }

    public Drawable getDrawable()
    {
        return drawable;
    }
}


MainActivity.java


public class MainActivity extends AppCompatActivity
{
    private ListView lv;

    private BeanAdapter mAdapter;
    private List<Bean> mList;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initData();

        lv = (ListView)findViewById(R.id.lv);
        mAdapter = new BeanAdapter(mList, this);
        lv.setAdapter(mAdapter);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                ViewHolder viewHolder = (ViewHolder)view.getTag();
                if(viewHolder.cb.isChecked())       //true -> false  {
                    viewHolder.cb.setChecked(false);
                    mList.get(position).setIsChcked(false);//改变状态并保存下来
                }
                else  {
                    viewHolder.cb.setChecked(true);
                    mList.get(position).setIsChcked(true);
                }
            }
        });
    }

    private void initData()
    {
        mList = new ArrayList<Bean>();
        Bean bean;
        for(int i = 0; i < 70; i++)
        {
            bean = new Bean(getDrawable(R.drawable.float1), "title" + i, "info" + i, false);
            mList.add(bean);
        }
    }
}
 源码地址:http://download.csdn.net/detail/u013792369/9091055