ListView多项选择功能的实现

在开发ListView时候,很多情况下,我们都会对item进行批量操作,那么怎样实现这样的功能呢?其实很简单,我就把我的思路分享给大家吧,如果有错的话,还请大家指出,谢谢大家。

具体思路有三步:

1、编辑一个ItemBean来保存item数据和item中CheckBox的显示状态。
2、重写BaseAdapter,根据传入的List 显示Item,并写一个内部类ViewHolder来关联item的View,便于结合ListView的双缓冲机制,提高效率。
3、设置item的单击和长按的响应事件(改变每个ItemBean的相关数据和状态),并更新ListView。

来两张程序图:
1、刚开始进入程序:

ListView多项选择功能的实现_第1张图片

2、长按item时候响应的图片:

ListView多项选择功能的实现_第2张图片

好了,接下来我们上代码:

一、ItemBean类:

package com.tielizi.listviewcheckbox;

/**
 * Created by Administrator on 2015/10/20.
 */
public class ItemBean {
    private int pictureId;
    private String grade;
    private String name;
    private boolean isSelect;
    private boolean isShowCheckBox;

    public boolean isShowCheckBox() {
        return isShowCheckBox;
    }

    public void setIsShowCheckBox(boolean isShowCheckBox) {
        this.isShowCheckBox = isShowCheckBox;
    }

    public ItemBean(int pictureId, String grade, String name, boolean isSelect,boolean isShowCheckBox) {
        this.pictureId = pictureId;
        this.grade = grade;
        this.name = name;
        this.isSelect = isSelect;
    }

    public int getPictureId() {
        return pictureId;
    }

    public void setPictureId(int pictureId) {
        this.pictureId = pictureId;
    }

    public String getGrade() {
        return grade;
    }

    public void setGrade(String grade) {
        this.grade = grade;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }



    public boolean isSelect() {
        return isSelect;
    }

    public void setIsSelect(boolean isSelect) {
        this.isSelect = isSelect;
    }
}

二、ListViewWithCheckBoxAdapter类:

package com.tielizi.listviewcheckbox;

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;

import java.util.List;

/**
 * Created by Administrator on 2015/10/20.
 */
public class ListViewWithCheckBoxAdapter extends BaseAdapter {

    private List list;
    private LayoutInflater layoutInflater;

    public ListViewWithCheckBoxAdapter(Context context,List list){
        this.list = list;
        layoutInflater = LayoutInflater.from(context);

    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if(convertView == null){
            convertView = layoutInflater.inflate(R.layout.item_layout,null);

            ViewHolder viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }

        ViewHolder viewHolder = (ViewHolder) convertView.getTag();
        viewHolder.imageView.setBackgroundResource(list.get(position).getPictureId());
        viewHolder.grade.setText(list.get(position).getGrade());
        viewHolder.name.setText(list.get(position).getName());
        if(list.get(position).isShowCheckBox()){
            viewHolder.checkBox.setVisibility(View.VISIBLE);
            if(list.get(position).isSelect()){
                viewHolder.checkBox.setChecked(true);
            }else{
                viewHolder.checkBox.setChecked(false);
            }
        }else{
            viewHolder.checkBox.setVisibility(View.INVISIBLE);
        }

        return convertView;
    }

    public class ViewHolder {
        public final ImageView imageView;
        public final TextView grade;
        public final TextView name;
        public final CheckBox checkBox;
        public final View root;

        public ViewHolder(View root) {
            imageView = (ImageView) root.findViewById(R.id.imageButton);
            grade = (TextView) root.findViewById(R.id.grade);
            name = (TextView) root.findViewById(R.id.name);
            checkBox = (CheckBox) root.findViewById(R.id.checkBox);
            this.root = root;
        }
    }
}

三、MainActivity类:

package com.tielizi.listviewcheckbox;

