Android中ExpandableListView控件基本使用

来源:互联网 发布:新济公活佛网络专属版 编辑:程序博客网 时间:2024/05/18 03:05

本文采用一个Demo来展示Android中ExpandableListView控件的使用,如如何在组/子ListView中绑定数据源。直接上代码如下:

程序结构图:

layout目录下的 main.xml 文件源码如下:

[html] 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.     <!-- 我们会自己定义listview的显示方式(在另外一个布局文件里边)不用默认的方式   
  7.          如果自定义listview的显示方式这里这个android:id="@id/android:list" 必须这样写 -->  
  8.     <!-- android:drawSelectOnTop="false"此属性用来设置listview上的背景颜色会不会  
  9.      挡住(覆盖)内容 , 如果这是为false就表示不会覆盖掉 -->   
  10.     <ExpandableListView   
  11.         android:id="@id/android:list"                 
  12.         android:layout_width="fill_parent"                  
  13.         android:layout_height="wrap_content"                
  14.         android:layout_weight="1"                 
  15.         android:drawSelectorOnTop="false"/>   
  16. </LinearLayout>  


包 com.andyidea.demo中ContactsActivity.java源码如下:

[html] view plaincopy
  1. package com.andyidea.demo;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import android.app.ExpandableListActivity;  
  7. import android.os.Bundle;  
  8. import android.view.Gravity;  
  9. import android.view.View;  
  10. import android.view.ViewGroup;  
  11. import android.view.Window;  
  12. import android.widget.AbsListView;  
  13. import android.widget.BaseExpandableListAdapter;  
  14. import android.widget.TextView;  
  15.   
  16. public class ContactsActivity extends ExpandableListActivity {
  17.     List<String> group;           //组列表  
  18.     List<List<String>> child;     //子列表  
  19.     ContactsInfoAdapter adapter;  //数据适配器  
  20.       
  21.     /** Called when the activity is first created. */  
  22.     @Override  
  23.     public void onCreate(Bundle savedInstanceState) {  
  24.         super.onCreate(savedInstanceState);  
  25.         requestWindowFeature(Window.FEATURE_NO_TITLE);  //设置为无标题  
  26.         setContentView(R.layout.main);  
  27.         getExpandableListView().setBackgroundResource(R.drawable.default_bg);  
  28.           
  29.         initializeData();  
  30.         getExpandableListView().setAdapter(new ContactsInfoAdapter());  
  31.         getExpandableListView().setCacheColorHint(0);  //设置拖动列表的时候防止出现黑色背景  
  32.     }  
  33.       
  34.     /**  
  35.      * 初始化组、子列表数据  
  36.      */  
  37.     private void initializeData() {
  38.         group = new ArrayList<String>();  
  39.         child = new ArrayList<List<String>>();  
  40.           
  41.         addInfo("Andy",new String[]{"male","138123***","GuangZhou"});  
  42.         addInfo("Fairy",new String[]{"female","138123***","GuangZhou"});  
  43.         addInfo("Jerry",new String[]{"male","138123***","ShenZhen"});  
  44.         addInfo("Tom",new String[]{"female","138123***","ShangHai"});  
  45.         addInfo("Bill",new String[]{"male","138231***","ZhanJiang"});
  46.     }  
  47.       
  48.     /**  
  49.      * 模拟给组、子列表添加数据  
  50.      * @param g-group  
  51.      * @param c-child  
  52.      */  
  53.     private void addInfo(String g,String[] c) {  
  54.         group.add(g);  
  55.         List<String> childitem = new ArrayList<String>();  
  56.         for(int i=0;i<c.length;i++){  
  57.             childitem.add(c[i]);  
  58.         }  
  59.         child.add(childitem);  
  60.     }  
  61.       
  62.     class ContactsInfoAdapter extends BaseExpandableListAdapter {  
  63.     
  64.         //-----------------Child----------------//  
  65.         @Override  
  66.         public Object getChild(int groupPosition, int childPosition) {  
  67.             return child.get(groupPosition).get(childPosition);  
  68.         }  
  69.           
  70.         @Override  
  71.         public long getChildId(int groupPosition, int childPosition) {  
  72.             return childPosition;  
  73.         }  
  74.           
  75.         @Override  
  76.         public int getChildrenCount(int groupPosition) {  
  77.             return child.get(groupPosition).size();  
  78.         }  
  79.           
  80.         @Override  
  81.         public View getChildView(int groupPosition, int childPosition,  
  82.                 boolean isLastChild, View convertView, ViewGroup parent) {  
  83.             String string = child.get(groupPosition).get(childPosition);   
  84.             return getGenericView(string);  
  85.         }  
  86.           
  87.         //----------------Group----------------//  
  88.         @Override  
  89.         public Object getGroup(int groupPosition) {  
  90.             return group.get(groupPosition);  
  91.         }                 
  92.   
  93.         @Override  
  94.         public long getGroupId(int groupPosition) {  
  95.             return groupPosition;  
  96.         }     
  97.           
  98.         @Override  
  99.         public int getGroupCount() {  
  100.             return group.size();  
  101.         }     
  102.           
  103.         @Override  
  104.         public View getGroupView(int groupPosition, boolean isExpanded,  
  105.                 View convertView, ViewGroup parent) {  
  106.             String string = group.get(groupPosition);    
  107.             return getGenericView(string);  
  108.         }  
  109.   
  110.         //创建组/子视图    
  111.         public TextView getGenericView(String s) {    
  112.             // Layout parameters for the ExpandableListView    
  113.             AbsListView.LayoutParams lp = new AbsListView.LayoutParams(    
  114.                     ViewGroup.LayoutParams.FILL_PARENT, 40);  
  115.     
  116.             TextView text = new TextView(ContactsActivity.this);    
  117.             text.setLayoutParams(lp);    
  118.             // Center the text vertically    
  119.             text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);    
  120.             // Set the text starting position    
  121.             text.setPadding(36, 0, 0, 0);    
  122.                 
  123.             text.setText(s);    
  124.             return text;    
  125.         }    
  126.           
  127.           
  128.         @Override  
  129.         public boolean hasStableIds() {  
  130.             // TODO Auto-generated method stub  
  131.             return false;  
  132.         }         
  133.   
  134.         @Override  
  135.         public boolean isChildSelectable(int groupPosition, int childPosition) {  
  136.             // TODO Auto-generated method stub  
  137.             return true;  
  138.         }
  139.     }  
  140. }  


最后,程序运行后截图如下:

       

转自:http://blog.csdn.net/cjjky/article/details/6903504

0 0