Android的ExpandableListView的学习

最近在公司做项目,有一个小模块,需要用到ExpandableListView这个控件,所以在工作中边学习边工作。根据需求,在进入ExpandableLisView的时候默认展开第一组,需要用到这个属性,expandableListView.expandGroup(0);

在ExpandableListView中,当点击一个组展开时,其他展开的组是不自动关上的,这需要在监听事件中处理一下:

@Override
 public void onGroupExpand(int groupPosition) {
  // TODO Auto-generated method stub
  for (int i = 0, count = expandableListView.getExpandableListAdapter()
    .getGroupCount(); i < count; i++) {
   if (groupPosition != i) {// 关闭其他分组
    expandableListView.collapseGroup(i);
   }
  }
 }

还有一个地方需要注意的就是在Adapter的getChildrenCount(int groupPosition)方法,要return child.get(groupPosition).size(),否则会在点击组得时候,报数组越界的问题。

下边是部分代码:

MainActivity.java

 1 package com.example.expandablelistviewdemo;
 2
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 import android.app.Activity;
 6 import android.os.Bundle;
 7 import android.widget.ExpandableListView;
 8 import android.widget.ExpandableListView.OnGroupExpandListener;
 9
10 public class MainActivity extends Activity implements OnGroupExpandListener {
11     private ExpandableListView expandableListView;
12     private ServiceStationAdapter adapter;
13     private ArrayList<String> groupList = new ArrayList<String>();
14     private ArrayList<List<ServerStation>> childList = new ArrayList<List<ServerStation>>();
15
16     @Override
17     protected void onCreate(Bundle savedInstanceState) {
18         // TODO Auto-generated method stub
19         super.onCreate(savedInstanceState);
20         setContentView(R.layout.activity_main);
21         expandableListView = (ExpandableListView) findViewById(R.id.expandableListView1);
22
23         setDate();
24     }
25
26     /**
27      * 填充数据
28      *
29      */
30     private void setDate() {
31         List<ServerStation> list1 = new ArrayList<ServerStation>();
32         for (int i = 0; i < 5; i++) {
33             ServerStation serverStation = new ServerStation();
34             serverStation.setAddressDiscri("北京丰台区甲23号" + "," + i);
35             serverStation.setFaxs("010-1234556" + "," + i);
36             serverStation.setPhones("010-74123412" + "," + i);
37             list1.add(serverStation);
38         }
39
40         addDate("北京", list1);
41         List<ServerStation> list2 = new ArrayList<ServerStation>();
42         for (int i = 0; i < 3; i++) {
43             ServerStation serverStation = new ServerStation();
44             serverStation.setAddressDiscri("河北省衡水市大风路23号5栋3门302室" + "," + i);
45             serverStation.setFaxs("0313-1234556" + "," + i);
46             serverStation.setPhones("0313-74123412" + "," + i);
47             list2.add(serverStation);
48         }
49         addDate("河北", list2);
50         expandableListView.setGroupIndicator(null);
51         adapter = new ServiceStationAdapter(MainActivity.this, groupList,
52                 childList);
53         expandableListView.setAdapter(adapter);
54         // 展开一组,关闭其他组
55         expandableListView.setOnGroupExpandListener(MainActivity.this);
56         // 默认展开第一项
57         expandableListView.expandGroup(0);
58
59     }
60
61     @Override
62     public void onGroupExpand(int groupPosition) {
63         // TODO Auto-generated method stub
64         for (int i = 0, count = expandableListView.getExpandableListAdapter()
65                 .getGroupCount(); i < count; i++) {
66             if (groupPosition != i) {// 关闭其他分组
67                 expandableListView.collapseGroup(i);
68             }
69         }
70     }
71
72     /**
73      * 添加数据信息
74      *
75      * @param g
76      * @param c
77      */
78     private void addDate(String g, List<ServerStation> c) {
79         groupList.add(g);
80         List<ServerStation> list = new ArrayList<ServerStation>();
81         for (int i = 0; i < c.size(); i++) {
82             list.add(c.get(i));
83         }
84         childList.add(list);
85     }
86
87 }

