实现自定义checkbox listview的方法
1。方法一:利用adapter这种方法的原理
破坏view的重用,使用List保存checkchange状态,然后在bindview中添加根据list的状态修改checkbox的勾。
注意mCheckItemView.mCheckBox.setOnCheckedChangeListener(null);修改前屏蔽事件
package com.handcent.im.adapter;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.handcent.im.ui.R;
public class CheckedCursorAdapter extends CursorAdapter {
 private List<Integer> mCheckedList;
public CheckedCursorAdapter(Context context, Cursor c) {
 super(context, c);
mCheckedList = new ArrayList<Integer>();
 }
@Override
 public void bindView(View view, Context context, Cursor cursor) {
 System.out.println("bind");
 CheckItemView mCheckItemView = new CheckItemView();
 mCheckItemView.mHeadView = (ImageView) view.findViewById(R.id.ci_img_head);
 mCheckItemView.mNickName = (TextView) view.findViewById(R.id.ci_txt_name);
 mCheckItemView.mSignature = (TextView) view.findViewById(R.id.ci_txt_signure);
 mCheckItemView.mCheckBox = (CheckBox) view.findViewById(R.id.ci_chk_choose);
 // mCheckItemView.mCheckBox.setTag(cursor.getInt(0));
 for (int i : mCheckedList) {
 System.out.println("checkLIst:" + i);
 }
 mCheckItemView.mCheckBox.setOnCheckedChangeListener(null);
 if (mCheckedList.contains(cursor.getInt(0))) {
 mCheckItemView.mCheckBox.setChecked(true);
 } else {
 mCheckItemView.mCheckBox.setChecked(false);
 }
 mCheckItemView.mCheckBox.setTag(cursor.getInt(0));
 mCheckItemView.mCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
 public void onCheckedChanged(CompoundButton buttonView,
 boolean isChecked) {
 // checkbox更改状态
 Integer index = (Integer) buttonView.getTag();
 if (isChecked) {
 if (!mCheckedList.contains(index))
 mCheckedList.add(index);
 } else {
 mCheckedList.remove(index);
 }
}
 });
mCheckItemView.mNickName.setText(cursor.getString(1));
 mCheckItemView.mSignature.setText(cursor.getString(2));
 // int tag = (Integer) mCheckItemView.mCheckBox.getTag();
}
@Override
 public View newView(Context context, Cursor cursor, ViewGroup parent) {
 LayoutInflater mInflater = LayoutInflater.from(context);
 View mView = mInflater.inflate(R.layout.contactitem, null);
return mView;
 }
private class CheckItemView {
 private ImageView mHeadView;
 private TextView mNickName;
 private TextView mSignature;
 private CheckBox mCheckBox;
 }
}
2。方法二:使用CheckedTextView
原理:自定义一个继承Checkable的Linearlayout或者RelativeLayout,在其中放置一个CheckedTextView不使用TextView的值而达到效果
package com.handcent.im.ui.view;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Checkable;
import android.widget.CheckedTextView;
import android.widget.RelativeLayout;
/**
 * custom view extends {@link RelativeLayout} and implements {@link Checkable}
 * to response the action CheckBox
 * 
 * @author Pandans
 * 
 */
public class ChkRelativeLayout extends RelativeLayout implements Checkable {
private CheckedTextView _checkbox;
public ChkRelativeLayout(Context context) {
 super(context);
 }
public ChkRelativeLayout(Context context, AttributeSet set) {
 super(context, set);
 }
@Override
 public boolean isChecked() {
 return _checkbox != null ? _checkbox.isChecked() : false;
 }
@Override
 protected void onFinishInflate() {
 super.onFinishInflate();
 // find checked text view
 int childCount = getChildCount();
 for (int i = 0; i < childCount; ++i) {
 View v = getChildAt(i);
 if (v instanceof CheckedTextView) {
 _checkbox = (CheckedTextView) v;
 }
 }
 }
/**
 * <p>
 * Changes the checked state of this LinearLayout
 * </p>
 * 
 * @param checed
 * true to check the LinearLayout ,false to uncheck it
 */
 @Override
 public void setChecked(boolean checked) {
 if (_checkbox != null) {
 _checkbox.setChecked(checked);
 }
}
@Override
 public void toggle() {
 if (_checkbox != null) {
 _checkbox.toggle();
 }
 }
}
                    
                
                
            
        
浙公网安备 33010602011771号