zoukankan      html  css  js  c++  java
  • 添加CheckBox实现ListView的长按批量删除

      工作中遇到这样的需求,在长按ListView的Item的时候出现复选框,可进行批量选择,从而实现批量删除。

      实现原理如下:

      ListView每一条Item的数据我们通常存放在List集合中,考虑到ListView的复用问题,需要在我们的数据集合中新加入一个字段,用来记录是否勾选了当前的Item,当勾选复选框或者取消勾选的时候,用来记录的字段相应的改为true或者false。避免布局复用而引起的数据混乱,下面代码进行详解。

      首先,布局中添加CheckBox,并设置状态为隐藏。

    <CheckBox
    android:clickable="false"
    android:id="@+id/cb"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="gone"
    android:focusable="false"
    android:textIsSelectable="false"
    android:layout_gravity="center_vertical"

      长按点击事件。CheckBox显示,同时删除按钮出现。

    wait_list.setOnItemLongClickListener(new OnItemLongClickListener() {

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view,
    int position, long id) {
    adapter.setVisable(true);
    deleteLayout.setVisibility(View.VISIBLE);
    adapter.notifyDataSetChanged();
    return true;
    }
    });

      还有一个问题就是ItemClickListener事件和CheckBox的点击事件会冲突,这里是这样处理的

    wait_list.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view,
    int position, long id) {
    if(position>=listAll.size()){
    return;
    }
    if(adapter.isVisable()){
    HodlerView h=(HodlerView) view.getTag();
    h.cb.toggle();
    if(h.cb.isChecked()){
    listAll.get(position).setMyChecked(true);
    adapter.notifyDataSetChanged();
    addMap(position, listAll.get(position).getWaybillNo());
    }else{
    listAll.get(position).setMyChecked(false);
    adapter.notifyDataSetChanged();
    removeMap(position);
    }
    }else{
    //点击item跳转详情
    handler.sendMessage(handler.obtainMessage(1,listAll.get(position).getInfoId()+","+listAll.get(position).getWaybillNo()
    +","+listAll.get(position).getHaulier()
    +","+listAll.get(position).getShipper()+","+listAll.get(position).getJudgeIsComplained()+","+listAll.get(position).getInfoContent()));
    }
    }
    });

      下面是勾选或者取消的时候用一个Map来进行记录的方法

    //用来增加勾选项
    public void addMap(int position,String wayBillNo) {
    Integer pos = Integer.valueOf(position);
    mySelect.put(pos, wayBillNo);
    System.out.println("=======集合中元素合数"+mySelect.size());
    }
    //用来删除取消勾选的选项
    public void removeMap(int position){
    Integer pos = Integer.valueOf(position);
    mySelect.remove(pos);
    if(mySelect.size()!=listAll.size()){
    selectAll.setChecked(false);
    }
    }
    //用来清除Map里面所有数据
    public void cleanMap(){
    mySelect.clear();
    }

    以上差不多就是实现这个小功能的核心代码,希望对大家有所帮助

  • 相关阅读:
    深漂一年,一位程序员的2016年终告白
    Springlake-02 权限&文档设置&Role设置&Folder设置&登录
    Springlake-01 介绍&功能&安装
    IOS Socket 03-建立连接与登录
    IOS Socket 02-Socket基础知识
    IOS Socket 01-网络协议基础知识
    IOS Animation-CAKeyframeAnimation例子(简单动画实现)
    IOS Animation-CAShapeLayer、UIBezierPath与Animation的结合
    IOS Animation-动画基础、深入
    IOS Animation-CABasicAnimation例子(简单动画实现)
  • 原文地址:https://www.cnblogs.com/fuyinshan/p/6039911.html
Copyright © 2011-2022 走看看