您的位置:首页 > 其它

仿qq联系人 学习笔记---ExpandableListActivity的使用

2016-04-25 17:37 260 查看
【转】原地址 http://blog.163.com/xygzx@126/blog/static/237809502011102010100331/
效果显示图:



1.布局文件

main.xml(ExpandableListActivity布局文件)

注意事项:

ExpandableListActivity的布局文件中必须包含一个ExpandableListView,并且id必须为="@id/android:list"。还可以包含一个id为empty的TextView,在ExpandableListActivity中没有数据的时候显示该控件的text值。

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ExpandableListView android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false">
</ExpandableListView>
<TextView
android:id="@id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="没有数据">
</TextView>
</LinearLayout>


group.xml(一级条目布局文件,样式外观可根据需要自由发挥)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="@+id/groupTo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="60dip"
android:paddingTop="10dip"
android:paddingBottom="10dip"
android:text="No Date"
android:textSize="20sp">
</TextView>
</LinearLayout>


child.xml(二级条目布局文件,样式外观可根据需要自由发挥)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="@+id/childTo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="50dip"
android:paddingTop="5dip"
android:paddingBottom="5dip"
android:textSize="20sp"
android:text="No Date">
</TextView>
</LinearLayout>


2.JAVA代码

package com.test;

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;
public class Sample_ExpandableListActivityActivity extends ExpandableListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//一级条目
List<Map<String, String>>groups=new ArrayList<Map<String,String>>();
Map<String, String> group1=new HashMap<String, String>();
group1.put("group","第一组");
Map<String, String> group2=new HashMap<String, String>();
group2.put("group", "第二组");
groups.add(group1);
groups.add(group2);
//二组条目
List<List<Map<String, String>>> childs=new ArrayList<List<Map<String,String>>>();
//第一组二级科目数据
List<Map<String, String>> child1=new ArrayList<Map<String,String>>();
Map<String, String> item1=new HashMap<String, String>();
item1.put("child","科目-1");
Map<String, String> item2=new HashMap<String, String>();
item2.put("child","科目-2");
child1.add(item1);
child1.add(item2);
//第二组二级科目数据
List<Map<String, String>> child2=new ArrayList<Map<String,String>>();
Map<String, String> item3=new HashMap<String, String>();
item3.put("child","科目-3");
Map<String, String> item4=new HashMap<String, String>();
item4.put("child","科目-4");
Map<String, String> item5=new HashMap<String, String>();
item5.put("child","科目-5");
child2.add(item3);
child2.add(item4);
child2.add(item5);
childs.add(child1);
childs.add(child2);
//SimpleExpandableListAdapter构造函数参数
//1.content
//2.一级条目数据
//3.一级条目布局文件
//4.一级条目Key
//5.一级条目显示信息控件id
//6.二级条目数据
//7.二级条目布局文件
//8.二级条目Key
//9.二级条目显示信息控件id
SimpleExpandableListAdapter adapter=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(adapter);

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: