如何更改SimpleExpandableListAdapter文本颜色 [英] how to change text color in SimpleExpandableListAdapter

查看:106
本文介绍了如何更改SimpleExpandableListAdapter文本颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ExpandableListFragment创建TabHost片段内的ExpandableList

i am using ExpandableListFragment to create an ExpandableList inside a TabHost fragment

我得到了code为ExpandableListFragment从这里 https://gist.github.com/1316903

i got the code for ExpandableListFragment from here https://gist.github.com/1316903

这似乎做工精细但我无法弄清楚如何改变文字颜色。

it seems to work fine except i cannot figure out how to change the text color.

我的实现看起来像这样

public class LocalListFragment extends ExpandableListFragment {

    private static final String NAME = "NAME";
    private static final String IS_EVEN = "IS_EVEN";

    private ArrayList<String> mGroups;
    private ArrayList<ArrayList<Song>> mChildren;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        // Set up our adapter

        List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
        List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
        for (int i = 0; i < 10; i++) {
            Map<String, String> curGroupMap = new HashMap<String, String>();
            groupData.add(curGroupMap);
            curGroupMap.put(NAME, "GROUP" + i);
            //curGroupMap.put(IS_EVEN, (i % 2 == 0) ? "This group is even" : "This group is odd");

            List<Map<String, String>> children = new ArrayList<Map<String, String>>();
            for (int j = 0; j < 5; j++) {
                Map<String, String> curChildMap = new HashMap<String, String>();
                children.add(curChildMap);
                curChildMap.put(IS_EVEN, "Child " + j);
            }
            childData.add(children);
        }


        mAdapter = new SimpleExpandableListAdapter(
            getActivity().getApplicationContext(),
            groupData,
            android.R.layout.simple_expandable_list_item_1,
            new String[] { NAME, IS_EVEN },
            new int[] { android.R.id.text1, android.R.id.text2 },
            childData,
            android.R.layout.simple_expandable_list_item_2,
            new String[] { NAME, IS_EVEN },
            new int[] { android.R.id.text1, android.R.id.text2 }
        );

        //TextView tv = (TextView)getActivity().findViewById(android.R.id.text1);
        //tv.setTextColor(0xffff0000);
        setListAdapter(mAdapter);

        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        Log.i("TAG", "Item selected");
    }

}

我的猜测是,该行新INT [] {android.R.id.text1,android.R.id.text2},有很多事情做,但我有点糊涂了这一点。

my guess is that the lines 'new int[] { android.R.id.text1, android.R.id.text2 },' has a lot to do with it but i'm a bit confused about this.

有人可以请帮助?我是否需要创造新的TextView的id,并指定文字颜色?我试过,但不能弄明白。谢谢。

can someone please help? do i need to creat new TextView id's and specify textColor? i tried but couldn't figure it out. thanks.

推荐答案

列表适配器只是orverride getview方式:

just orverride getview method of list adapter :

mAdapter = new SimpleExpandableListAdapter(
            getActivity().getApplicationContext(),
            groupData,
            android.R.layout.simple_expandable_list_item_1,
            new String[] { NAME, IS_EVEN },
            new int[] { android.R.id.text1, android.R.id.text2 },
            childData,
            android.R.layout.simple_expandable_list_item_2,
            new String[] { NAME, IS_EVEN },
            new int[] { android.R.id.text1, android.R.id.text2 }){


                @Override
                public View getChildView(int groupPosition, int childPosition,
                        boolean isLastChild, View convertView, ViewGroup parent) {

                    TextView tv =  (TextView) super.getChildView(groupPosition, childPosition, isLastChild,convertView, parent);
                    //change background of tv here
                    return tv;
                }

                @Override
                public View getGroupView(int groupPosition, boolean isExpanded,
                        View convertView, ViewGroup parent) {
                    // TODO Auto-generated method stub
                    TextView tv = (TextView) super.getGroupView(groupPosition, isExpanded, convertView, parent);
                    //change background of tv here
                    return tv;
                }

    };

这篇关于如何更改SimpleExpandableListAdapter文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