`
zhangyf1987hb
  • 浏览: 80497 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Android中ExpandableListView控件的使用

阅读更多

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


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

       

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics