我可以为ListView中的单个项目设置不同的边框吗?

时间:2012-03-19 09:10:16

标签: android listview colors border listitem

我有一个自定义ListView适配器,可根据某些规则更改列表项的背景颜色。

这主要是有效的。

在1个场景中,我想在同一个列表项中表示2种颜色。我以为我可以通过设置边框的边框粗细和颜色来做到这一点,但我无法让它工作。

有人可以告诉我是否可以这样做,如果可以的话。

另外,如果还有另一种方法可以在同一个列表项中显示2种颜色(比如1种渐变到另一种颜色的颜色),那么我会对不同的实现感到满意。

以下是代码:

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

我的最后一步是我想单独设置一些列表项的边框。

1 个答案:

答案 0 :(得分:0)

尝试使用BaseAdapter,然后使用getView函数:

public View getView(int position, View convertView, ViewGroup parent) 
{
View view = inflater.inflate(R.layout.yourlayout, null);        
try
  {

      if (listColours.get(position).equals("Y"))
      {
          view.setBackgroundColor (android.graphics.Color.rgb(40, 150, 40));
          ColorDrawable green = new ColorDrawable(android.graphics.Color.rgb(40, 150, 40));
      }
      else if (listColours.get(position).equals("C"))
          view.setBackgroundColor (android.graphics.Color.rgb(40, 40, 150));
          ColorDrawable blue = new     ColorDrawable(android.graphics.Color.rgb(40, 40, 150));
      }
      else if (listColours.get(position).equals("Y/C"))
      {
          view.setBackgroundColor (android.graphics.Color.rgb(40, 150, 40));
          ColorDrawable blue = new     ColorDrawable(android.graphics.Color.rgb(40, 150, 40));
      }

      else
      {
          view.setBackgroundColor (android.graphics.Color.BLACK);
          ColorDrawable black = new     ColorDrawable(android.graphics.Color.rgb(0, 0, 0));
      }
  }
  catch (Exception e)
  {
      Log.e(LOGid, "Error rendering list item: " + e.getMessage());
  }

return view;
}

使用您自己的分隔符设置项目的布局,并在此处更改其颜色。不要忘记从ListView中删除分隔符。