ListView实现全选,单选删除

本文介绍如何在Android应用中实现ListView的批量删除功能,并加入全选操作。通过自定义Adapter来控制CheckBox的显示与状态,实现多选及全选效果。

android长按ListView显示Checkbox,实现批量删除基础上加了一个全选操作

activity_main.xml 代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="circleimageview.com.listviewdemo.MainActivity">

    <LinearLayout
        android:layout_width="368dp"
        android:layout_height="495dp"
        android:orientation="vertical"
        tools:layout_editor_absoluteY="8dp"
        tools:layout_editor_absoluteX="8dp">
        <Button
            android:id="@+id/btn_delete_item"
            android:layout_width="match_parent"
            android:layout_height="88px"
            android:text="删除选项"/>

        <ListView
            android:id="@+id/lv"
            android:layout_width="wrap_content"
            android:layout_height="0px"
            android:layout_weight="1"/>
        <LinearLayout
            android:id="@+id/ll"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="gone">
            <CheckBox
                android:id="@+id/cb_cheak_all"
                android:layout_width="0dp"
                android:layout_height="88px"
                android:layout_weight="1"
                android:text="全选"/>
            <Button
                android:id="@+id/btn_delete"
                android:layout_width="0dp"
                android:layout_height="88px"
                android:layout_weight="1"
                android:text="删除"/>
            <TextView
                android:id="@+id/tv_count"
                android:layout_width="0px"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:textColor="#000000"
                android:textSize="40px"
                />
        </LinearLayout>
    </LinearLayout>

</android.support.constraint.ConstraintLayout>

listview的item代码

<?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="88px"
    android:gravity="center">
    <CheckBox
        android:id="@+id/item_cb"
        android:layout_width="60px"
        android:layout_height="40px"
        android:layout_marginLeft="20px"
        android:clickable="false"
        android:visibility="gone"/>
    <TextView
        android:id="@+id/item_tv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="40px"
        android:clickable="false"/>

</LinearLayout>
MainActivity代码

