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

在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<TagForChose> tagForChoseList;
    private BaseAdapter tagSelectedAdapter;
    //将第n行的显示状态记下来,便于在getView中显示正确的图片。
private HashMap<Integer,Boolean> isCheckedMap = new HashMap<Integer, Boolean>(); private Context context; //在构造器中进行赋值,为1则显示复选框,为2则不显示复选框和下一级提示。 private int showCheckBox = 1; /** * 只有当自己就是TagSelect时,才调用这个构造器。 * @param context * @param tagCategoryList * @param tagSelectedAdapter TagSelected 对应的adapter */ public TagSelectAdapter(Context context, List<TagForChose> 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<TagForChose> tagCategoryList) { this.context = context; inflater = LayoutInflater.from(context); this.tagForChoseList = tagCategoryList; } public final int getCount() { return tagForChoseList.size(); } public List<TagForChose> 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; }}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值