Expandablelistview

MainActivity.java

package com.example.expandablelistview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.widget.SimpleExpandableListAdapter;

/*实现可展开列表视图实例 * 需要注意的是必须在layout中创建group.xml,child.xml * */
public class MainActivity extends ExpandableListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

// 创建一个第一级条目的数据
        List<Map<String, String>> groups =  new ArrayList<Map<String,String>>();
        Map<String, String> group1 = new HashMap<String, String>();
        Map<String, String> group2 = new HashMap<String, String>();
        group1.put("group", "我");
        group2.put("group", "是");
        groups.add(group1);
        groups.add(group2);

// 创建第一个一级条目的第二级条目(子条目)的数据
        List<Map<String, String>> child1 = new ArrayList<Map<String,String>>();
        Map<String, String> child1data1 = new HashMap<String, String>();
        Map<String, String> child1data2 = new HashMap<String, String>();
        child1data1.put("child", "2015图书馆");
        child1data2.put("child", "人数统计表");
        child1.add(child1data1);
        child1.add(child1data2);

// 创建第二个一级条目的第二个条目(子条目)的数据
        List<Map<String, String>> child2 = new ArrayList<Map<String,String>>();
        Map<String, String> child2data1 = new HashMap<String, String>();
        Map<String, String> child2data2 = new HashMap<String, String>();
        child2data1.put("child", "序号");
        child2data2.put("child", "地点");
        child2.add(child2data1);
        child2.add(child2data2);

// 创建一个可以用于存放子列表的列表
        List<List<Map<String, String>>> childs = new ArrayList<List<Map<String,String>>>();
// 将child1,child2当做两个子列表,添加到childs父列表中
        childs.add(child1);     
        childs.add(child2);

// 设置一个适配器
// 第一个参数content 第二个参数指定第一级条目的数据列表 第三个参数指定第一级列表的显示样式 第四个参数指定第一级条目数据的key 第五个参数指定第一级条目显示控件的ID
// 第六个参数指定第二级条目的数据列表 第七个参数指定第二级列表的显示样式 第八个参数指定第二级条目数据的key 第九个参数指定第二级条目显示控件的ID 
        SimpleExpandableListAdapter SimpleEXadapter = new SimpleExpandableListAdapter(MainActivity.this, 
                groups, R.layout.group, new String[]{"group"}, new int[]{R.id.tv_group}, childs, R.layout.child, new String[]{"child"},new int[]{R.id.tv_child}); // 执行ExpandableListView.setListAdapter为在 activity_main的expandablelistview设置适配器 setListAdapter(SimpleEXadapter); } } 

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >

    <ExpandableListView android:id="@id/android:list" android:layout_width="match_parent" android:layout_height="match_parent" android:drawSelectorOnTop="false"/>

    <TextView android:id="@id/android:empty" android:layout_width="match_parent" android:layout_height="match_parent" android:text="no data"/>

</LinearLayout>

child.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >

    <TextView android:id="@+id/tv_child" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="no data"/>


</LinearLayout>

group.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >

    <TextView android:id="@+id/tv_group" android:layout_width="match_parent" android:layout_height="wrap_content"/>


</LinearLayout>

你可能感兴趣的:(Expandablelistview)