ListView批量删除

这篇文章是一个listview批量删除功能的一个demo,可以根据自己的需求进行拓展。

源码地址:https://github.com/lerendan/ListViewBatchDelete

效果图

ListView批量删除_第1张图片

1、bean
在bean中加入一个isChecked,表示是否被选中(点击),默认为false

public class Check {
    private String id;  //ID
    private String name;   //姓名
    private boolean isChecked;  //选中状态

    public boolean isChecked() {
        return isChecked;
    }
    public void setChecked(boolean checked) {
        isChecked = checked;
    }
    public Check(String id, String name) {
        this.id = id;
        this.name = name;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getId() {
        return id;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
}

2、listview的adapter
在getview中判断bean中的isChecked的值,若为true则显示一个小图片表示被选中了

public class Check_Adapter extends BaseAdapter {
    private List mData;
    private LayoutInflater mInflater;

    public Check_Adapter(Context context, List data) {
        mInflater = LayoutInflater.from(context);
        this.mData = data;
    }

    public void setChecks(List data) {
        this.mData = data;
    }

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.lv_check_item, null);
            viewHolder.iv_click = (ImageView) convertView.findViewById(R.id.iv_click);
            viewHolder.tv_id = (TextView) convertView.findViewById(R.id.tv_id);
            viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (mData.get(position).isChecked()) {
            viewHolder.iv_click.setVisibility(View.VISIBLE);
        }else {
            viewHolder.iv_click.setVisibility(View.INVISIBLE);
        }
        viewHolder.tv_id.setText(mData.get(position).getId());
        viewHolder.tv_name.setText(mData.get(position).getName());

        return convertView;
    }

    public class ViewHolder {
        public TextView tv_id, tv_name;
        public ImageView iv_click;
    }
}

3、Activity or Fragment
批量删除的原理就是判断bean中的isCheck的值,为true就删除

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private List mData;
    private Button btn_confirm;
    private ListView mListview;
    private Check_Adapter mAdapter;
    private int n = 0;

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

        initView();
        initData();
        setListener();
    }

    private void initView() {
        mListview = (ListView)findViewById(R.id.lv);
        btn_confirm = (Button)findViewById(R.id.confirm);
    }
    private void initData() {
        if (mAdapter == null) {
            mData = getCheckData();
            mAdapter = new Check_Adapter(MainActivity.this, mData);
            mListview.setAdapter(mAdapter);
        } else {
            mAdapter.setChecks(mData);
            mAdapter.notifyDataSetChanged();
        }

    }
    private List getCheckData() {
        List data = new ArrayList();
        Check check1 = new Check("id111111111111", "张三 11111111");
        Check check2 = new Check("id222222222222", "但超 22222222");
        Check check3 = new Check("id333333333333","李四 33333333");
        Check check4 = new Check("id444444444444","王五 44444444");
        Check check5 = new Check("id555555555555", "赵六 55555555");
        Check check6 = new Check("id666666666666", "张七 66666666");
        Check check7 = new Check("id777777777777","吴八 77777777");
        Check check8 = new Check("id888888888888", "关关 88888888");
        Check check9 = new Check("id999999999999", "笑笑 99999999");
        data.add(check1);
        data.add(check2);
        data.add(check3);
        data.add(check4);
        data.add(check5);
        data.add(check6);
        data.add(check7);
        data.add(check8);
        data.add(check9);
        return data;
    }
    private void setListener() {
        mListview.setOnItemClickListener(new MyOnItemClickListner());
        btn_confirm.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.confirm:
                if (n != 0) {
                            deleteCheckedItem(); //验证完成后删除选中的item
                } else {
                    Toast.makeText(this, "未点击", Toast.LENGTH_SHORT).show();
                }

                break;
            default:
                break;
        }

    }

    /**
     * 删除选中的Item
     */
    public void deleteCheckedItem() {
        Iterator iterator = mData.iterator();
        while (iterator.hasNext()) {
            Check check = iterator.next();
            if (check.isChecked()) {
                iterator.remove();
            }
        }
        mAdapter.setChecks(mData);
        mAdapter.notifyDataSetChanged();
        mListview.setSelection(0);
        n = 0;
        btn_confirm.setText("确认(" + n + ")");
    }

    //Listview Item的点击事件
    private class MyOnItemClickListner implements AdapterView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView parent, View view, int position, long id) {
            Check check = mData.get(position);
            if (check.isChecked()) {
                check.setChecked(false);
                n--;
                btn_confirm.setText("确认(" + n + ")");
            } else {
                check.setChecked(true);
                n++;
                btn_confirm.setText("确认(" + n + ")");
            }
            mAdapter.setChecks(mData);
            mAdapter.notifyDataSetChanged();
        }
    }
}

拓展:这是一个基本的listview批量删除功能,可以根据自己的需求做拓展。比如,在menu里加一个全选的功能;把标识item被选中的imageview换成checkbox;加入删除的动画。

你可能感兴趣的:(ListView批量删除)