我正在通过ExpandableListViewActivity使用ExpandableList。
现在,我想在每个ChildView中更改某个TextView的文本颜色。我不希望它们全部相同,而是像绿色的“确定”和红色的“错误”之类的东西。
我确实偶然遇到了getExpandableListAdapter()。getChildView(...),但是我不确定最后一个参数(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
方法,并在其内部基于此适配器可访问的任何标志设置文本颜色。
如果更改是明显的,请不要忘记在更改标志后在适配器上调用ExpandableListAdapter
!
要覆盖notifyDatasetChanged()
的getChildView
方法,您需要对其进行扩展(此处为匿名):
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;
}
};
更新资料
着色问题出在资源文件中,您还需要为文本颜色设置alpha值,因此红色应为
SimpleExpandableListAdapter
,绿色应为#FFFF3523
:<resources>
<color name="white">#ffffffff</color>
<color name="myred">#FFFF3523</color>
<color name="mygreen">#FFA2CD5A</color>
</resources>
关于java - Android:在ExpandableListViewActivity的子级中访问TextView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5948285/