v.setBackgroundColor(Color.parseColor("#88dfdf"));
你不仅应该这样做,而且还应该有一个ArrayList<Boolean> selectedList
如果选择了该项目,您将“记住”其中的内容。然后在 getView 中,您应该“检查”该列表并相应地设置颜色。
if ( selectedList.get(position) )
convertView.setBackgroundColor(Color.parseColor("#88dfdf"));
else
convertView.setBackgroundColor(Color.parseColor( normal color ));
初始化适配器类中的列表:
ArrayList<Boolean> selectedList = new ArrayList<Boolean>();
public CustomAdapter(Context context, List<Contacts> mList)
{
.........
int nr = -1;
while (++nr < mList.size() )
selectedList.add(false);
}
}
还将其添加到 getView() 函数中
public View getView(final int position, View convertView, ViewGroup parent)
{ ...............................
holder.contact_img.setImageBitmap(ImageHelper.getRoundedCornerBitmap(scaleBitmap, 100));
if(selectedList.get(position)== true)
{
convertView.setBackgroundColor(Color.parseColor("#88dfdf"));
}
else
{
convertView.setBackgroundColor(Color.background_light);
}
return convertView;
}
还将以下行添加到 onListItemClick() 中。
protected void onListItemClick(ListView l, View v, int position, long id)
{
..................
if(mArrayAdapter.selectedList.get(position)==true)
{
v.setBackgroundColor(Color.background_light));
mArrayAdapter.selectedList.set(position,false);
}
else
{
v.setBackgroundColor(Color.parseColor("#88dfdf"));
mArrayAdapter.selectedList.set(position,true);
Toast.makeText(getApplicationContext(), "Items Pos: " + position +"and Name : "+ name, 0).show();
}
}
并在 CustomAdapter 中将 selectedList 变量设置为公共。