【Android】学习笔记(8)――ListView(下)

4.使用SimpleExpandableListAdapter来实现ListView
使用这种SimpleExpandableListAdapter来实现的ListView和上面三种都不一样,但和使用SimpleAdapter,也就是第三种方式实现ListView有着差不多的思路,可以做一下横向的对比。
先来看一下运行效果吧,因为和之前那三个不一样。

234405478.png

看出不一样了吧~~~
它实现了一种组的概念的ListView,点击左侧的箭头可以对目录进行展开和合上。
来看一下布局文件吧,它相对于第三种方式更加复杂些。
首先,他有一个Activity的布局文件,如下:

  1. <?xmlversion="1.0"encoding="utf-8"?>

  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

  3. android:orientation="vertical"

  4. android:layout_width="fill_parent"

  5. android:layout_height="fill_parent"

  6. >

  7. <ExpandableListView

  8. android:id="@id/android:list"

  9. android:layout_width="fill_parent"

  10. android:layout_height="fill_parent"

  11. android:drawSelectorOnTop="false"

  12. ></ExpandableListView>

  13. <TextView

  14. android:id="@id/android:empty"

  15. android:layout_width="fill_parent"

  16. android:layout_height="wrap_content"

  17. android:text="@string/nodata"

  18. />

  19. </LinearLayout>

该文件中必须有一个ExpandableListView的组件,还有一个nodata的TextView,当没有数据时,会默认显示nodata这个字符串的内容。
接下来是一个group的布局文件,也就是上图中的Male与Female的布局文件,如下:

  1. <?xmlversion="1.0"encoding="utf-8"?>

  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

  3. android:orientation="vertical"

  4. android:layout_width="fill_parent"

  5. android:layout_height="fill_parent"

  6. >

  7. <TextView

  8. android:id="@+id/gender"

  9. android:layout_width="180dp"

  10. android:layout_height="40dp"

  11. android:textSize="10pt"

  12. android:textColor="#fff"

  13. android:layout_marginLeft="40dp"

  14. android:paddingTop="5dp"

  15. android:text="@string/nodata"

  16. />

  17. </LinearLayout>

最后是一个child的布局文件,也就是上图中每个实际条目的布局文件,这个例子中是两个TextView,分别是姓名和年龄,如下:

  1. <?xmlversion="1.0"encoding="utf-8"?>

  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

  3. android:orientation="horizontal"

  4. android:layout_width="fill_parent"

  5. android:layout_height="fill_parent"

  6. >

  7. <TextView

  8. android:id="@+id/name"android:layout_width="180dp"android:layout_height="40dp"android:layout_marginLeft="20dp"android:paddingTop="10dp"android:textColor="#fff"android:text="@string/nodata"

  9. />

  10. <TextView

  11. android:id="@+id/age"android:layout_width="180dp"android:layout_height="40dp"android:layout_marginLeft="20dp"android:paddingTop="10dp"android:textColor="#fff"android:text="@string/nodata"

  12. />

  13. </LinearLayout>

下来看Java代码:

  1. publicclass SimpleExpandableListAdapterDemoActivity extends

  2. ExpandableListActivity {

  3. private List<List<HashMap<String,String>>> childs = null;

  4. private List<HashMap<String,String>> gender = null;

  5. @Override

  6. protectedvoid onCreate(Bundle savedInstanceState) {

  7. // TODO Auto-generated method stub

  8. super.onCreate(savedInstanceState);

  9. setContentView(R.layout.expandable);

  10. gender = new ArrayList<HashMap<String,String>>();

  11. HashMap<String,String> male = new HashMap<String, String>();

  12. male.put("gender", "Male");

  13. HashMap<String,String> female = new HashMap<String, String>();

  14. female.put("gender", "Female");

  15. gender.add(male);

  16. gender.add(female);

  17. List<HashMap<String,String>> men = new ArrayList<HashMap<String,String>>();

  18. HashMap<String,String> man1 = new HashMap<String, String>();

  19. man1.put("name", "Theron");

  20. man1.put("age", "25");

  21. HashMap<String,String> man2 = new HashMap<String, String>();

  22. man2.put("name", "Jack");

  23. man2.put("age", "62");

  24. men.add(man1);

  25. men.add(man2);

  26. List<HashMap<String,String>> women = new ArrayList<HashMap<String,String>>();

  27. HashMap<String,String> woman1 = new HashMap<String, String>();

  28. woman1.put("name", "Helen");

  29. woman1.put("age", "12");

  30. HashMap<String,String> woman2 = new HashMap<String, String>();

  31. woman2.put("name", "Alice");

  32. woman2.put("age", "15");

  33. women.add(woman1);

  34. women.add(woman2);

  35. childs = new ArrayList<List<HashMap<String,String>>>();

  36. childs.add(men);

  37. childs.add(women);

  38. SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(SimpleExpandableListAdapterDemoActivity.this, gender, R.layout.expandable, R.layout.group, new String[]{"gender"}, newint[]{R.id.gender}, childs, R.layout.child, new String[]{"name","age"}, newint[]{R.id.name,R.id.age});

  39. setListAdapter(adapter);

  40. }

  41. @Override

  42. publicboolean onChildClick(ExpandableListView parent, View v,

  43. int groupPosition, int childPosition, long id) {

  44. // TODO Auto-generated method stub

  45. Toast.makeText(SimpleExpandableListAdapterDemoActivity.this, childs.get(groupPosition).get(childPosition).get("name")+" is a "+gender.get(groupPosition).get("gender"), Toast.LENGTH_SHORT).show();

  46. returntrue;

  47. }

  48. }

要注意与SimpleAdapter的比较,首先,这个Activity继承了ExpandableListActivity,现在需要创建一个SimpleExpandableListAdapter的对象,需要9个参数,不用害怕,每个都有理有据,不用紧张,仔细一点,不会错的。
第一个参数:是Activity的上下文对象;
第二个参数:是每个组的标签上数据,就是图中的Male与Female,这个参数是一个List,List中存放的是Map对象,有几个Map就有几个组,这里有两个组,male和female;
第三个参数:是Activity的布局文件;
第四个参数:是组的布局文件;
第五个参数:是组的Map中的Key的字符串数组,这里只有gender一个Key;
第六个参数:是上述的Key在布局文件中所对应的控件的id所组成的int数组;
第七个参数:是具体的数据,也就是组下方的数据,这个数据是个List<List<Map<String,Object>>>型,它里面也是存着List型数据,有几个组,就有几个这样的List<Map<String,Object>>,在每个组中存放的即为该组的数据了,这和第3个例子中的数据结构是一样的;
第八个参数:是组下面所包含的列的属性,也就是Map中Key的内容;
第九个参数:是上述的Key在布局文件中所对应的控件的id所组成的int数组;
可能解释的不是很清楚,可以照着代码来看,注意数据是如何一层层放入的。
数据整合完后,创建了SimpleExpandableListAdapter的对象,就把该对象设置给Activity,就可以显示了。
接下来同样可以对每个条目进行事件监听器的绑定,使用的是重写onChildClick方法。
上述四种ListView各有各的优点,第一种简单易行,但扩展性比较差;第二种主要用于对数据库的列表显示和操作;第三种是最灵活的,可以往每个条目中添加各种空间;第四种是一种展开式的ListView,在某些特殊情况(比如有各种层级关系的list)使用会比上面的三种效果都要好一些。
附件中是示例代码,仅供参考。
如果我的文章给与了你帮助,就不妨请我喝杯咖啡吧,点击-> btn-index.png

你可能感兴趣的:(【Android】学习笔记(8)――ListView(下))