记录下android 中SimpleExpandableListAdapter的使用。直接上代码,有详细的注释
MainActivity.java
package com.yx.expandablelistview;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.app.Activity;
import android.app.ExpandableListActivity;
import android.view.Menu;
import android.widget.SimpleExpandableListAdapter;
public class MainActivity extends ExpandableListActivity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//定义一个list该list是为一级条目提供数据
List> groups = new ArrayList>();
Map group1 = new HashMap();
group1.put("group", "group1");
Map group2 = new HashMap();
group2.put("group", "group2");
groups.add(group1);
groups.add(group2);
//定义一个list该list是为第一个一级条目提供二级条目数据
List> child1 = new ArrayList>();
Map child1Date1 = new HashMap();
child1Date1.put("child", "child1Date1");
Map child1Date2 = new HashMap();
child1Date2.put("child", "child1Date2");
child1.add(child1Date1);
child1.add(child1Date2);
//定义一个list该list是为第二个一级条目提供二级条目数据
List> child2 = new ArrayList>();
Map child2Date1 = new HashMap();
child2Date1.put("child", "child2Date1");
Map child2Date2 = new HashMap();
child2Date2.put("child", "child2Date2");
child2.add(child2Date1);
child2.add(child2Date2);
//定义一个list,该list用于存放所有的二级条目的数据
List>> childs = new ArrayList>>();
childs.add(child1);
childs.add(child2);
//生成一个SimpleExpandableListAdapter对象
//1.context,2.以及条目的数据3.用来设置以及条目样式的布局文件4.指定以及条目的key
//5.指定一级条目数据显示控件的id6.指定二级条目的数据7.设置二级条目样式的布局文件8.指定二级条目数据的key9.指定二级条目数据显示的id
SimpleExpandableListAdapter simpleExpandableListAdapter = 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(simpleExpandableListAdapter);
}
}
在这里需要三个xml布局文件
activity_main.xml主布局文件,主要是负责整体可下拉的视图的位置
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
>
android:id="@id/android:empty"
android:text="no date"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
group.xml 这个布局文件主要负责对最顶级的条目进行样式的调整
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:id="@+id/groupTo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="60px"
android:paddingTop="10px"
android:paddingBottom="10px"
android:textSize="26sp"
android:text="No date"
/>
child.xml这个布局文件主要是对每个大级下面的小条目进行样式的调整
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
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="No date"
/>