Могу ли я установить другую границу для отдельных элементов в ListView? - PullRequest
0 голосов
/ 19 марта 2012

У меня есть собственный адаптер 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 голосов
/ 19 марта 2012

Попробуйте использовать 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.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...