ServiceStationAdapter.java

  1 package com.example.expandablelistviewdemo;
  2
  3 import java.util.List;
  4
  5
  6 import android.app.AlertDialog;
  7 import android.content.Context;
  8 import android.content.DialogInterface;
  9 import android.content.Intent;
 10 import android.net.Uri;
 11 import android.view.LayoutInflater;
 12 import android.view.View;
 13 import android.view.View.OnClickListener;
 14 import android.view.ViewGroup;
 15 import android.widget.BaseExpandableListAdapter;
 16 import android.widget.ImageView;
 17 import android.widget.LinearLayout;
 18 import android.widget.TextView;
 19
 20 /**
 21  * expandableListView
 22  *
 23  */
 24 public class ServiceStationAdapter extends BaseExpandableListAdapter {
 25     private Context context;
 26     private List<String> group;
 27     private List<List<ServerStation>> child;
 28
 29     public ServiceStationAdapter(Context context, List<String> group,
 30             List<List<ServerStation>> child) {
 31         this.context = context;
 32         this.group = group;
 33         this.child = child;
 34     }
 35
 36     @Override
 37     public int getGroupCount() {
 38         return group.size();
 39     }
 40
 41     @Override
 42     public int getChildrenCount(int groupPosition) {
 43         // return child.size();
 44         return child.get(groupPosition).size();
 45     }
 46
 47     @Override
 48     public Object getGroup(int groupPosition) {
 49         return group.get(groupPosition);
 50     }
 51
 52     @Override
 53     public Object getChild(int groupPosition, int childPosition) {
 54         return child.get(childPosition).get(childPosition);
 55     }
 56
 57     @Override
 58     public long getGroupId(int groupPosition) {
 59         return groupPosition;
 60     }
 61
 62     @Override
 63     public long getChildId(int groupPosition, int childPosition) {
 64         return childPosition;
 65     }
 66
 67     @Override
 68     public boolean hasStableIds() {
 69         return false;
 70     }
 71
 72     /**
 73      * 修改listview组得样式 group
 74      */
 75     @Override
 76     public View getGroupView(int groupPosition, boolean isExpanded,
 77             View convertView, ViewGroup parent) {
 78         ViewHolder holder;
 79         if (convertView == null) {
 80             convertView = LayoutInflater.from(context).inflate(
 81                     R.layout.expandlistview_group_layout, null);
 82             holder = new ViewHolder();
 83             holder.tvCity = (TextView) convertView.findViewById(R.id.tv_city);
 84             holder.imgArrow = (ImageView) convertView
 85                     .findViewById(R.id.imageView);
 86             convertView.setTag(holder);
 87         } else {
 88             holder = (ViewHolder) convertView.getTag();
 89         }
 90         if (isExpanded) {
 91             holder.imgArrow
 92                     .setBackgroundResource(R.drawable.down_selected_icon);
 93         } else {
 94             holder.imgArrow.setBackgroundResource(R.drawable.up_selected_icon);
 95         }
 96         holder.tvCity.setText(group.get(groupPosition));
 97         return convertView;
 98
 99     }
100
101     /**
102      * child
103      */
104     @Override
105     public View getChildView(int groupPosition, int childPosition,
106             boolean isLastChild, View convertView, ViewGroup parent) {
107         ViewHolder holder;
108         if (convertView == null) {
109             convertView = LayoutInflater.from(context).inflate(
110                     R.layout.expandlistview_child_layout, null);
111             holder = new ViewHolder();
112             holder.tvAddress = (TextView) convertView
113                     .findViewById(R.id.tv_address);
114             holder.tvFax = (TextView) convertView.findViewById(R.id.tv_fax);
115             holder.tvTel = (TextView) convertView.findViewById(R.id.tv_tel);
116             holder.telLayout = (LinearLayout) convertView
117                     .findViewById(R.id.tel_layout);
118             convertView.setTag(holder);
119         } else {
120             holder = (ViewHolder) convertView.getTag();
121         }
122         String address = child.get(groupPosition).get(childPosition)
123                 .getAddressDiscri();
124         holder.tvAddress.setText(address);
125         String fax = child.get(groupPosition).get(childPosition).getFaxs();
126         if ("".equals(fax)) {
127             holder.tvFax.setText("暂无传真信息");
128         } else {
129             holder.tvFax.setText(fax);
130         }
131
132         String mobile = child.get(groupPosition).get(childPosition).getPhones();
133         if ("".equals(mobile)) {
134             holder.tvTel.setText("暂无电话信息");
135         } else {
136             holder.tvTel.setText(mobile);
137         }
138
139         holder.telLayout.setOnClickListener(new MyclickListener(groupPosition,
140                 childPosition));
141         return convertView;
142     }
143
144     class ViewHolder {
145         TextView tvCity, tvAddress, tvFax, tvTel;
146         ImageView imgArrow;
147         LinearLayout telLayout;
148
149     }
150
151     @Override
152     public boolean isChildSelectable(int groupPosition, int childPosition) {
153         return false;
154     }
155
156     class MyclickListener implements OnClickListener {
157         int groupPosition, childPosition;
158
159         public MyclickListener(int groupIndex, int childIndex) {
160             // TODO Auto-generated constructor stub
161
162             this.groupPosition = groupIndex;
163             this.childPosition = childIndex;
164         }
165
166         @Override
167         public void onClick(View v) {
168
169             // showCustomerService(child.get(groupPosition).get(childPosition)
170             // .getTel());
171             showDialog(child.get(groupPosition).get(childPosition).getPhones(),
172                     "拨打电话");
173         }
174     }
175
176     /**
177      *
178      * @Title: showDialog
179      * @Description: TODO(这里用一句话描述这个方法的作用)
180      * @param: @param visible 控件的显隐
181      * @param: @param message dialog内容显示
182      * @param: @param titleStr dialog的标题
183      * @param: @param id 根据id不同设置dialog确定按钮显示
184      * @return: void 返回类型
185      * @date: 2014-9-17 下午4:40:10
186      */
187     public void showDialog(final String message, final String titleStr) {
188         AlertDialog.Builder builder = new AlertDialog.Builder(context);
189         builder.setTitle(titleStr);
190         builder.setMessage(message);
191         builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
192
193             @Override
194             public void onClick(DialogInterface dialog, int which) {
195                 // TODO Auto-generated method stub
196                 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"
197                         + message));
198                 context.startActivity(intent);
199                 dialog.dismiss();
200             }
201
202         });
203
204         builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
205
206             @Override
207             public void onClick(DialogInterface dialog, int which) {
208                 // TODO Auto-generated method stub
209                 dialog.dismiss();
210             }
211         });
212
213         builder.create().show();
214     }
215
216 }

