可扩展的列表视图组标题颜色更改

时间:2016-09-29 12:32:56

标签: android android-layout

我在exapandable listview中为备用组头设置了两种颜色。但是,当我多次单击以展开或折叠颜色更改为任何组行时。

这是我的代码,

 if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_group, null);

        if(groupPosition % 2 == 1) {
            convertView.setBackgroundColor(Color.parseColor("#3C3C3C"));
        }else {
            convertView.setBackgroundColor(Color.parseColor("#000000"));
        }
    }

滚动列表后会发生这种情况。我也试过这个

private int[] colors = new int[] { Color.parseColor("#000000"), Color.parseColor("#3C3C3C") };
int colorPos = groupPosition % colors.length;
convertView.setBackgroundColor(colors[colorPos]);

3 个答案:

答案 0 :(得分:3)

if(groupPosition % 2 == 0) {
            convertView.setBackgroundColor(Color.parseColor("#3c3c3c"));
        }else {
            convertView.setBackgroundColor(Color.parseColor("#000000"));
        }

将此代码置于if(convertView == null)此条件之外

答案 1 :(得分:1)

你可以试试这个:

if (groupPosition % 2 == 0) 
{
    convertView.setBackgroundColor(Color.parseColor("#3C3C3C"));
} 
else
{
    convertView.setBackgroundColor(Color.parseColor("#000000"));
}

答案 2 :(得分:1)

待办事项

if(groupPosition % 2 == 0)

而不是

if(groupPosition % 2 == 1)

并且如果条件超出convertView == null检查

if (convertView == null) {
    LayoutInflater infalInflater = (LayoutInflater) this._context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = infalInflater.inflate(R.layout.list_group, null);
}

if(groupPosition % 2 == 0) {
      convertView.setBackgroundColor(Color.parseColor("#3C3C3C")); 
  }else {
      convertView.setBackgroundColor(Color.parseColor("#000000"));
  }