使用ExpandableListView和ExpandableListAdapter实现分组列表

ExpandableListView直接继承自ListView,而ExpandableListAdapter是一个超级接口,独立于Adapter接口。

ExpandableListAdapter类图:

 

下面的例子将ExpandableListView与SimpleExpandableListAdapter结合,实现分组列表。

先准备好三个布局文件:

主布局文件expandablelistview_activity.xml,显示ExpandableListView:

[html] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <ExpandableListView  
  8.         android:id="@+id/expandableListView1"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content">  
  11.     </ExpandableListView>  
  12.   
  13. </LinearLayout>  

 

父列表项布局文件expandable_group.xml:

[html] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/expandable_group_textview"  
  9.         android:layout_marginLeft="30dp"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"/>  
  12.   
  13. </LinearLayout>  

子列表项布局文件expandable_child.xml:

[html] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/expandable_child_textview"  
  9.         android:layout_marginLeft="50dp"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content" />  
  12.   
  13. </LinearLayout>  

Activity类:

[java] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. package com.zzj.ui.expandablelistviewdemo;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5. import java.util.List;  
  6. import java.util.Map;  
  7.   
  8. import android.app.Activity;  
  9. import android.os.Bundle;  
  10. import android.view.View;  
  11. import android.widget.ExpandableListView;  
  12. import android.widget.ExpandableListView.OnChildClickListener;  
  13. import android.widget.SimpleExpandableListAdapter;  
  14. import android.widget.Toast;  
  15.   
  16. import com.zzj.ui.R;  
  17.   
  18. public class MainActivity extends Activity implements OnChildClickListener {  
  19.     private String[] names = { "腾讯", "百度", "阿里巴巴" };  
  20.   
  21.     private String[][] childnames = { { "QQ", "微信", "手机卫士" },  
  22.             { "百度地图", "百度视频", "PPS&奇艺" }, { "支付宝", "新浪微博", "高德地图" } };  
  23.   
  24.     @Override  
  25.     protected void onCreate(Bundle savedInstanceState) {  
  26.         super.onCreate(savedInstanceState);  
  27.   
  28.         setContentView(R.layout.expandablelistview_activity);  
  29.         ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.expandableListView1);  
  30.   
  31.         List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();  
  32.         List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();  
  33.   
  34.         for (int i = 0; i < names.length; i++) {  
  35.             Map<String, String> map = new HashMap<String, String>();  
  36.             map.put("name", names[i]);  
  37.             groupData.add(map);  
  38.   
  39.             String[] childs = childnames[i];  
  40.             List<Map<String, String>> list = new ArrayList<Map<String, String>>();  
  41.             for (int j = 0; j < childs.length; j++) {  
  42.                 Map<String, String> childMap = new HashMap<String, String>();  
  43.                 childMap.put("childname", childs[j]);  
  44.                 list.add(childMap);  
  45.             }  
  46.             childData.add(list);  
  47.         }  
  48.   
  49.         SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(  
  50.                 this, groupData, R.layout.expandable_group,  
  51.                 new String[] { "name" },  
  52.                 new int[] { R.id.expandable_group_textview }, childData,  
  53.                 R.layout.expandable_child, new String[] { "childname" },  
  54.                 new int[] { R.id.expandable_child_textview });  
  55.   
  56.         expandableListView.setAdapter(adapter);  
  57.         expandableListView.setOnChildClickListener(this);  
  58.     }  
  59.   
  60.     @Override  
  61.     public boolean onChildClick(ExpandableListView parent, View v,  
  62.             int groupPosition, int childPosition, long id) {  
  63.         String text = names[groupPosition] + "\r\n"  
  64.                 + childnames[groupPosition][childPosition];  
  65.         Toast.makeText(this, text, Toast.LENGTH_LONG).show();  
  66.         return true;  
  67.     }  
  68. }  

父列表数据与子列表数据的顺序必须相对应。

 

给ExpandableListView设置adapter的时候调用的是setAdapter(ExpandableListAdapter adapter)方法,而不是setAdapter(Adapter adapter)方法。setAdapter(Adapter adapter)方法已被重写:

[java] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. @Override  
  2.    public void setAdapter(ListAdapter adapter) {  
  3.        throw new RuntimeException(  
  4.                "For ExpandableListView, use setAdapter(ExpandableListAdapter) instead of " +  
  5.                "setAdapter(ListAdapter)");  
  6.    }  


该方法已不允许被调用。

 

以上例子使用OnChildClickListener对子列表项的点击事件进行监听。

效果图如下:



可以参照SimpleExpandableListAdapter,扩展BaseExpandableListAdapter,实现自己的ExpandableListAdapter。

posted @ 2016-11-28 16:58  天涯海角路  阅读(127)  评论(0)    收藏  举报