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 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; }}



你可能感兴趣的:(android)