public class ListViewCheckBoxBaseAdapter extends BaseAdapter{
private List<Map<String, Object>> mDatas;
private int mLayoutId;
private String[] mFrom;
private int[] mTo;
private Context mContext;
public boolean isVisible=false;//复选框是否可见
public boolean[] mHasChecked;//记录每个复选框的状态,是否被选中
public boolean[] mHasCheckedCache;//缓存
public ZHCG_ListViewBaseAdapter(){}
public ZHCG_ListViewBaseAdapter(
Context context,
List<Map<String, Object>> datas,
int resLayoutId,
String[] from,
int[] to){
this.mContext=context;
this.mDatas=datas;
this.mLayoutId=resLayoutId;
this.mFrom=from;
this.mTo=to;
}
@Override
public int getCount() {
if(mDatas==null){
return 0;
}
return mDatas.size();
}
@Override
public Object getItem(int position) {
return mDatas.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if(convertView==null){
v=LayoutInflater.from(mContext).inflate(mLayoutId, null);
}else{
v=convertView;
}
//当复选框个数多于mHasChecked长度时,以复选框个数为长度重新申请mHasChecked
//用于ListView下拉加载更多的时候
if(isVisible&&mHasChecked.length<getCount()){
mHasCheckedCache=mHasChecked.clone();
mHasChecked=new boolean[getCount()];
for(int i=0;i<mHasCheckedCache.length;i++){
mHasChecked[i]=mHasCheckedCache[i];
}
}
bindView(v,position);
return v;
}
private void bindView(View view,int position){
if(position>mDatas.size()){
return;
}
final Map<String, Object> dataSet = mDatas.get(position);
if (dataSet == null) {
return;
}
final String[] from = mFrom;
final int[] to = mTo;
final int count = to.length;
for (int i = 0; i < count; i++) {
final View v = view.findViewById(to[i]);
if (v != null) {
String clzzName = v.getClass().getSimpleName();
try {
if (clzzName.equals("TextView")) {// 设置文本框内容
final Object data = dataSet.get(from[i]);
String text = data == null ? "" : data.toString();
if (text == null) {
text = "";
}
((TextView) v).setText(text);
}else if (clzzName.equals("CheckBox")) {// 设置复选框是否可见,并设置点击监听器
if (isVisible) {
v.setVisibility(View.VISIBLE);
v.setTag(position);
((CheckBox) v).setChecked(mHasChecked[position]);
v.setOnClickListener(new CheckBoxClickLsn());
} else {
v.setVisibility(View.GONE);
}
}else if (clzzName.equals("")) {//根据实际开发时控件的类型,进行相应操作
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
*
* 2014-6-24 下午4:24:57
* @return
* TODO 获取复选框选中个数
*/
public int getCheckedCount(){
int count=0;
for (int i = 0; i < mHasChecked.length; i++) {
if(isChecked(i)){
count++;
}
}
return count;
}
/**
*
* 2014-6-25 上午10:01:42
* TODO 复选框全选
*/
public void selectAllCheckBox(){
for (int i = 0; i < mHasChecked.length; i++) {
mHasChecked[i]=!mHasChecked[i];
}
}
/**
*
* 2014-6-24 下午4:30:24
* @param index
* @return
* TODO 判断复选框是否被选中
*/
public boolean isChecked(int index){
return mHasChecked[index];
}
/**
*
* 2014-6-24 下午4:39:11
* @param index
* TODO 记录哪个复选框被勾选了
*/
public void checkChange(int index){
mHasChecked[index]=!mHasChecked[index];
}
/**
*
* 2014-6-24 下午4:46:31
* @param isVisible
* @param texTitle
* TODO 设置复选框是否可见
*/
public void setCheckBoxVisible(boolean isVisible,TextView textTitle){
this.isVisible=isVisible;
if(isVisible){
mHasChecked=new boolean[getCount()];
}
}
/**
*
* @Create_date 2014-6-25 上午9:01:43
* @TODO 复选框选中监听器
*/
public class CheckBoxClickLsn implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
final int position=(Integer) v.getTag();
checkChange(position);
}
}
}
这里主要用于ListView item布局里有一个TextView显示内容的,CheckBox复选框,初始时是隐藏的,当长按时调用setCheckBoxVisible() 进行显示