如何在SimpleExpandableListAdapter中更改文本颜色

ste*_*veh 1 android expandablelistadapter

我正在使用ExpandableListFragment在TabHost片段内创建一个ExpandableList

我从这里获得了ExpandableListFragment的代码https://gist.github.com/1316903

它似乎工作正常,除了我无法弄清楚如何更改文本颜色.

我的实现看起来像这样

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");
    }

}
Run Code Online (Sandbox Code Playgroud)

我的猜测是'new int [] {android.R.id.text1,android.R.id.text2},'与它有很多关系,但我对此有点困惑.

有人可以帮忙吗?我需要创建新的TextView ID并指定textColor吗?我试过但无法弄明白.谢谢.

小智 6

只是列表适配器的orverride getview方法:

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;
                }

    };
Run Code Online (Sandbox Code Playgroud)