1

我正在使用 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");
    }

}

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

有人可以帮忙吗?我需要创建新的 TextView id 并指定 textColor 吗?我试过但无法弄清楚。谢谢。

4

3 回答 3

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

    };
于 2013-01-29T14:10:46.103 回答
0

感谢 Anand,该语法看起来比创建新类更简洁。我不知道你可以重写方法而不必创建派生类,这是 c++ 所没有的。

但是,您的代码在 getChildView 中略有错误,它会崩溃。以下作品

 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)
            {
                final View itemRenderer = super.getChildView(groupPosition, childPosition, isLastChild, convertView, parent);
                final TextView tv = (TextView)itemRenderer.findViewById(android.R.id.text2);
                tv.setTextColor(Color.BLACK);
                return itemRenderer;
            }

            @Override
            public  View    getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
                TextView tv = (TextView) super.getGroupView(groupPosition, isExpanded, convertView, parent);
                tv.setTextColor(Color.BLUE);
                return tv;
            }
        };
于 2013-01-30T01:05:49.310 回答
0

我想出了一种方法

我像这样创建了 SimpleExpandableListAdapter 的派生类(或 java 所说的任何东西)

class MySimpleExpandableListAdapter extends SimpleExpandableListAdapter {

        public MySimpleExpandableListAdapter(Context context,
                List<? extends Map<String, ?>> groupData, int groupLayout,
                String[] groupFrom, int[] groupTo,
                List<? extends List<? extends Map<String, ?>>> childData,
                int childLayout, String[] childFrom, int[] childTo) {
            super(context, groupData, groupLayout, groupFrom, groupTo, childData,
                    childLayout, childFrom, childTo);
            // TODO Auto-generated constructor stub
        }

        @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 tv2 = (TextView)itemRenderer.findViewById(android.R.id.text2);
            tv2.setTextColor(0xff0000ff);
            return itemRenderer;
        }

        @Override
        public  View    getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent){

            final View itemRenderer = super.getGroupView(groupPosition, isExpanded, convertView, parent);
            final TextView tv = (TextView)itemRenderer.findViewById(android.R.id.text1);
            tv.setTextColor(0xff00ff00);
            return itemRenderer;
         }
    }

这里的关键是 getChildView 和 getGroupView 的覆盖,我猜它允许我弄乱 android.R.id.text1 和 2 资源。我怀疑这是最有效的方法,但它似乎有效。

于 2013-01-29T14:10:25.593 回答