实现自定义checkbox listview的方法
2011-11-10 18:19
295 查看
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();
}
}
}
破坏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();
}
}
}
相关文章推荐
- 带checkbox的ListView实现(二)——自定义Checkable控件的实现方法
- 【转】带checkbox的ListView实现(二)——自定义Checkable控件的实现方法
- 带checkbox的ListView实现——自定义Checkable控件的实现方法
- 带checkbox的ListView实现(二)——自定义Checkable控件的实现方法
- 实现自定义checkbox listview的方法
- 带checkbox的ListView实现(二)——自定义Checkable控件的实现方法
- 带checkbox的ListView实现(二)——自定义Checkable控件的实现方法
- 带checkbox的ListView实现(三)——CheckableImageView的实现方法
- ListView使用自定义适配器的情况下实现适配器的文本和图标控件点击事件执行Activity界面中的方法
- 自定义实现带checkbox的listView(用于项目中的多语言实现界面)
- listView 与自定义checkBox 实现多选效果,并记录多选的结果(左文字,右图片)
- 自定义ListView item包含checkbox实现单选和多选
- ListView使用自定义适配器的情况下实现适配器的控件点击事件执行Activity界面中的方法
- AndrowListView实现(自定义游戏列表)防止屏幕闪烁,设置分割线android中不推荐的方法,要考虑向下兼容,用了推荐的新方法,可能不兼容旧版本系统的手机
- Android---自定义带CheckBox的ListView实现
- Android---自定义带CheckBox的ExpandableListView实现
- Android 使用CheckBox实现ListView自定义单选
- Android开发:setContentView切换界面,自定义带CheckBox的ListView显示SQlite条目-----实现
- 关于checkbox的ListView实现多选、全选、反选、删除更简单的方法
- 自定义ListView item包含checkbox实现单选和多选