ExpandableListAdapter

来源:互联网 发布:360软件应用中心 编辑:程序博客网 时间:2024/06/06 14:00

1在布局文件中声明expandablelistActivity

<ExpandableListView android:id="@id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:drawSelectorOnTop="false" 
       
       />
    <TextView
        android:id="@id/android:empty"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="No data" />

2在布局文件中声明group式样

group.xml

<TextView android:id="@+id/groupTo"

    android:layout_width="fill_parent"

    android:lyaout_height="fill_parent"

    android:paddingLeft="60px"

   android:paddingTop="10px"

   android:paddingBottom="10px"

   android:textSize="26sp"

   android:text="Nodata"/>

3 布局文件中声明child式样

  <TextView android:id="@+id/childTo"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:paddingLeft="50px"

   android:paddingTop="5px"

   android:paddingBottom="5px"

   android:textSize="20sp"

   android:text="Nodata"/>

4定义list 为一级条目提供数据

List<Map<String,String>> groups = new ArrayList<Map<String,String>>();
         Map<String,String> group1 = new HashMap<String,String>();
         group1.put("group", "group1");
         Map<String,String> group2 = new HashMap<String,String>();
         group2.put("group", "group2");
         groups.add(group1);
         groups.add(group2);

5 定义list为一级条目提供二条二级条目数据

//定义一个list,该list为第一个一级条目提供二级条目的数据
         List<Map<String,String>> child1 = new ArrayList<Map<String,String>>();
         Map<String,String> child1data1 = new HashMap<String,String>();
         child1data1.put("child", "child1data1");
         child1.add(child1data1);
         Map<String,String> child1data2 = new HashMap<String,String>();
         child1data2.put("child", "child1data2");
         child1.add(child1data2);
        // 定义一个List,该List对象为第二个一级条目提供二级条目的数据
         List<Map<String,String>> child2 = new ArrayList<Map<String,String>>();
         Map<String,String> child2data = new HashMap<String,String>();
         child2data.put("child", "child2data");
         child2.add(child2data);
          //定义一个List,该List对象用来存储所有的二级条目的数据
        List<List<Map<String,String>>> childs = new ArrayList<List<Map<String,String>>>();
        childs.add(child1);
        childs.add(child2);

6 定义simpleExpandableListAdapter对象为ExpandableListAdapter提供数据

SimpleExpandableListAdapter sela = new SimpleExpandableListAdapter(
         this,groups,R.layout.group,new String[]{"group"},new int[]{R.id.groupTo},childs,
         R.layout.child,new String[]{"child"},new int[]{R.id.childTo});
         setListAdapter(sela);