Android ExpandableListView的简单应用
时间:2014-04-28 18:50:31
收藏:0
阅读:1928
Expandablelistview1Activity.java
package com.wangzhu.demoexpandablelistview; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.os.Bundle; import android.widget.ExpandableListView; import android.widget.SimpleExpandableListAdapter; public class Expandablelistview1Activity extends Activity { private ExpandableListView expandableListView1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.expandablelistview1); init(); } private void init() { expandableListView1 = (ExpandableListView) findViewById(R.id.expandableListView1); getDatas(); } private void getDatas() { // 创建两个一级条目标题 Map<String, String> title_1 = new HashMap<String, String>(); title_1.put("group", "移动开发"); Map<String, String> title_2 = new HashMap<String, String>(); title_2.put("group", "男人的需求"); // 创建一级条目容器 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>(); content_1.put("child", "Android"); Map<String, String> content_2 = new HashMap<String, String>(); content_2.put("child", "Ios"); List<Map<String, String>> childs_1 = new ArrayList<Map<String, String>>(); childs_1.add(content_1); childs_1.add(content_2); // 内容二 Map<String, String> content_3 = new HashMap<String, String>(); content_3.put("child", "金钱"); Map<String, String> content_4 = new HashMap<String, String>(); content_4.put("child", "权利"); Map<String, String> content_5 = new HashMap<String, String>(); content_5.put("child", "女人"); List<Map<String, String>> childs_2 = new ArrayList<Map<String, String>>(); childs_2.add(content_3); childs_2.add(content_4); childs_2.add(content_5); // 存放两个内容,以便显示在列表中 List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>(); childs.add(childs_1); childs.add(childs_2); /** * 参数1:上下文对象context 参数2:一级条目目录集合 参数3:一级条目对应的布局文件 * 参数4:fromto,就是map中的key,指定要显示的对象 参数5:与参数4对应,指定要显示在groups中的id * 参数6:二级条目目录集合 参数7:二级条目对应的布局文件 参数8:fromto,就是map中的key,指定要显示的对象 * 参数9:与参数8对应,指定要显示在childs中的id */ SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter( this, groups, R.layout.expandablelistview1_groups, new String[] { "group" }, new int[] { R.id.textGroup }, childs, R.layout.expandablelistview1_child, new String[] { "child" }, new int[] { R.id.textChild }); expandableListView1.setAdapter(adapter); } }
expandablelistview1.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" > <ExpandableListView android:id="@+id/expandableListView1" android:layout_width="match_parent" android:layout_height="wrap_content" > </ExpandableListView> </LinearLayout>
expandablelistview1_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" > <TextView android:id="@+id/textGroup" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="6dp" android:paddingLeft="40dp" android:paddingTop="6dp" android:text="No data" android:textSize="15sp" /> </LinearLayout>
expandablelistview1_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/textChild" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="10dp" android:paddingLeft="40dp" android:paddingTop="10dp" android:text="No data" android:textSize="20sp" /> </LinearLayout>
备注:
简单的应用,网上导出都可见,理论就不写了,直接源码吧!
评论(0)