ServiceStation.java

 1 package com.example.expandablelistviewdemo;
 2
 3 import java.io.Serializable;
 4
 5
 6 public class ServerStation implements Serializable {
 7     /**
 8      */
 9     private static final long serialVersionUID = 7492110209741256694L;
10     private String addressDiscri = "";// 详细地址
11     private String phones = "";// 电话
12     private String faxs = "";// 传真
13     public String getAddressDiscri() {
14         return addressDiscri;
15     }
16
17     public void setAddressDiscri(String addressDiscri) {
18         this.addressDiscri = addressDiscri;
19     }
20
21     public String getPhones() {
22         return phones;
23     }
24
25     public void setPhones(String phones) {
26         this.phones = phones;
27     }
28
29     public String getFaxs() {
30         return faxs;
31     }
32
33     public void setFaxs(String faxs) {
34         this.faxs = faxs;
35     }
36 }

布局文件

activity_main.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:background="#f9f9f9"
 6     android:orientation="vertical" >
 7
 8     <ExpandableListView
 9         android:id="@+id/expandableListView1"
10         android:layout_width="match_parent"
11         android:layout_height="wrap_content" >
12     </ExpandableListView>
13
14 </LinearLayout>

expandlistview_group_layout.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6
 7     <RelativeLayout
 8         android:padding="10dp"
 9         android:gravity="center_vertical"
10         android:layout_width="match_parent"
11         android:layout_height="40dp" >
12
13         <TextView
14             android:textColor="@android:color/black"
15             android:textSize="10sp"
16             android:id="@+id/tv_city"
17             android:layout_width="wrap_content"
18             android:layout_height="wrap_content"
19             android:layout_alignParentLeft="true"
20             android:text="北京" />
21
22         <ImageView
23             android:id="@+id/imageView"
24             android:layout_width="wrap_content"
25             android:layout_height="wrap_content"
26             android:layout_alignParentRight="true"
27             android:layout_centerVertical="true" />
28
29     </RelativeLayout>
30
31 </LinearLayout>

