Android基础之ExpandableListActivity

来源:互联网 发布:高考失利知乎 编辑:程序博客网 时间:2024/06/05 20:14

ExpandableListActivity 与ExpandableListView的关系就向 ListActivity与ListView一样总是一起出现的 


先上个个效果图


 

 

 






















1.首先在main.xml添加ExpandableListView

[xhtml] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns: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"   //ExpandableListView的ID为Android自定义,不可改变  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="fill_parent"  
  11.         android:background="@drawable/background"/>  
  12. </LinearLayout>  


2.为父目录创建布局文件

[xhtml] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:layout_width="wrap_content"  
  5.   android:layout_height="wrap_content">  
  6.   <TextView  
  7.     android:id="@+id/parent_group"  
  8.     android:layout_width="wrap_content"  
  9.     android:layout_height="wrap_content"  
  10.     android:paddingTop="8px"  
  11.     android:paddingLeft="50px"  
  12.     android:paddingBottom="6px"  
  13.     android:textColor="#ff00ff"  
  14.     android:textStyle="bold"/>  
  15. </LinearLayout>  


3.为子目录创建布局文件

[xhtml] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:layout_width="wrap_content"  
  5.   android:layout_height="wrap_content"  
  6.   android:paddingLeft="20dp"  
  7.   android:orientation="horizontal">  
  8.   <ImageView  
  9.     android:layout_width="wrap_content"  
  10.     android:layout_height="wrap_content"  
  11.     android:paddingLeft="20px"  
  12.     android:paddingTop="4dp"  
  13.     android:background="@drawable/child_image"/>  
  14.   <LinearLayout  
  15.     android:orientation="vertical"  
  16.     android:layout_width="wrap_content"  
  17.     android:layout_height="wrap_content">  
  18.     <TextView  
  19.         android:id="@+id/child_group"  
  20.         android:textSize="20sp"  
  21.         android:layout_width="wrap_content"  
  22.         android:layout_height="wrap_content"/>  
  23.     <TextView  
  24.         android:layout_width="wrap_content"  
  25.         android:layout_height="wrap_content"  
  26.         android:text="地址:xx省xx市xx"/>  
  27.     </LinearLayout>  
  28. </LinearLayout>  



4.DisplayExpandableList.java

[java] view plaincopy
  1. package com.yin;  
  2. import java.util.ArrayList;  
  3. import java.util.HashMap;  
  4. import java.util.List;  
  5. import android.app.ExpandableListActivity;  
  6. import android.os.Bundle;  
  7. import android.widget.ExpandableListAdapter;  
  8. import android.widget.ExpandableListView;  
  9. import android.widget.SimpleExpandableListAdapter;  
  10. public class DisplayExpandableList extends ExpandableListActivity {  
  11.     /** Called when the activity is first created. */  
  12.     @Override  
  13.     public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.main);  
  16.         List<HashMap<String, String>> parent_groups = new ArrayList<HashMap<String, String>>();  
  17.         String[] parent_group_names = {"我的好友","初中同学","高中同学","大学同学","黑名单"};  
  18.         parent_groups = this.addParentItems(parent_groups, parent_group_names);  
  19.         List<List<HashMap<String, String>>> child_groups = new ArrayList<List<HashMap<String, String>>>();  
  20.         List<HashMap<String, String>> child_lists = new ArrayList<HashMap<String, String>>();  
  21.         child_groups = this.addChildsItems(child_groups, child_lists,  
  22.                 "小华");  
  23.         /** 
  24.          * SimpleExpandableListAdapter的参数那是相当的多啊 
  25.          * 参数   1:context   
  26.          *      2:父级目录的数据 
  27.          *      3:父级目录的布局文件 
  28.          *      4: 夫级目录的数据来源 
  29.          *      5:指定父级目录显示数据的控件 
  30.          *      6:子级目录的数据 
  31.          *      7:子级目录的布局文件 
  32.          *      8:子级目录的数据来源 
  33.          *      9:指定子级目录显示数据的控件 
  34.          */  
  35.         SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(  
  36.                 this, parent_groups, R.layout.parent_layout,  
  37.                 new String[]{"parent_group"}, new int[]{R.id.parent_group},  
  38.                 child_groups, R.layout.child_layout, new String[]{"child_group"},  
  39.                 new int[]{R.id.child_group});  
  40.         setListAdapter(adapter);  
  41.     }  
  42.     //为每个父母目录下子目录添加数据  
  43.     public List<List<HashMap<String, String>>> addChildsItems(  
  44.             List<List<HashMap<String, String>>> child_groups,  
  45.             List<HashMap<String, String>> child_list, String item) {  
  46.           
  47.         child_groups = new ArrayList<List<HashMap<String, String>>>();  
  48.         child_list = new ArrayList<HashMap<String, String>>();  
  49.         HashMap<String, String> map = new HashMap<String, String>();  
  50.         map.put("child_group", item);  
  51.         child_list.add(map);  
  52.         child_groups.add(child_list);  
  53.         return child_groups;  
  54.     }  
  55.     //为父级目录分组,添加标识  
  56.     public List<HashMap<String, String>> addParentItems(  
  57.             List<HashMap<String, String>> parent_groups, String[] parent_group_names) {  
  58.         parent_groups = new ArrayList<HashMap<String, String>>();  
  59.       
  60.         for(int i=0;i<parent_group_names.length;i++){  
  61.             HashMap<String, String> groups = new HashMap<String, String>();  
  62.             groups.put("parent_group", parent_group_names[i]);  
  63.             parent_groups.add(groups);  
  64.         }  
  65.           
  66.         return parent_groups;  
  67.     }  
  68. }  

0 0
原创粉丝点击