Android中的可扩展列表

Android中的可扩展列表,android,Android,我想实现一个可扩展的ImageListView _exlist=getExpandableListView(); final String NAME = "name"; final String IMAGE = "image"; final LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); final A

我想实现一个可扩展的ImageListView

_exlist=getExpandableListView();

    final String NAME = "name";
    final String IMAGE = "image";
    final LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final ArrayList<HashMap<String, String>> headerData = new ArrayList<HashMap<String, String>>();

    final HashMap<String, String> group1 = new HashMap<String, String>();
    group1.put(NAME, "Group 1");

    headerData.add( group1 );

    final HashMap<String, String> group2 = new HashMap<String, String>();
    group2.put(NAME, "Group 2");
    headerData.add( group2);


    final ArrayList<ArrayList<HashMap<String, Object>>> childData = new ArrayList<ArrayList<HashMap<String, Object>>>();

    final ArrayList<HashMap<String, Object>> group1data = new ArrayList<HashMap<String, Object>>();
    childData.add(group1data);

    final ArrayList<HashMap<String, Object>> group2data = new ArrayList<HashMap<String, Object>>();
    childData.add(group2data);


    // Set up some sample data in both groups
    for( int i=0; i<10; ++i) {
        final HashMap<String, Object> map = new HashMap<String,Object>();
        map.put(NAME, "Child " + i );
        map.put(IMAGE, getResources().getDrawable(R.drawable.icon));
        ( i%2==0 ? group1data : group2data ).add(map);
    }

    setListAdapter( new SimpleExpandableListAdapter(
            this,
            headerData,
            android.R.layout.simple_expandable_list_item_1,
            new String[] { NAME },            // the name of the field data
            new int[] { android.R.id.text1 }, // the text field to populate with the field data
            childData,
            0,
            null,
            new int[] {}
        ) {
            @Override
            public ViewGroup getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
                final ViewGroup v = (ViewGroup) super.getChildView(groupPosition, childPosition, isLastChild, convertView, parent);

                // Populate your custom view here
                ((TextView)v.findViewById(R.id.name)).setText( (String) ((Map<String,Object>)getChild(groupPosition, childPosition)).get(NAME) );
                ((ImageView)v.findViewById(R.id.image)).setImageDrawable( (Drawable) ((Map<String,Object>)getChild(groupPosition, childPosition)).get(IMAGE) );

                return v;
            }

            @Override
            public ViewGroup newChildView(boolean isLastChild, ViewGroup parent) {
                 return (ViewGroup) layoutInflater.inflate(R.layout.list, null, false);
            }
        }
    );
\u exlist=getExpandableListView();
最终字符串NAME=“NAME”;
最终字符串IMAGE=“IMAGE”;
final LayoutInflater LayoutInflater=(LayoutInflater)this.getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);
最终ArrayList headerData=新ArrayList();
final HashMap group1=新HashMap();
集团1.出售(名称为“集团1”);
headerData.add(第1组);
final HashMap group2=新HashMap();
第2组:出售(名称为“第2组”);
headerData.add(第2组);
final ArrayList childData=新ArrayList();
最终ArrayList group1data=新ArrayList();
添加(group1data);
最终ArrayList group2data=新ArrayList();
添加(group2data);
//在两个组中设置一些示例数据
对于(int i=0;i您可以覆盖

public View getGroupView(int groupPosition, boolean isExpanded, 
    View convertView, ViewGroup parentView)
方法,以及(如有必要)使用
newGroupView方法,并像处理子视图一样操作组视图

该示例使用
BaseExpandableListAdapter
扩展来填充可扩展列表视图,但上述方法对这两种视图都是通用的