在开发ListView时候,很多情况下,我们都会对item进行批量操作,那么怎样实现这样的功能呢?其实很简单,我就把我的思路分享给大家吧,如果有错的话,还请大家指出,谢谢大家。
具体思路有三步:
1、编辑一个ItemBean来保存item数据和item中CheckBox的显示状态。
2、重写BaseAdapter,根据传入的List 显示Item,并写一个内部类ViewHolder来关联item的View,便于结合ListView的双缓冲机制,提高效率。
3、设置item的单击和长按的响应事件(改变每个ItemBean的相关数据和状态),并更新ListView。
来两张程序图:
1、刚开始进入程序:
2、长按item时候响应的图片:
好了,接下来我们上代码:
一、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类: