当前位置: 移动技术网 > IT编程>移动开发>Android > 分享Android中ExpandableListView控件使用教程

分享Android中ExpandableListView控件使用教程

2019年07月24日  | 移动技术网IT编程  | 我要评论

烧饼皇后主题曲,于娟博客活着就是王道,九龙剑典

本文采用一个demo来展示android中expandablelistview控件的使用,如如何在组/子listview中绑定数据源。直接上代码如下:
程序结构图:

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

<?xml version="1.0" encoding="utf-8"?> 
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:orientation="vertical" 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent"> 
  <!-- 我们会自己定义listview的显示方式(在另外一个布局文件里边)不用默认的方式  
     如果自定义listview的显示方式这里这个android:id="@id/android:list" 必须这样写 --> 
  <!-- android:drawselectontop="false"此属性用来设置listview上的背景颜色会不会 
   挡住(覆盖)内容 , 如果这是为false就表示不会覆盖掉 -->  
  <expandablelistview  
    android:id="@id/android:list"         
    android:layout_width="fill_parent"         
    android:layout_height="wrap_content"        
    android:layout_weight="1"         
    android:drawselectorontop="false"/>  
</linearlayout> 

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

package com.andyidea.demo; 
 
import java.util.arraylist; 
import java.util.list; 
 
import android.app.expandablelistactivity; 
import android.os.bundle; 
import android.view.gravity; 
import android.view.view; 
import android.view.viewgroup; 
import android.view.window; 
import android.widget.abslistview; 
import android.widget.baseexpandablelistadapter; 
import android.widget.textview; 
 
public class contactsactivity extends expandablelistactivity { 
   
  list<string> group;      //组列表 
  list<list<string>> child;   //子列表 
  contactsinfoadapter adapter; //数据适配器 
   
  /** called when the activity is first created. */ 
  @override 
  public void oncreate(bundle savedinstancestate) { 
    super.oncreate(savedinstancestate); 
    requestwindowfeature(window.feature_no_title); //设置为无标题 
    setcontentview(r.layout.main); 
    getexpandablelistview().setbackgroundresource(r.drawable.default_bg); 
     
    initializedata(); 
    getexpandablelistview().setadapter(new contactsinfoadapter()); 
    getexpandablelistview().setcachecolorhint(0); //设置拖动列表的时候防止出现黑色背景 
  } 
   
  /** 
   * 初始化组、子列表数据 
   */ 
  private void initializedata(){ 
    group = new arraylist<string>(); 
    child = new arraylist<list<string>>(); 
     
    addinfo("andy",new string[]{"male","138123***","guangzhou"}); 
    addinfo("fairy",new string[]{"female","138123***","guangzhou"}); 
    addinfo("jerry",new string[]{"male","138123***","shenzhen"}); 
    addinfo("tom",new string[]{"female","138123***","shanghai"}); 
    addinfo("bill",new string[]{"male","138231***","zhanjiang"}); 
     
  } 
   
  /** 
   * 模拟给组、子列表添加数据 
   * @param g-group 
   * @param c-child 
   */ 
  private void addinfo(string g,string[] c){ 
    group.add(g); 
    list<string> childitem = new arraylist<string>(); 
    for(int i=0;i<c.length;i++){ 
      childitem.add(c[i]); 
    } 
    child.add(childitem); 
  } 
   
  class contactsinfoadapter extends baseexpandablelistadapter{ 
 
     
    //-----------------child----------------// 
    @override 
    public object getchild(int groupposition, int childposition) { 
      return child.get(groupposition).get(childposition); 
    } 
     
    @override 
    public long getchildid(int groupposition, int childposition) { 
      return childposition; 
    } 
     
    @override 
    public int getchildrencount(int groupposition) { 
      return child.get(groupposition).size(); 
    } 
     
    @override 
    public view getchildview(int groupposition, int childposition, 
        boolean islastchild, view convertview, viewgroup parent) { 
      string string = child.get(groupposition).get(childposition);  
      return getgenericview(string); 
    } 
     
    //----------------group----------------// 
    @override 
    public object getgroup(int groupposition) { 
      return group.get(groupposition); 
    }         
 
    @override 
    public long getgroupid(int groupposition) { 
      return groupposition; 
    }   
     
    @override 
    public int getgroupcount() { 
      return group.size(); 
    }   
     
    @override 
    public view getgroupview(int groupposition, boolean isexpanded, 
        view convertview, viewgroup parent) { 
      string string = group.get(groupposition);  
      return getgenericview(string); 
    } 
 
    //创建组/子视图  
    public textview getgenericview(string s) {  
      // layout parameters for the expandablelistview  
      abslistview.layoutparams lp = new abslistview.layoutparams(  
          viewgroup.layoutparams.fill_parent, 40); 
  
      textview text = new textview(contactsactivity.this);  
      text.setlayoutparams(lp);  
      // center the text vertically  
      text.setgravity(gravity.center_vertical | gravity.left);  
      // set the text starting position  
      text.setpadding(36, 0, 0, 0);  
        
      text.settext(s);  
      return text;  
    }  
     
     
    @override 
    public boolean hasstableids() { 
      // todo auto-generated method stub 
      return false; 
    }     
 
    @override 
    public boolean ischildselectable(int groupposition, int childposition) { 
      // todo auto-generated method stub 
      return true; 
    } 
     
  } 
} 

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

希望本文所述对大家学习android软件编程有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网