2011-12-01 68 views
0

Iam尝试为可扩展列表视图中的组设置动态背景。所以在我的listviewAdapter我有以下代码:可扩展列表视图组的动态背景颜色不起作用

@Override 
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 

    if (convertView == null) { 
     LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = infalInflater.inflate(R.layout.elv_group, parent, false); 
    } 

    ((LinearLayout) convertView.findViewById(R.id.elv_group_root_layout)) 
      .setBackgroundColor(getBackgroundColor(groupPosition)); 

    return convertView; 
} 

public int getBackgroundColor(int groupPosition) { 
    if (getGroup(groupPosition).getInput().size() != getGroup(groupPosition).getOutput().size()) { 
     return R.color.attention_row; 
    } else { 
     return R.color.normal_row; 
    } 
} 

正如你所看到的,我尝试设置取决于方法getBackgroundColor给定的语句中的根布局的背景颜色。

但我得到的是一个总是灰色背景的组列表!任何人都可以告诉我什么Iam在这里做错了吗?似乎与Android列表生命周期或缓存机制的一些问题。 在可扩展列表视图中是否存在改变组的linearLayout颜色的问题?是否有其他方式突出特定的组?

+0

确定getBackgroundColor曾经返回attention_row颜色? – zmbq

+0

是的,getBackgroundColor确实返回正确的颜色(除灰色外,将显示!) – Lars

回答

1
convertView.setBackgroundResource(getBackgroundColor(groupPosition)); 

代替

((LinearLayout) convertView.findViewById(R.id.elv_group_root_layout)).setBackgroundColor(getBackgroundColor(groupPosition)); 

救了我的命;) 感谢this post