栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

分享Android中ExpandableListView控件使用教程

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

分享Android中ExpandableListView控件使用教程

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

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

 
 
   
    
    
 

包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 group;      //组列表 
  List> child;   //子列表 
  ContactsInfoAdapter adapter; //数据适配器 
   
   
  @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(); 
    child = new ArrayList>(); 
     
    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"}); 
     
  } 
   
   
  private void addInfo(String g,String[] c){ 
    group.add(g); 
    List childitem = new ArrayList(); 
    for(int i=0;i

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

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/163781.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号