Android列表实现单选、多选、全选、取消、删除

1.init方法中的实现

定义2个HashMap用于存储已选择的及是否可见的标志

private void init() {
        isSelected = new HashMap();
        isVisible = new HashMap();
        if (mData != null && mData.size() > 0) {
            for (int i = 0; i < mData.size(); i++) {
                isSelected.put(i, false);
                isVisible.put(i, false);
            }
        }
    }
复制代码

2.getView方法中的实现

 @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view==null){

            view= LayoutInflater.from(mContext).inflate(R.layout.item_fund,viewGroup,false);

            holder=new ViewHolder();
            view.setTag(holder);

            holder.tv_name=view.findViewById(R.id.tv_name);
            holder.tv_code=view.findViewById(R.id.tv_code);
            holder.checkBox=view.findViewById(R.id.checkBox);

        }else {
            holder=(ViewHolder) view.getTag();
        }

        if (mData!=null){
            holder.tv_name.setText(mData.get(i).getName());
            holder.tv_code.setText(mData.get(i).getCode());

            holder.checkBox.setTag(i);
            if (type.equals("delete")){
                if (isSelected.get(i)){
                    isSelected.put(i,false);
                }

            }else {

            }

            holder.checkBox.setChecked(isSelected.get(i));


            if (isVisible.get(i)) {
                holder.checkBox.setVisibility(View.VISIBLE);
            } else {
                holder.checkBox.setVisibility(View.GONE);
            }
        }


        return view;
    }
复制代码

3.Activity中的实现

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView adapterView, View view, int position, long l) {
                if (isEdit) {
                    mAdapter.type = "select";
                    FundListAdpater.ViewHolder holder = (FundListAdpater.ViewHolder) view.getTag();
                    holder.checkBox.toggle();
                    mAdapter.isSelected.put(position, holder.checkBox.isChecked());
                }
            }
        });
        @Override
    public void onClick(View view) {

        isHasSelect = false;
        for (int i = 0; i < datas.size(); i++) {
            if (mAdapter.isSelected.get(i)) {
                isHasSelect = true;
                break;
            }
        }

        switch (view.getId()) {
            case R.id.tv_edit:
                if (datas != null && datas.size() > 0) {

                    if (isEdit) {
                        tv_edit.setText("编辑");
                        ll_bottom.setVisibility(View.GONE);

                        for (int i = 0; i < datas.size(); i++) {
                            mAdapter.isVisible.put(i, false);
                        }
                        mAdapter.notifyDataSetChanged();
                        isEdit = false;
                    } else {
                        tv_edit.setText("完成");
                        ll_bottom.setVisibility(View.VISIBLE);

                        for (int i = 0; i < datas.size(); i++) {
                            mAdapter.isVisible.put(i, true);
                        }
                        mAdapter.notifyDataSetChanged();
                        isEdit = true;
                    }
                }
                break;
            case R.id.tv_cancel:
                mAdapter.type = "select";
                tv_cancel.setSelected(true);
                tv_delete.setSelected(false);
                if (isAllSelect) {
                    for (int i = 0; i < datas.size(); i++) {
                        mAdapter.isSelected.put(i, false);
                    }
                } else {
                    for (int i = 0; i < datas.size(); i++) {
                        mAdapter.isSelected.put(i, true);
                    }
                }
                mAdapter.notifyDataSetChanged();
                isAllSelect = !isAllSelect;
                break;
            case R.id.tv_delete:
                mAdapter.type = "delete";
                if (isHasSelect) {
                    tv_cancel.setSelected(false);
                    tv_delete.setSelected(true);
                    if (deleteOrderNoList != null) {
                        deleteOrderNoList.clear();
                    }

                    for (int i = 0; i < datas.size(); i++) {
                        if (mAdapter.isSelected.get(i)) {
                            //FundListAdpater.ViewHolder holder = (FundListAdpater.ViewHolder) lv.getChildAt(i-lv.getFirstVisiblePosition()).getTag();

                            deleteOrderNoList.add(datas.get(i).getCode());//选择删除的订单号holder.tv_code.getText().toString()
                            FundBean deleteOrder = (FundBean) lv.getItemAtPosition(i);
                            deleteDatas.add(deleteOrder);
                        }
                    }
                    deleteFund(deleteOrderNoList);
                }

                break;
        }
    }

    private void deleteFund(List deleteOrderNoList) {
        if (datas != null) {
            datas.removeAll(deleteDatas);
            mAdapter.notifyDataSetChanged();
            mAdapter.notifyDataSetInvalidated();
        }
        Toast.makeText(this, deleteOrderNoList.toString(), Toast.LENGTH_LONG).show();
        if (datas.size() == 0) {
            tv_edit.setText("编辑");
            ll_bottom.setVisibility(View.GONE);

            for (int i = 0; i < datas.size(); i++) {
                mAdapter.isVisible.put(i, false);
            }
            mAdapter.notifyDataSetChanged();
            isEdit = false;
        }
    }
复制代码

4.效果图

转载于:https://juejin.im/post/5a70648b6fb9a01cbc6eafb7

你可能感兴趣的:(Android列表实现单选、多选、全选、取消、删除)