expandlistview_child_layout.xml

  1 <?xml version="1.0" encoding="utf-8"?>
  2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3     android:layout_width="match_parent"
  4     android:layout_height="match_parent"
  5     android:background="#6f737d"
  6     android:orientation="vertical" >
  7
  8     <TextView
  9         android:id="@+id/tv_address"
 10         android:layout_width="match_parent"
 11         android:layout_height="50dp"
 12         android:layout_marginLeft="10dp"
 13         android:gravity="center_vertical"
 14         android:textColor="@android:color/white" />
 15
 16     <LinearLayout
 17         android:layout_width="match_parent"
 18         android:layout_height="wrap_content"
 19         android:gravity="center_vertical"
 20
 21         android:orientation="horizontal" >
 22
 23         <LinearLayout
 24             android:layout_width="fill_parent"
 25             android:layout_height="match_parent"
 26             android:layout_weight="1"
 27             android:gravity="center_vertical"
 28             android:orientation="horizontal"
 29             android:padding="10dp" >
 30
 31             <ImageView
 32                 android:layout_width="wrap_content"
 33                 android:layout_height="wrap_content"
 34                 android:src="@drawable/fax_icon" />
 35
 36             <TextView
 37                 android:id="@+id/tv_fax"
 38                 android:layout_width="100dp"
 39                 android:layout_height="wrap_content"
 40                 android:layout_marginLeft="10dp"
 41                 android:textColor="@android:color/white" />
 42         </LinearLayout>
 43
 44         <LinearLayout
 45             android:id="@+id/tel_layout"
 46             android:layout_width="fill_parent"
 47             android:layout_height="match_parent"
 48             android:layout_weight="1"
 49             android:background="@drawable/station_tel_selected"
 50             android:gravity="center_vertical"
 51             android:orientation="horizontal"
 52             android:padding="10dp" >
 53
 54             <ImageView
 55                 android:layout_width="wrap_content"
 56                 android:layout_height="wrap_content"
 57                 android:src=<?xml version="1.0" encoding="utf-8"?>
 58 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 59     android:layout_width="match_parent"
 60     android:layout_height="match_parent"
 61     android:background="#6f737d"
 62     android:orientation="vertical" >
 63
 64     <TextView
 65         android:id="@+id/tv_address"
 66         android:layout_width="match_parent"
 67         android:layout_height="50dp"
 68         android:layout_marginLeft="10dp"
 69         android:gravity="center_vertical"
 70         android:textColor="@android:color/white" />
 71
 72     <LinearLayout
 73         android:layout_width="match_parent"
 74         android:layout_height="wrap_content"
 75         android:gravity="center_vertical"
 76
 77         android:orientation="horizontal" >
 78
 79         <LinearLayout
 80             android:layout_width="fill_parent"
 81             android:layout_height="match_parent"
 82             android:layout_weight="1"
 83             android:gravity="center_vertical"
 84             android:orientation="horizontal"
 85             android:padding="10dp" >
 86
 87             <ImageView
 88                 android:layout_width="wrap_content"
 89                 android:layout_height="wrap_content"
 90                 android:src="@drawable/fax_icon" />
 91
 92             <TextView
 93                 android:id="@+id/tv_fax"
 94                 android:layout_width="100dp"
 95                 android:layout_height="wrap_content"
 96                 android:layout_marginLeft="10dp"
 97                 android:textColor="@android:color/white" />
 98         </LinearLayout>
 99
100         <LinearLayout
101             android:id="@+id/tel_layout"
102             android:layout_width="fill_parent"
103             android:layout_height="match_parent"
104             android:layout_weight="1"
105             android:background="@drawable/station_tel_selected"
106             android:gravity="center_vertical"
107             android:orientation="horizontal"
108             android:padding="10dp" >
109
110             <ImageView
111                 android:layout_width="wrap_content"
112                 android:layout_height="wrap_content"
113                 android:src="@drawable/tel_icon" />
114
115             <TextView
116                 android:id="@+id/tv_tel"
117                 android:layout_width="100dp"
118                 android:layout_height="wrap_content"
119                 android:layout_marginLeft="10dp"
120                 android:textColor="@android:color/white" />
121         </LinearLayout>
122     </LinearLayout>
123
124     <LinearLayout
125         android:layout_width="match_parent"
126         android:layout_height="0.5dp"
127         android:background="@android:color/black" >
128     </LinearLayout>
129
130 </LinearLayout>"@drawable/tel_icon" />
131
132             <TextView
133                 android:id="@+id/tv_tel"
134                 android:layout_width="100dp"
135                 android:layout_height="wrap_content"
136                 android:layout_marginLeft="10dp"
137                 android:textColor="@android:color/white" />
138         </LinearLayout>
139     </LinearLayout>
140
141     <LinearLayout
142         android:layout_width="match_parent"
143         android:layout_height="0.5dp"
144         android:background="@android:color/black" >
145     </LinearLayout>
146
147 </LinearLayout>

 

