服务器之家

服务器之家 > 正文

分享Android中ExpandableListView控件使用教程

时间:2021-04-24 16:39     来源/作者:cjjky

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

分享Android中ExpandableListView控件使用教程

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?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源码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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中ExpandableListView控件使用教程

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

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
返回顶部