在listView中如果需要使用复选框功能,强烈建议使用图片作为背景代替checkBox。亲测好用。

1,可以将该复选框的显示状态记录在一个字段中,

if (this.list.get(position).isChecked()) {    viewHolder.checkBox.setImageResource(R.drawable.checkbox_selected);}else {    viewHolder.checkBox.setImageResource(R.drawable.checkbox_up);}


2,也可以按我下面的方法按位置记录在一个map中。



import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.*;import com.justonetech.zzxs.android.R;import com.justonetech.zzxs.android.activity.base.MyApplication;import com.justonetech.zzxs.webservice.domain.TagForChose;import java.util.HashMap;import java.util.List;public class TagSelectAdapter extends BaseAdapter {    private LayoutInflater inflater;    private List tagForChoseList;    private BaseAdapter tagSelectedAdapter;
   
    //将第n行的显示状态记下来,便于在getView中显示正确的图片。
private HashMap isCheckedMap = new HashMap(); private Context context; //在构造器中进行赋值,为1则显示复选框,为2则不显示复选框和下一级提示。 private int showCheckBox = 1; /** * 只有当自己就是TagSelect时,才调用这个构造器。 * @param context * @param tagCategoryList * @param tagSelectedAdapter TagSelected 对应的adapter */ public TagSelectAdapter(Context context, List tagCategoryList, BaseAdapter tagSelectedAdapter, int showCheckBox) { this. context = context; inflater = LayoutInflater. from(context); this. tagForChoseList = tagCategoryList; this. tagSelectedAdapter = tagSelectedAdapter; this. showCheckBox = showCheckBox; } /** * 只有当自己就是TagSelected时,才调用这个构造器。用于上面的 listview 。 * @param context * @param tagCategoryList */ public TagSelectAdapter(Context context, List tagCategoryList) { this. context = context; inflater = LayoutInflater. from(context); this. tagForChoseList = tagCategoryList; } public final int getCount() { return tagForChoseList.size(); } public List getTagForChoseList() { return tagForChoseList; } public final Object getItem( int paramInt) { return tagForChoseList.get(paramInt); } public final long getItemId( int paramInt) { return paramInt; } public View getView( int position, View paramView, ViewGroup paramViewGroup) { ViewHolder holder = null; if (paramView == null) { paramView = inflater.inflate(R.layout. category_item, null); holder = new ViewHolder(); paramView.setTag(holder); holder. txtCategory = (TextView) paramView.findViewById(R.id. txtCategory); holder. txtArrowNext = (ImageView) paramView.findViewById(R.id. txtArrowNext); holder. ivCheckbox = (ImageView) paramView.findViewById(R.id. iv_checkbox); } else { holder = (ViewHolder) paramView.getTag(); } holder. txtCategory.setText( tagForChoseList.get(position).getTagName()); if (! tagForChoseList.get(position).getHasChild()){ holder. txtArrowNext.setVisibility(View. GONE); if( showCheckBox == 1){ holder. ivCheckbox.setVisibility(View. VISIBLE); if( tagSelectedAdapter == null){ holder. ivCheckbox.setImageResource(R.drawable. apptheme_btn_check_on_disabled_focused_holo_light); holder. ivCheckbox.setOnClickListener( new MyOnclickListener(position)); } else { holder. ivCheckbox.setOnClickListener( new MyOnclickListener(position)); Boolean b = isCheckedMap.get(position); if( null == b || b.booleanValue() == false){ holder. ivCheckbox.setImageResource(R.drawable. apptheme_btn_check_off_disabled_focused_holo_light); } else { holder. ivCheckbox.setImageResource(R.drawable. apptheme_btn_check_on_disabled_focused_holo_light); } } } else if( showCheckBox== 2) { holder. ivCheckbox.setVisibility(View. GONE); } } return paramView; } private class MyOnclickListener implements View.OnClickListener{ private int position; public MyOnclickListener( int position) { this. position = position; if( null == tagSelectedAdapter){ isCheckedMap.put(position, true); } else { isCheckedMap.put(position, false); } } @Override public void onClick(View v) { ImageView imageView = (ImageView) v; if( null == tagSelectedAdapter){ imageView.setImageResource(R.drawable. apptheme_btn_check_off_disabled_focused_holo_light); if(MyApplication. getInstance(). tagForChoseList.contains( tagForChoseList.get( position))){ MyApplication. getInstance(). tagForChoseList.remove( tagForChoseList.get( position)); } isCheckedMap.put( position, false); } else { Boolean b = isCheckedMap.get( position); if( null == b || b.booleanValue() == false){ imageView.setImageResource(R.drawable. apptheme_btn_check_on_disabled_focused_holo_light); if(!MyApplication. getInstance(). tagForChoseList.contains( tagForChoseList.get( position))){ MyApplication. getInstance(). tagForChoseList.add( 0, tagForChoseList.get( position)); } else { Toast. makeText( context, "该项已存在",Toast. LENGTH_SHORT).show(); } isCheckedMap.put( position, true); } else { imageView.setImageResource(R.drawable. apptheme_btn_check_off_disabled_focused_holo_light); if(MyApplication. getInstance(). tagForChoseList.contains( tagForChoseList.get( position))){ MyApplication. getInstance(). tagForChoseList.remove( tagForChoseList.get( position)); } isCheckedMap.put( position, false); } } if( null != tagSelectedAdapter){ tagSelectedAdapter.notifyDataSetChanged(); } else { notifyDataSetChanged(); } } } public class ViewHolder { protected TextView txtCategory; protected ImageView txtArrowNext; // protected CheckBox checkbox; protected ImageView ivCheckbox; }}



更多相关文章

  1. Android(安卓)说说钟表控件
  2. Android(安卓)EditText 点击时不弹出键盘但显示光标
  3. Android(安卓)画虚线显示实线的BUG
  4. Android(安卓)Dialog,Toast封装
  5. RxJava 学习笔记(四)
  6. android 开发艺术探索-Activity的生命周期和启动模式
  7. Services的生命周期
  8. android ndk的使用
  9. android中OkHttp的导入和get、post请求的简单教程

随机推荐

  1. Android ListView中处理嵌套滑动问题,类
  2. Android的无边界程序设计理念
  3. onTouchEvent, onClick及onLongClick的调
  4. Android串口开发之使用JNI实现ANDROID和
  5. Android网络通信(7):NFC
  6. android 笔记 --- InputFilter实现EditTe
  7. Android开心消消乐代码实例详解
  8. 如何判断android activity是否运行
  9. Android中使用Handler&Looper更新UI范例
  10. 为什么很多机器不能升级Android 4.3 (4.0