2011-12-01 66 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