import android.app.Activity;
import android.content.ClipData;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends Activity implements AdapterView.OnItemClickListener,AdapterView.OnItemLongClickListener,View.OnClickListener{

    private LinearLayout linearLayout;//按钮布局
    private Button sure,cancel;
    private ListView listView;
    private ListViewWithCheckBoxAdapter adapter;
    private List list;
    private boolean isLineaLayoutVisible = false;//标记按钮布局的显示

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

        linearLayout = (LinearLayout) findViewById(R.id.linearlayout);
        sure = (Button) findViewById(R.id.sure);
        cancel = (Button) findViewById(R.id.cancel);
        sure.setOnClickListener(this);
        cancel.setOnClickListener(this);
        listView = (ListView) findViewById(R.id.listView);
        list = new ArrayList();

        list.add(new ItemBean(R.mipmap.head1,"2013","彭大爷",false,false));
        list.add(new ItemBean(R.mipmap.ic_launcher, "2012", "李大爷", false, false));
        list.add(new ItemBean(R.mipmap.head1, "2011", "哈哈", false, false));
        list.add(new ItemBean(R.mipmap.ic_launcher,"2014","呵呵",false,false));
        list.add(new ItemBean(R.mipmap.head1,"2015","叶良辰",false,false));
        list.add(new ItemBean(R.mipmap.head1,"2013","彭大爷",false,false));
        list.add(new ItemBean(R.mipmap.ic_launcher,"2012","李大爷",false,false));
        list.add(new ItemBean(R.mipmap.head1,"2011","哈哈",false,false));
        list.add(new ItemBean(R.mipmap.ic_launcher,"2014","呵呵",false,false));
        list.add(new ItemBean(R.mipmap.head1,"2015","叶良辰",false,false));
        list.add(new ItemBean(R.mipmap.head1,"2013","彭大爷",false,false));
        list.add(new ItemBean(R.mipmap.ic_launcher,"2012","李大爷",false,false));
        list.add(new ItemBean(R.mipmap.head1,"2011","哈哈",false,false));
        list.add(new ItemBean(R.mipmap.ic_launcher,"2014","呵呵",false,false));
        list.add(new ItemBean(R.mipmap.head1,"2015","叶良辰",false,false));
        adapter = new ListViewWithCheckBoxAdapter(this,list);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
        listView.setOnItemLongClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {
        ListViewWithCheckBoxAdapter.ViewHolder viewHolder = (ListViewWithCheckBoxAdapter.ViewHolder) view.getTag();
        if(isLineaLayoutVisible){//当按钮布局显示时候才有权多项选择
            list.get(position).setIsSelect(!list.get(position).isSelect());//向表中记录被选择的item
            adapter.notifyDataSetChanged();//更新ListView
        }
        else{
            Toast.makeText(this,list.get(position).getGrade()+"级"+list.get(position).getName(),Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public boolean onItemLongClick(AdapterView parent, View view, int position, long id) {

        list.get(position).setIsSelect(true);//记录选择的item
        for(ItemBean itemBean : list){
            itemBean.setIsShowCheckBox(true);//将所有的Item的CheckBox设置为选择状态
        }
        adapter.notifyDataSetChanged();
        linearLayout.setVisibility(View.VISIBLE);//长按item设置按钮布局为显示状态
        isLineaLayoutVisible = true;
        return true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.sure:
                String str = "";
                for(ItemBean itemBean : list){
                    if(itemBean.isSelect()){
                        str+=itemBean.getGrade()+"级"+itemBean.getName()+"\n";
                    }
                }
                if(str.equals("")){
                    Toast.makeText(this,"您没有选择",Toast.LENGTH_SHORT).show();

                }else{
                    Toast.makeText(this,str,Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.cancel:
                if(cancel.getText().equals("选择全部")){
                    for(ItemBean itemBean : list){
                        itemBean.setIsSelect(true);
                    }
                    cancel.setText("取消全部");
                }else{
                    for(ItemBean itemBean : list){
                        itemBean.setIsSelect(false);
                    }
                    cancel.setText("选择全部");
                }


                adapter.notifyDataSetChanged();
                break;

        }

    }

    @Override
    public void onBackPressed() {
        if (isLineaLayoutVisible){
            linearLayout.setVisibility(View.INVISIBLE);
            isLineaLayoutVisible = false;
            for(ItemBean itemBean : list){//按返回键时取消所有选择记录,同是吧按钮布局设置为不可见
                itemBean.setIsShowCheckBox(false);
                itemBean.setIsSelect(false);
            }
            adapter.notifyDataSetChanged();
        }else {
            super.onBackPressed();
        }
    }
}

好,就这些了。

源码下载

你可能感兴趣的:(android)