使用 ExpandableListView 实现折叠ListView
2024-08-23 02:19:29
1:layout/expandablelistview_groups.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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:paddingLeft="40dp"
android:text="标题:" />
<TextView
android:id="@+id/tvEmailTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="承运人自有车辆、船舶及营运资质通知" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:paddingLeft="40dp"
android:text="发件人:" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我"/>
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:paddingLeft="40dp"
android:text="时间:" />
<TextView
android:id="@+id/tvEmailSendTimes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2013-11-11 19:25:16"/>
</LinearLayout>
</LinearLayout>
2:layout/expandablelistview_child.xml 展开文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tvEmailContent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:lineSpacingExtra="2dp"
android:lineSpacingMultiplier="1.2"
android:text="this is content" />
</LinearLayout>
3:MainActivity.java
public class MainActivity extends Activity {
private ExpandableListView expandableListView_one; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); expandableListView_one =(ExpandableListView)findViewById(R.id.expandableListView); //expandableListView.setDivider();这个是设定每个Group之间的分割线。
//expandableListView.setGroupIndicator();这个是设定每个Group之前的那个图标。 //创建二个一级条目标题
Map<String, String> title_1 = new HashMap<String, String>();
Map<String, String> title_2 = new HashMap<String, String>(); title_1.put("tvEmailTitle", "第一封邮件");
title_1.put("tvEmailSendTimes", "2013-11-11 20:08:20"); title_2.put("tvEmailTitle", "第二封邮件");
title_2.put("tvEmailSendTimes", "2013-11-11 20:08:20"); //创建一级条目容器
List<Map<String, String>> groups = new ArrayList<Map<String,String>>();
groups.add(title_1);
groups.add(title_2); //创建二级条目内容
//内容一
Map<String, String> content_1 = new HashMap<String, String>();
String str="您好,R.java是项目中所有资源文件的索引文件,是由系统自动生成,不需要开发者手动的编辑。";
content_1.put("tvEmailContent", str); List<Map<String, String>> childs_1 = new ArrayList<Map<String,String>>();
childs_1.add(content_1); //内容一
Map<String, String> content_2 = new HashMap<String, String>();
content_2.put("tvEmailContent", str); List<Map<String, String>> childs_2 = new ArrayList<Map<String,String>>();
childs_2.add(content_2); //存放两个内容, 以便显示在列表中
List<List<Map<String, String>>> childs = new ArrayList<List<Map<String,String>>>();
childs.add(childs_1);
childs.add(childs_2); /*
使用SimpleExpandableListAdapter显示ExpandableListView
参数1.上下文对象Context
参数2.一级条目目录集合
参数3.一级条目对应的布局文件 (expandablelistview_groups.xml文件
参数4.标题,map中的key,指定要显示的对象
参数5.与参数4对应,指定要显示在groups中的id
参数6.二级条目目录集合
参数7.二级条目对应的布局文件 (expandablelistview_child)
参数8.展开内容,map中的key,指定要显示的对象
参数9.与参数8对应,指定要显示在childs中的id
*/
SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
this,
groups,
R.layout.expandablelistview_groups,
new String[]{"tvEmailTitle", "tvEmailSendTimes"},
new int[]{R.id.tvEmailTitle, R.id.tvEmailSendTimes},
childs,
R.layout.expandablelistview_child,
new String[]{"tvEmailContent"},
new int[]{R.id.tvEmailContent}
);
//将适配器加入列表
expandableListView_one.setAdapter(adapter);
//注册事件
expandableListView_one.setOnChildClickListener(listener);
} private OnChildClickListener listener = new OnChildClickListener(){
@Override
public boolean onChildClick(ExpandableListView arg0, View arg1,
int arg2, int arg3, long arg4) {
Toast.makeText(MainActivity.this, "ok", Toast.LENGTH_SHORT).show();
return false;
}
};
}
最新文章
- SOA 面向服务的体系结构
- MTK 常见的编译命令
- python 函数的调用 和执行 小知识
- [MAC] Mac下的SVN命令行
- sql 集合运算
- No package identifier when getting name for resource number 0x00000000
- UVa 1210 (高效算法设计) Sum of Consecutive Prime Numbers
- NGUI 3.5教程(六)Font字体
- Canvas保存图片保存到本地
- 除法(Division ,UVA 725)-ACM集训
- LNMP搭建(CentOS 6.3+Nginx 1.2.0+PHP 5.3.15(fpm)+ MySQL 5.5.35)
- iOS_SN_地图的使用(2)
- SLC和MLC
- #define DEBUG用法
- NSOJ Constructing Roads(图论)
- 如何使用Linq或EF来对数据去重——Distinct方法详解
- 【MFC】利用双缓冲和随机函数rand()实现蒲公英飞舞
- 利用Pycharm本地调试spark-streaming(包含kafka和zookeeper等操作)
- 008-Python-模块
- 【编程基础】编译器 cc、gcc、g++、CC 的区别