我正在使用自定义ListView适配器,并且运行良好。
我目前正在使用checkBox来存储选中的值,但我认为在选择列表项时更改列表项的背景颜色可能会更好。
我已尝试(并且失败)使用选择器执行此操作,并尝试修改我的代码以更改视图背景。
这是我的自定义适配器代码,可以更改为在选择行时设置蓝色背景,并在选择多个时保持选择吗?
public class ImageAdapter extends BaseAdapter {
Context context;
ArrayList<Bitmap> images;
ArrayList folderName;
boolean[] checkBoxState;
public ImageAdapter(Context c, ArrayList<Bitmap> images, ArrayList folderName){
this.context = c;
this.images = images;
this.folderName = folderName;
this.context = context;
checkBoxState=new boolean[images.size()];
}
public int getCount() {
return images.size();
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
private class ViewHolder {
TextView title;
ImageView iconImage;
CheckBox checkbox;
}
public void performClick(Integer position) {
if(isChecked(position))
{
checkBoxState[position] = false;
}
else
{
checkBoxState[position] = true;
}
notifyDataSetChanged();
}
public void selectedAll(ArrayList<Integer> checkedPositions) {
for(int i = 0; i< checkedPositions.size(); i++){
checkBoxState[checkedPositions.get(i)] = true;
}
notifyDataSetChanged();
}
public void deselectedAll(ArrayList<Integer> checkedPositions) {
for(int i = 0; i< checkedPositions.size(); i++){
checkBoxState[checkedPositions.get(i)] = false;
}
notifyDataSetChanged();
}
public boolean isChecked(Integer pos){
if(checkBoxState[pos] == true)
return true;
else
return false;
}
public View getView(final int position, View arg1, ViewGroup arg2) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//View gridView;
View v = arg1;
ViewHolder holder;
if (arg1 == null) {
LayoutInflater vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_row, null);
holder = new ViewHolder();
holder.title = (TextView) v.findViewById(R.id.filename);
holder.iconImage = (ImageView) v.findViewById(R.id.list_image);
holder.checkbox = (CheckBox)v.findViewById(R.id.checkBox1);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
holder.title.setText(folderName.get(position).toString());
holder.iconImage.setImageBitmap(images.get(position));
//Set the state of the
//CheckBox using the boolean array
holder.checkbox.setChecked(checkBoxState[position]);
//for managing the state of the boolean
//array according to the state of the
//CheckBox
holder.checkbox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(((CheckBox)v).isChecked())
{
checkBoxState[position]=true;
}
else
{
checkBoxState[position]=false;
}
}
});
return v;
}
}
答案 0 :(得分:0)
看起来你已经有了复选框的状态,可以进行多项选择,太棒了!
为什么不使用这种方法来做背景色?
在代码中找到此行(适配器)
//Set the state of the
//CheckBox using the boolean array
holder.checkbox.setChecked(checkBoxState[position]);
并将其放在
下面v.setBackgroundColor( (checkBoxState[position]) ? Color.parseColor("#0000FF") : Color.parseColor("#000000") );
更改颜色以符合您的需要,但基本上第一种颜色是您想要的SELECTED颜色,而第二种颜色是未选择该项目时的颜色。
享受!