【问题标题】:Android: Access TextView inside Child of ExpandableListViewActivityAndroid:在 ExpandableListView Activity 的子项中访问 TextView
【发布时间】:2011-05-10 09:54:30
【问题描述】:

我正在通过 ExpandableListViewActivity 使用 ExpandableList。

现在,我想更改每个 ChildView 中某个 TextView 的文本颜色。我不希望它们都一样,而是像绿色的“ok”和红色的“error”等。

我确实偶然发现了 getExpandableListAdapter().getChildView(...),但我不确定最后一个参数(View convertView、ViewGroup parent)应该是什么。另外我不太清楚是否需要将返回值(视图)转换成什么东西?

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>

【问题讨论】:

    标签: java android layout expandablelistview textcolor


    【解决方案1】:

    您应该覆盖ExpandableListAdapter 实现的getChildView 方法,并在其中根据此适配器可访问的任何标志设置文本颜色。

    如果更改是显式的,请不要忘记在更改标志后在适配器上调用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;
        }
    };
    

    更新
    着色的问题在于你的资源文件,你需要为文本颜色设置alpha值,所以你的红色应该是#FFFF3523,你的绿色:#FFA2CD5A

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

    【讨论】:

    • 对不起,我不明白...什么标志?为什么要getChildView?我使用“setListAdapter”时是否总是调用它? atm我只使用默认的SimpleExpandableListAdapter,所以对自定义Adapter不太熟悉。
    • 您也可以覆盖SimpleExpandableListAdaptergetChildView 方法。使用super.getChildView 检索自动项目渲染器,然后调用findViewById(R.id.your_colored_textview) 访问您要为其分配新颜色的文本视图。 getChildView 方法在子项即将显示或更改需要刷新时调用。
    • 请参阅我的更新以获取示例实现。 (另外:你应该只给setListAdapter打一次电话!)
    • 哇,非常感谢!但是,我没有让颜色工作......文本只是变成黑色(所以你甚至无法阅读它)。我已经在上面发布了我的代码。颜色资源没问题,我检查了 XML 文件。
    • 感谢您的补充,请参阅我的更新以了解正确的文本颜色定义。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-27
    • 1970-01-01
    • 2015-01-10
    • 2015-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多