神图压镇!
ExpandableListActivity
1.生成一个新的activity要继承ExpandableListActivity
public class MainActivity extends ExpandableListActivity
2.为一级列表和二级列表,一级主activity分别创建布局文件。
2.1main的布局文件中主要是要声明总的acivity的布局,需要声明
ExpandableListActivity控件:
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false">
android:id="@id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="no
data"/>
2.2 fistlayout中主要是一级列表的显式格式:
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 data" />
2.3 sendlayout是二级子列表的显示格式:
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="15sp"
android:paddingLeft="50px"
android:paddingTop="5px"
android:paddingBottom="5px"
android:text="no data"
android:id="@+id/secondId"
/>
3.生成了三个布局文件之后,然后就是在主代码中为列表准备数据,而ExpandableListActivity的数据和布局文件的样式来源主要是SimpleExpandableListAdapter。
具体的数据声明过程为:
//定义一个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 child1Data1 = new HashMap();
child1Data1.put("child", "child1Data1");
child1.add(child1Data1);
Map child1Data2 = new HashMap();
child1Data2.put("child", "child1Data2");
child1.add(child1Data2);
//定义一个List,该List对象为第二个一级条目提供二级条目的数据
List> child2 = new ArrayList>();
Map child2Data = new HashMap();
child2Data.put("child", "child2Data");
child2.add(child2Data);
//定义一个List,该List对象用来存储所有的二级条目的数据
List>> childs = new ArrayList>>();
childs.add(child1);
childs.add(child2);
//生成一个SimpleExpandableListAdapter对象
//1.context
//2.一级条目的数据
//3.用来设置一级条目样式的布局文件
//4.指定一级条目数据的key
//5.指定一级条目数据显示控件的id
//6.指定二级条目的数据
//7.用来设置二级条目样式的布局文件
//8.指定二级条目数据的key
//9.指定二级条目数据显示控件的id
SimpleExpandableListAdapter sela = new SimpleExpandableListAdapter(
this, groups, R.layout.fistlayout, new String[] { "group" },
new int[] { R.id.firstId }, childs, R.layout.sendlayout,
new String[] { "child" }, new int[] { R.id.secondId });
//将SimpleExpandableListAdapter对象设置给当前的ExpandableListActivity
setListAdapter(sela);
总结:在声明布局文件的时候,一开始将textview嵌套在了ExpandableListView中,导致尝试很多次之后,app一直运行退出,感觉被坑了,将mars老师的源代码拷贝过来然后运行还是出错,后来才发现问题在布局文件里。。。真是一个大坑啊。
Listview中的一级列表和二级列表没有明显的配对的关系,就是没有明确的指出谁是谁的下级,而是在为而是从一级中拿一个,从二级中拿一个(而二级是一个list的list,因此一个事实上有一串),然后依次放好。。。