我通過ExpandableListViewActivity使用ExpandableList。Android:訪問ExpandableListViewActivity中的子視圖TextView
現在,我想改變每個ChildView中某個TextView的文本顏色。我不希望他們都是相同的,而是像綠色的「好」和紅色的「錯誤」等
我確實碰到getExpandableListAdapter()。getChildView(...),但我' m確定最後的參數(View convertView,ViewGroup parent)應該是什麼。此外,我不知道是否需要將返回值(View)轉換爲某種東西?
TextView tv = (TextView)this.getExpandableListAdapter().getChildView(0, 0, false, ???, ???).findViewById(R.id.xlist_child_tv);
親切的問候, 水母
解決方案概要
SimpleExpandableListAdapter expListAdapter = new SimpleExpandableListAdapter(...parameters...){
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
{
final View itemRenderer = super.getChildView(groupPosition, childPosition, isLastChild, convertView, parent);
final TextView tv = (TextView) itemRenderer.findViewById(R.id.kd_child_value);
if (tv.getText().toString().contentEquals(getString(R.string.true)))
{
tv.setTextColor(getResources().getColor(R.color.myred));
}
else if (tv.getText().toString().contentEquals(getString(R.string.false)))
{
tv.setTextColor(getResources().getColor(R.color.mygreen));
}
else
{
tv.setTextColor(getResources().getColor(R.color.white));
}
return itemRenderer;
}
};
顏色資源:
<resources>
<color name="white">#ffffffff</color>
<color name="myred">#FFFF3523</color>
<color name="mygreen">#FFA2CD5A</color>
</resources>
對不起,我不明白..什麼標誌?爲什麼getChildView?當我使用「setListAdapter」時,它總是被調用嗎? Atm我只使用默認的SimpleExpandableListAdapter,所以我不太熟悉定製適配器。 – jellyfish 2011-05-10 10:10:37
您也可以覆蓋該'SimpleExpandableListAdapter'的'getChildView'方法。使用'super.getChildView'獲取自動項目渲染器,並在該調用'findViewById(R.id.your_colored_textview)'上訪問要爲其分配新顏色的文本視圖。 'getChildView'方法在子項即將顯示或被更改需要刷新時調用。 – rekaszeru 2011-05-10 10:15:19
請參閱我的更新以獲取示例實現。 (另外:你應該只調用一次'setListAdapter'!) – rekaszeru 2011-05-10 10:22:55