Android ExpandableListView的使用

来源:互联网 发布:linux如何切换盘符 编辑:程序博客网 时间:2024/04/27 20:11
 ExpandableListView继承于ListView,但是它不同于ListView,它可以有多个Group,每一个Group里都可以有多个Child。

  比如可以实现QQ好友栏里类似的功能。

  

view plaincopy to clipboardprint?
  1. <span style="font-size:16px;">import android.content.Context;  
  2. import android.view.View;  
  3. import android.view.ViewGroup;  
  4. import android.widget.BaseExpandableListAdapter;  
  5. import android.widget.TextView;  
  6. /** 
  7.  * @author wing 
  8.  * @date 2011/8/15 
  9.  */  
  10. public class OwnExpandableListAdapter extends BaseExpandableListAdapter {  
  11.     private String[] group;  
  12.     private String[][] child;  
  13.     private Context context;  
  14.     public OwnExpandableListAdapter(String[] group,String[][] child,Context context)  
  15.     {  
  16.         this.group=group;  
  17.         this.child=child;  
  18.         this.context=context;  
  19.     }  
  20.     /** 
  21.      *  获取Group中的一个Child的值 
  22.      */  
  23.     @Override  
  24.     public Object getChild(int groupPosition, int childPosition)    
  25.     {  
  26.           
  27.         return child[groupPosition][childPosition];  
  28.     }  
  29.     /** 
  30.      *  获取Group中的一个Child的ID,本人没有写 
  31.      */  
  32.     @Override  
  33.     public long getChildId(int groupPosition, int childPosition) {  
  34.           
  35.         return 0;  
  36.     }  
  37.   /** 
  38.    *  Child的视图 
  39.    */  
  40.     @Override  
  41.     public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)   
  42.     {  
  43.         //每个Group的Child,用TextView表示,当然也可以用其他的例如button之类的控件  
  44.         TextView textView=new TextView(context);  
  45.         textView.setText(child[groupPosition][childPosition]);  
  46.         textView.setPadding(36000);  
  47.         return textView;  
  48.     }  
  49.       /** 
  50.        *  某个Group的Child的数量 
  51.        */  
  52.     @Override  
  53.     public int getChildrenCount(int groupPosition) {  
  54.           
  55.         return child[groupPosition].length;  
  56.     }  
  57.      
  58.     @Override  
  59.     public Object getGroup(int arg0) {  
  60.           
  61.         return null;  
  62.     }  
  63.     /** 
  64.      *  Group的数目 
  65.      */  
  66.     @Override  
  67.     public int getGroupCount() {  
  68.           
  69.         return group.length;  
  70.     }  
  71.   
  72.     @Override  
  73.     public long getGroupId(int arg0) {  
  74.           
  75.         return 0;  
  76.     }  
  77.     /** 
  78.      * Group的视图 
  79.      */  
  80.     @Override  
  81.     public View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {  
  82.         //每个Group也用TextView表示,当然也可以用其他的例如button之类的控件  
  83.         TextView textView=new TextView(context);  
  84.         textView.setText(group[groupPosition]);  
  85.         textView.setPadding(36000);  
  86.         return textView;  
  87.     }  
  88.   
  89.     @Override  
  90.     public boolean hasStableIds() {  
  91.         // TODO Auto-generated method stub  
  92.         return false;  
  93.     }  
  94.   
  95.     @Override  
  96.     public boolean isChildSelectable(int arg0, int arg1) {  
  97.         // TODO Auto-generated method stub  
  98.         return false;  
  99.     }  
  100.   
  101. }</span>  
  上面是实现的一个ExpandableListAdapter,用于给ExpandableListView填充数据。

  可以看见每一个Group和Child都是用的TextView。当然也可以用Button。

  然后再使用。

  

view plaincopy to clipboardprint?
  1. String[]group= {"我的好友","同事"};  
  2. String[][]child= {{"张三","李四","王五"},{"赵六","杨七","嘿嘿","哈哈"}};  
  3. expandableListView.setAdapter(new OwnExpandableListAdapter(group, child, this));  

  

  另外,其实ExpandableListView也可以实现HTML中下拉框的功能。

  首先介绍几个方法:

  expandableListView.setDivider();这个是设定每个Group之间的分割线。

  expandableListView.setGroupIndicator();这个是设定每个Group之前的那个图标。

  expandableListView.collapseGroup(int group); 将第group组收起

  expandableListView.expandGroup(int group);  将第group组展开


  那么如何实现HTML中下拉框类似的功能呢?

  由于我做的时候使用的是项目素材。不方便截图。所以就给大家一点提示。

  每个Group和每个Child都使用Button,然后设定自己的样式(button selector)。

  然后,每个Button都添加自己的事件,Group Button点击时,展开列表。Child Button点击时,收缩列表,并且改变Group Button的内容即可。

  这样就可以实现下拉框的功能了。而且根据Button selector,基本可以定义出自己的样式。

原创粉丝点击