如何在Android中的不同可扩展ListView组中具有不同的背景颜色

时间:2016-05-28 08:59:48

标签: android listview

我有一个Tab Layout,每个布局都有一个可扩展的ListView,每个ListView都有它的BaseExpandableListAdapter。我希望每个小组都有不同的背景。我应该如何更改我的适配器来实现这一目标?

public View getGroupView(int listPosition, boolean isExpanded, View convertView, ViewGroup parent)
{
    String listTitle = (String) getGroup(listPosition);
    if (convertView == null)
    {
        LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.off_group, null);
    }
    TextView listTitleTextView = (TextView) convertView.findViewById(R.id.listTitle);
    listTitleTextView.setTypeface(null, Typeface.BOLD);
    listTitleTextView.setText(listTitle);
    return convertView;
}

我尝试了建议,并且我全局声明了带有十六进制颜色的int颜色数组!喜欢

private int[] colors = {Color.parseColor("#b8dbd3"), Color.parseColor("#ffe975"), Color.parseColor("#dbdcff"), Color.parseColor("#f5f5f5"), Color.parseColor("#ccd6dd")};

public View getGroupView(int listPosition, boolean isExpanded, View convertView, ViewGroup parent)
{
    String listTitle = (String) getGroup(listPosition);
    if (convertView == null)
    {
        LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.off_group, null);
    }
    Random ranndom = new Random();
    int ranndomColor = ranndom.nextInt(5);
    LinearLayout GroupLayout = (LinearLayout) convertView.findViewById(R.id.ParentGroup);
    GroupLayout.setBackgroundResource(colors[ranndomColor]);

    TextView listTitleTextView = (TextView) convertView.findViewById(R.id.listTitle);
    listTitleTextView.setTypeface(null, Typeface.BOLD);
    listTitleTextView.setText(listTitle);
    return convertView;
}

但我在资源上遇到错误

FATAL EXCEPTION: 
main android.content.res.Resources$NotFoundException: Resource ID #0xffffff00

2 个答案:

答案 0 :(得分:0)

试试这个:

 private int[] colors = {Color.RED, Color.YELLOW, Color.BLUE, Color.CYAN, Color.GREEN};

 Random ranndom = new Random();
 int ranndomColor = ranndom.nextInt(5);
 whatever.setBackground(colors[ranndomColor]);

答案 1 :(得分:0)

答案是

LinearLayout GroupLayout = (LinearLayout) convertView.findViewById(R.id.ParentGroup);
    GroupLayout.setBackgroundColor(colors[listPosition]); 

谢谢