Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 我可以为ListView中的各个项目设置不同的边框吗?_Android_Listview_Colors_Border_Listitem - Fatal编程技术网

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

Android 我可以为ListView中的各个项目设置不同的边框吗?,android,listview,colors,border,listitem,Android,Listview,Colors,Border,Listitem,我有一个自定义的ListView适配器,它可以根据一些规则更改列表项的背景颜色 这基本上是可行的 在1个场景中,我想在同一个列表项中表示2种颜色。我想我可以通过设置边框的厚度和颜色来实现这一点,但我无法实现这一点 有人能告诉我这是否可以做到,如果可以,如何做到 此外,如果有另一种方法可以在同一列表项中显示2种颜色(比如说一种颜色渐变为另一种颜色),那么我很乐意使用不同的实现 代码如下: public class SpecialAdapter extends SimpleAdapter { pub

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

这基本上是可行的

在1个场景中,我想在同一个列表项中表示2种颜色。我想我可以通过设置边框的厚度和颜色来实现这一点,但我无法实现这一点

有人能告诉我这是否可以做到,如果可以,如何做到

此外,如果有另一种方法可以在同一列表项中显示2种颜色(比如说一种颜色渐变为另一种颜色),那么我很乐意使用不同的实现

代码如下:

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;
}
}
公共类SpecialAdapter扩展了SimpleAdapter{
public ArrayList listcolors=new ArrayList();
ArrayList listItems=新的ArrayList();
公共静态最终字符串LOGid=“TouchAndGo”;
公共SpecialAdapter(上下文上下文、列表项、int资源、字符串[]从、int[]到){
超级(上下文、项目、资源、发件人、收件人);
}
公共SpecialAdapter(WWFoodInderActivity上下文,
列表数据,int-simpleListItem2,字符串[]from,
int[]至){
super(上下文、数据、simpleListItem2、from、to);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图=空;
尝试
{
view=super.getView(位置、转换视图、父级);
Log.d(LOGid,“位置:“+Position+”值:“+listcolors.get(Position)+”行:“+listItems.get(Position));
}
捕获(例外e)
{
Log.e(LOGid,“无法获取视图的句柄”+e.getMessage());
}
/*这永远不起作用-无法通过以下方式获取thisview的句柄:(*/
ListView lv=null;
尝试
{
lv=(ListView)视图;
}
捕获(例外e)
{
Log.e(LOGid,“未能获取Listview上的句柄”+e.getMessage());
}
尝试
{
if(listcolors.get(position).equals(“Y”))
{
Log.d(LOGid,“视图设置为绿色,带绿色边框”);
view.setBackgroundColor(android.graphics.Color.rgb(40150,40));
lv.设置分隔灯(2);
ColorDrawable green=新的ColorDrawable(android.graphics.Color.rgb(40150,40));
低压设置分隔器(绿色);
}
else if(listcolors.get(position).equals(“C”))
{
Log.d(LOGid,“视图设置为带蓝色边框的蓝色”);
view.setBackgroundColor(android.graphics.Color.rgb(40,40150));
lv.设置分隔灯(2);
ColorDrawable blue=新的ColorDrawable(android.graphics.Color.rgb(40,40150));
低压分压器(蓝色);
}
/*这是例外情况,需要在同一列表项中表示2种颜色*/
else if(listcolors.get(position).equals(“Y/C”))
{
Log.d(LOGid,“视图设置为带蓝色边框的绿色”);
view.setBackgroundColor(android.graphics.Color.rgb(40150,40));
lv.设置分隔灯(2);
ColorDrawable blue=新的ColorDrawable(android.graphics.Color.rgb(40150,40));
低压分压器(蓝色);
//lv.setDivider();
//Log.d(LOGid,“视图设置为绿色”);
}
其他的
{
Log.d(LOGid,“视图设置为黑色加黑色”);
view.setBackgroundColor(android.graphics.Color.BLACK);
lv.设置分隔灯(2);
ColorDrawable black=新的ColorDrawable(android.graphics.Color.rgb(0,0,0));
低压设置分隔器(黑色);
}
}
捕获(例外e)
{
Log.e(LOGid,“错误呈现列表项:”+e.getMessage());
}
/*int colorPos=位置%colors.length;
view.setBackgroundColor(颜色[colorPos]);*/
返回视图;
}
}

最后一步是,我想分别为一些列表项设置边框。

尝试使用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中删除分隔符