2011-05-10 68 views
0

我通過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> 

回答

2

,你應該重寫你的的getChildView方法3210實現,並在其內部根據此適配器可訪問的任何標誌設置文本顏色。

如果更改是明確的,不要忘記更改標誌後在適配器上調用notifyDatasetChanged()

要覆蓋一個SimpleExpandableListAdaptergetChildView方法,你需要(此處匿名)擴展它:

final SimpleExpandableListAdapter sa = new SimpleExpandableListAdapter(/*your list of 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.text); 
     if (/*check whether the data at groupPosition:childPosition is correct*/) 
      tv.setTextColor(getResources().getColor((R.color.green)); 
     else 
      tv.setTextColor(getResources().getColor((R.color.red)); 
     return itemRenderer; 
    } 
}; 

更新
着色問題就出在你的資源文件,你需要設置阿爾法值也爲文本的顏色,所以你應該是火紅的#FFFF3523,和你的綠色:#FFA2CD5A

<resources> 
    <color name="white">#ffffffff</color> 
    <color name="myred">#FFFF3523</color> 
    <color name="mygreen">#FFA2CD5A</color> 
</resources> 
+0

對不起,我不明白..什麼標誌?爲什麼getChildView?當我使用「setListAdapter」時,它總是被調用嗎? Atm我只使用默認的SimpleExpandableListAdapter,所以我不太熟悉定製適配器。 – jellyfish 2011-05-10 10:10:37

+0

您也可以覆蓋該'SimpleExpandableListAdapter'的'getChildView'方法。使用'super.getChildView'獲取自動項目渲染器,並在該調用'findViewById(R.id.your_colored_textview)'上訪問要爲其分配新顏色的文本視圖。 'getChildView'方法在子項即將顯示或被更改需要刷新時調用。 – rekaszeru 2011-05-10 10:15:19

+0

請參閱我的更新以獲取示例實現。 (另外:你應該只調用一次'setListAdapter'!) – rekaszeru 2011-05-10 10:22:55