时间: 2024-10-11 14:50:19

Android的ExpandableListView的学习的相关文章

22.Android之ExpandableListView树形列表学习

Android经常用到树形菜单,一般ExpandableListView可以满足这个需要,今天学习下. XML代码: 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent

Android(java)学习笔记197:使用ExpandableListView组件(ListView的扩展)

1.ExpandableListView是一个用来显示二级节点的ListView. 比如如下效果的界面: 2.使用ExpandableListView步骤 (1)要给ExpandableListView设置适配器,那么必须先设置数据源: (2)数据源,就是此处的适配器类ExpandableAdapter,此方法继承了BaseExpandableListAdapter,它是ExpandableListView的一个子类.需要重写里面的多个方法.getChildView()和getGroupView

Android中ExpandableListView中嵌套ListView

最近项目挺紧张,一直没有时间总结学习,今天把最近遇到的一个奇葩的设计,做一下总结.其他的好多的APP中做的通讯录都类似微信通讯录这样,但是有这样一个需求的设计. 就是分为两个组,第一个组不需要A-Z的索引,第二组需要A-Z的索引.以下是我的实现思路. 1.实现思路 1.写布局文件,将ExpandableListView添加到布局文件. 2.写MyExpandableListViewAdapter的实现 3.ListView设置为不可滑动. 4.再第一组中,添加一个ListView,第二个组中,添

android的ExpandableListView

activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_

Android Window PhoneWindow Activity学习心得--第三弹

Android Window  PhoneWindow Activity学习心得--第三弹 前面 我们完成了从Activity到PhoneWindow的整体跨度 正如我们所知道的与Activity组件关联的一个应用程序窗口视图对象关联一个ViewRoot对象,而将 一个Activity组件的应用程序窗口视图对象与一个ViewRoot对象关联是通过该Activity组件所使用的 窗口管理器(WindowManager)来执行的. 在我们初始化DecorView完成之后,我们需要关联应用程序窗口视图

Android系统源码学习步骤

Android系统是基于Linux内核来开发的,在分析它在运行时库层的源代码时,我们会经常碰到诸如管道(pipe).套接字(socket)和虚拟文件系统(VFS)等知识. 此外,Android系统还在Linux内核中增加了一些专用的驱动程序,例如用于日志系统的Logger驱动程序.用于进程间通信的Binder驱动程序和用于辅助内存管理的匿名共享内存Ashmem驱动程序.在分析这些Android专用驱动程序的时候,也会碰到Linux内核中与进程.内存管理相关的数据结构. 因此,我们有必要掌握一些L

针对Android平台我们需要学习如何在Unity中调用Android的JAVA代码。

Unity for Android 比较特殊,Unity for IOS 打包是将XCODE工程直接交给开发者,开发者可以在工程的基础上继续添加新的视图,最后由开发者自行打包生成IPA包,发布程序.而Unity for Android打包直接生成APK包,等于说源代码开发者是看不到的,但是Unity的自身确实有些局限,针对Android平台我们需要学习如何在Unity中调用Android的JAVA代码.本章我们的目标是使用Unity的脚本打开Activity.首先我们创建一个普通的Android

C#程序员学习Android开发系列之学习路线图

通过前面的3篇博客已经简单的介绍了Android开发的过程并写了一个简单的demo,了解了Android开发的环境以及一些背景知识. 接下来这篇博客不打算继续学习Android开发的细节,先停一下,明确一下接下来的学习目标以及学习路线. 一.对Android开发的基本认识 1.Android原生开发是基于Java语言的,由于我比较擅长C#,所以对Java语言本身不太熟练,需要加强Java语言基础的练习,这一块我会穿插到具体的知识点练习当中,并且在必要的地方给出与C#语言的对比(其实基本上在语法层

quick cocos 或者 Cocos2dx 项目下的Android.mk文件的学习

android.mk文件的作用:编译需要的cpp文件,生成.so动态库,供android端调用. 先上一个android.mk文件: 第一次创建项目,在Android平台编译时,都需要通过android.mk文件编译整个cocos2dx的库(第一次编译我们需要等待很长的时间.....). 首先知道$(call import-module,dir_name)的作用,然后顺着lib/proj.android目录继续找对应目录下的android.mk文件 类似于递归一样,把所有目录下的android.