Android ListView设置项目背景



我似乎不知道如何使一个或多个listview项的背景颜色发生变化。

我目前正在尝试通过适配器获取视图。

ViewGroup v = (ViewGroup) adapter.getView(i, null, listView);
for(int k = 0; k < v.getChildCount(); k++) {
    View child = v.getChildAt(k);
    child.setBackgroundColor(0xFFA6D2FF);
}
listView.invalidateViews();

我已经尝试设置包含文本项的ViewGroup v(列表视图项有子项,因此它是2个文本视图)。我也尝试过设置孩子的背景,这可能有效,但它们的边界似乎是0。所以它可能有效,但即使你能看到文本,孩子们也没有尺寸。

最好从自定义基本适配器更改项目的颜色,创建以下方法:

private int[] colors;
// In Constructor or whenever you get data
colors = new int[sizeOfList];
private void setColors(int[] positions, int color) {
  for (int pos : positions) {
    colors[pos] = color;
   }
   notifyDataSetChanged();
}
private void setColors(int position, int color) {
   colors[pos] = color;
   notifyDataSetChanged();
}
public View getView(...) {
  if (colors[position] != 0) {
    child.setBackgroundColor(colors[position]);
  } else {
    child.setBackgroundColor(// default color);
  }
  return view;
}

希望能有所帮助。我认为其他答案也建议你们也这样做

如果我正确理解您的代码,您需要为列表项中的每个视图指定背景色。

如果您想以程序方式(而不是通过XML)执行此操作,我认为最简单的方法是创建一个自定义适配器并在那里设置holder(视图)背景色。

当然,您可以很容易地通过XML实现这一点。

有关详细信息,请参阅:http://www.vogella.com/tutorials/AndroidListView/article.html#tutorial_ownadapter

如果你有什么问题,或者我没有理解你,请随时发表评论。

当您调用listView.invalidateViews();时,它将使用适配器的getView()方法并绘制视图。如果您想更改ListView中视图的背景色,最好的方法是在适配器的getView()中进行更改。

@Override
  public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
    rowView.setBackgroundColor(0xFFA6D2FF);
    return rowView;
  }

据我所知。这应该通过在自定义适配器的getView方法中创建自定义视图来完成。

公共类MyAdapter扩展BaseAdapter{

     public Activity activity;
     ArrayList<String> data = new ArrayList<String>();
     private static LayoutInflater inflater = null;
     public MyAdapter(Activity a, ArrayList<String> d) {
      activity = a;
      data = d;
      inflater = LayoutInflater.from(activity);
      }    
   @Override
   public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.my_list_item, null);
            holder = new ViewHolder();
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }
   convertView.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
           //Touch on view handle
         Log.d("", "Touched row "+position);
      }
   });
   //customizing view
    holder.textView = (TextView)convertView.findViewById(R.id.my_textview);
   holder.textView.setText(data.get(position));
   return convertView;
 }       
  public static class ViewHolder {
     public TextView textView;
  }
 @Override
  public int getCount() {
     return data.size();
  }
 @Override
 public Object getItem(int position) {
     return position;
 }
@Override
 public long getItemId(int position) {
     return position;
}

}

列表视图中的每一行的背景

最新更新