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