【问题标题】:How can I set different background color for each row in listview?如何为列表视图中的每一行设置不同的背景颜色?
【发布时间】:2012-05-15 18:50:59
【问题描述】:

我想在 listview 的每一行设置不同的背景颜色?我为listview 使用了自定义适配器。应该会在activity加载的时候出现.static不同颜色的行。

【问题讨论】:

    标签: android listview


    【解决方案1】:

    getView(...) method

    if (position == 0) {
        view.setBackgroundResource(R.drawable.bg_list_even);
    } else if (position == 1) {
        view.setBackgroundResource(R.drawable.bg_list_odd);
    } else...
    

    更新::

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = convertView;
        ViewHolder holder;
    
        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
            view = inflater.inflate(R.layout.row, null);
    
            holder = new ViewHolder();
            view.setTag(holder);
    
        } else {
            holder = (ViewHolder) view.getTag();
        }
    
        holder.title = (TextView) view.findViewById(R.id.txttitle);
        holder.description = (TextView) view.findViewById(R.id.txtdesc);
    
        holder.title.setText("Title" + position);
        holder.description.setText("Desc" + position);
    
        //here set your color as per position
    
        if (position == 0) {
            view.setBackgroundResource(R.drawable.bg_list_even);
        } else if (position == 1) {
            view.setBackgroundResource(R.drawable.bg_list_odd);
        }
        return view;
    }
    

    持有人类

    public class ViewHolder {
    
        public TextView title;
        public TextView description;
    }
    

    【讨论】:

    • 这是一个很好的解决方案... // 偶数行和奇数行... if ((position % 2) == 0) { view.setBackgroundResource(R.drawable.bg_list_even); } else { view.setBackgroundResource(R.drawable.bg_list_odd); } 你的网站上有很好的解决方案!
    【解决方案2】:

    如下所示创建一个数组作为列表项的编号,我想你有五个项目

     int[] color_arr={Color.BLUE,Color.CYAN,Color.DKGRAY,Color.GREEN,Color.RED};
    

    然后在你的自定义适配器的getView方法中做如下

     public View getView(int position, View convertView, ViewGroup parent)
         {
    
         LayoutInflater inflater = getLayoutInflater();
         View row=convertView;
    
         row = inflater.inflate(R.layout.listview_custome, parent, false);
         row.setBackgroundColor(color_arr[position]);// this set background color
    
         TextView textview = (TextView) row.findViewById(R.id.tv_list);
         ImageView imageview = (ImageView) row.findViewById(R.id.iv_list);
    
         textview.setText(data_text[position]);
         imageview.setImageResource(data_image[position]);
    
         return (row);
    
        }
    

    【讨论】:

    • 非常感谢您的宝贵建议。
    • 谢谢,如果有超过 5 种商品,颜色只有 5 种,怎么可能?
    【解决方案3】:

    正如您所说,您已经为列表视图使用了自定义适配器,那么您需要做的如下。 在适配器的getView 方法中,您需要设置列表行xml 父视图的背景颜色。

    【讨论】:

    • 我听不懂你在说什么。
    • 我认为你需要检查@Samir 更新,你会得到答案。
    【解决方案4】:
    public View getView(int position, View convertView, ViewGroup parent) {
    
        LayoutInflater inflater = getLayoutInflater();
        View rowView = convertView;
    
        rowView = inflater.inflate(R.layout.listview_custome, parent, false);
        rowView.setBackgroundColor(color_arr[position]);// this set background color
    
        TextView textview = (TextView) rowView.findViewById(R.id.tv_list);
        ImageView imageview = (ImageView) rowView.findViewById(R.id.iv_list);
    
        textview.setText(data_text[position]);
        imageview.setImageResource(data_image[position]);
        if (position == 0) {
            rowView.setBackgroundColor(Color.BLUE);
        }
        else if (position % 2 == 1) {
            rowView.setBackgroundColor(Color.RED);
        }
        else if (position % 2 == 0) {
            rowView.setBackgroundColor(Color.BLUE);
        }
        return (rowView);
    
    }
    

    【讨论】:

      猜你喜欢
      • 2015-11-16
      • 1970-01-01
      • 1970-01-01
      • 2011-11-18
      • 2011-01-11
      • 2016-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多