0
我有一个自定义的ListView适配器,根据一些规则更改列表项的背景颜色。我可以为ListView中的单个项目设置不同的边框吗?
这主要是工作。
在1场景中,我想在同一个listitem中表示2种颜色。我认为我可以通过设置边框的边框厚度和颜色来做到这一点,但我无法让它工作。
有人可以告诉我,如果可以这样做,如果是的话如何。另外,如果有另一种方式来显示同一个listitem中的两种颜色(比如说一种颜色渐变为另一种颜色),那么我会对另一种实现感到满意。
下面是代码:
public class SpecialAdapter extends SimpleAdapter {
public ArrayList<String> listColours=new ArrayList<String>();
ArrayList<String> listItems=new ArrayList<String>();
public static final String LOGid = "TouchAndGo";
public SpecialAdapter(Context context, List<HashMap<String, String>> items, int resource, String[] from, int[] to) {
super(context, items, resource, from, to);
}
public SpecialAdapter(WWFoodFinderActivity context,
List<Map<String, String>> data, int simpleListItem2, String[] from,
int[] to) {
super(context, data, simpleListItem2, from,to);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
try
{
view = super.getView(position, convertView, parent);
Log.d(LOGid, "Position:" + position + "Value:" + listColours.get(position) + "Line:" + listItems.get(position));
}
catch (Exception e)
{
Log.e(LOGid, "Failed to get handle on view " + e.getMessage());
}
/* This never works - cannot get a handle on the thisview this way :(*/
ListView lv = null;
try
{
lv = (ListView) view;
}
catch (Exception e)
{
Log.e(LOGid, "Failed to get handle on Listview " + e.getMessage());
}
try
{
if (listColours.get(position).equals("Y"))
{
Log.d(LOGid, "View set to green with green border");
view.setBackgroundColor (android.graphics.Color.rgb(40, 150, 40));
lv.setDividerHeight(2);
ColorDrawable green = new ColorDrawable(android.graphics.Color.rgb(40, 150, 40));
lv.setDivider(green);
}
else if (listColours.get(position).equals("C"))
{
Log.d(LOGid, "View set to blue with blue border");
view.setBackgroundColor (android.graphics.Color.rgb(40, 40, 150));
lv.setDividerHeight(2);
ColorDrawable blue = new ColorDrawable(android.graphics.Color.rgb(40, 40, 150));
lv.setDivider(blue);
}
/* This is the exception case, need to represent 2 colours in the same listitem */
else if (listColours.get(position).equals("Y/C"))
{
Log.d(LOGid, "View set to green with blue border");
view.setBackgroundColor (android.graphics.Color.rgb(40, 150, 40));
lv.setDividerHeight(2);
ColorDrawable blue = new ColorDrawable(android.graphics.Color.rgb(40, 150, 40));
lv.setDivider(blue);
//lv.setDivider();
// Log.d(LOGid, "View set to green");
}
else
{
Log.d(LOGid, "View set to black with black");
view.setBackgroundColor (android.graphics.Color.BLACK);
lv.setDividerHeight(2);
ColorDrawable black = new ColorDrawable(android.graphics.Color.rgb(0, 0, 0));
lv.setDivider(black);
}
}
catch (Exception e)
{
Log.e(LOGid, "Error rendering list item: " + e.getMessage());
}
/* int colorPos = position % colors.length;
view.setBackgroundColor(colors[colorPos]); */
return view;
}
}
最后一步我是,我想单独设置边框几个ListItem。使用BaseAdapter然后在getView功能