2011-05-10 66 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