package circleimageview.com.listviewdemo;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Context mContext;
    private Button btn_delete_item, btn_delete;
    //全选
    private CheckBox cb_cheak_all;
    private int boo_check_all = 0;

    private ListView lv;
    private LinearLayout ll;
    private TextView tv_count;


    //数据源
    private ArrayList<String> arraylist;
    //给item添加id
    private ArrayList<String> selectId;
    private boolean isMulChoice = false; //是否多选
    //适配器对象
    private MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = this;
        initView();
        initData();
    }

    private void initData() {
        //2.初始化数据
        btn_delete_item.setOnClickListener(this);
        btn_delete.setOnClickListener(this);


        arraylist = new ArrayList<String>();
        selectId = new ArrayList<String>();
        for (int i = 0; i < 20; i++) {
            arraylist.add("测试" + i);
        }
        //适配器
        adapter = new MyAdapter(mContext, tv_count);
        lv.setAdapter(adapter);

        cb_cheak_all.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    //全选
                    boo_check_all = 1;
//                    
                } else {
                    //取消全选,清空
                    boo_check_all = 2;
                    
                }
		//清除数据,防止和点击后item叠加
                selectId.clear();
                //重置Adapter的数据
                adapter = new MyAdapter(mContext, tv_count);
                lv.setAdapter(adapter);
            }
        });
    }

    private void initView() {
        //1.初始化UI
        btn_delete_item = (Button) findViewById(R.id.btn_delete_item);
        btn_delete = (Button) findViewById(R.id.btn_delete);

        cb_cheak_all = (CheckBox) findViewById(R.id.cb_cheak_all);

        lv = (ListView) findViewById(R.id.lv);

        ll = (LinearLayout) findViewById(R.id.ll);
        //显示选中的总数
        tv_count = (TextView) findViewById(R.id.tv_count);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_delete_item:
                //3.点击后显示全选,删除,和选中了多少项布局,并且让item的CheckBox全部显示
                //显示删除按钮

                cb_cheak_all.setChecked(false);
                ll.setVisibility(View.VISIBLE);
                //显示item的Checkbox,
                //多选
                isMulChoice = true;
                //清空
                selectId.clear();
                for (int i = 0; i < arraylist.size(); i++) {
                    //让Check全部显示
                    adapter.visiblecheck.put(i, CheckBox.VISIBLE);
                }
                //重新给适配器传数据
                adapter = new MyAdapter(mContext, tv_count);
                lv.setAdapter(adapter);
                break;

            case R.id.btn_delete:
                //删除操作,每点击一次item,selectId都会记录当前的item是否选中,
		//选中则添加到selectId中,没选中则从selectId集合中删除,然后去和数据源ArrayList
		//的数据去比较,如果相同就从arraylist中删除,最后更新数据源
                //不多选
                isMulChoice = false;
                for (int i = 0; i < selectId.size(); i++) {
                    for (int j = 0; j < arraylist.size(); j++) {
                        //让选中的id项去和arraylist列表去比较相同就删除arraylist中的item
                        if (selectId.get(i).equals(arraylist.get(j))) {
                            arraylist.remove(j);
                        }

                    }
                }
                //清空
                selectId.clear();

                //更新适配器数据
                adapter = new MyAdapter(mContext, tv_count);
                lv.setAdapter(adapter);
                //让底部的删除按钮不显示
                ll.setVisibility(View.GONE);
                //让cb不显示
                break;
        }

    }

    class MyAdapter extends BaseAdapter {
        private Context context;
        private LayoutInflater inflater = null;
        private HashMap<Integer, View> mView;

        //用来记录每个item显示checkBox的参数(显示还是隐藏)
        // 通过点击按钮在构造方法中重新赋值,然后在getView()方法中设置给item
        public HashMap<Integer, Integer> visiblecheck;
       
        private TextView txtcount;

        public MyAdapter(Context context, TextView txtcount) {
            this.context = context;
            this.txtcount = txtcount;

            inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
            mView = new HashMap<Integer, View>();
            visiblecheck = new HashMap<Integer, Integer>();
            ischeck = new HashMap<Integer, Boolean>();

            //让Checkbox是否显示,是多选择显示cb
            if (isMulChoice) {
                //显示Check
                for (int i = 0; i < arraylist.size(); i++) {
                    //记录显示了多少个Check
                   
                    visiblecheck.put(i, CheckBox.VISIBLE);
                }
            } else {
                //不显示
                for (int i = 0; i < arraylist.size(); i++) {
               
                    visiblecheck.put(i, CheckBox.GONE);
                }
            }


        }

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

        @Override
        public Object getItem(int i) {
            return arraylist.get(i);
        }

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

        @Override
        public View getView(final int i, View view, ViewGroup viewGroup) {
            View v = null;
            if (v == null) {
                v = inflater.inflate(R.layout.item_listview, null);

                TextView tv = v.findViewById(R.id.item_tv);
                final CheckBox cb = v.findViewById(R.id.item_cb);
                //设置让tv和Checkbox不能点击

                //设置数据
                tv.setText(arraylist.get(i));
                //设置是否被选中
                cb.setChecked(false);
                //设置cb是否显示
                cb.setVisibility(visiblecheck.get(i));
                //点击每个item,后设置是否被选中,先将每个item的Check设置为不可点击(在xml文件中设置),
		//不设置会导致该方法无效
                v.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (isMulChoice) {
                            //说明已经按了删除按钮,可以选择

                            //如果cb选中则设置为不选中,否则为选中
                            if (cb.isChecked()) {
                                cb.setChecked(false);
                                cb.setBackgroundColor(Color.parseColor("#00ff00"));
                                //删除该item的id
                                selectId.remove(arraylist.get(i));
                            } else {
                                cb.setChecked(true);
                                cb.setBackgroundColor(Color.parseColor("#ff0000"));
                                selectId.add(arraylist.get(i));
                            }
                        }
                        //记录一共选中了多少项
                        tv_count.setText("共选中了" + selectId.size() + "项");
                    }
                });

                //全选CheckBox的操作根据传的参数让Check全部选中或者全部不选中
                switch (boo_check_all) {
                    case 0:
                        //没点击全选按钮时
                        break;
                    case 1:
                        //全选
                        //全选
                        cb.setChecked(true);
                        //并记录被选中的item的ID
                        selectId.add(arraylist.get(i));
                        tv_count.setText("共选中了"+selectId.size()+"项");
                        break;
                    case 2:
                        //取消全选
                        //取消全选
                        cb.setChecked(false);
                        selectId.remove(arraylist.get(i));
                        tv_count.setText("共选中了"+selectId.size()+"项");
                        break;
                }

            }

            return v;
        }
    }
}




           



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值