先来看下什么是ExpandableListView

跟列表有点像,这种是可以折叠的列表,下面来看下是如何在代码中实现

一、在布局文件中声明一个ExpandableListView

<LinearLayout 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_parent"tools:context=".MainActivity" >    <ExpandableListViewandroid:id="@id/android:list"android:layout_width="fill_parent"android:layout_height="fill_parent"android:drawSelectorOnTop="false" ></ExpandableListView><TextViewandroid:id="@id/android:empty"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/no_data" /></LinearLayout>

二、为每个Group和Child定义一个布局

group.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TextView android:id="@+id/groupTo"android:layout_width="fill_parent"android:layout_height="fill_parent"android:paddingLeft="50dp"android:paddingTop="5dp"android:paddingBottom="5dp"android:textSize="20sp"android:text="@string/no_data"/>
</LinearLayout>

child.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TextView android:id="@+id/childTo"android:layout_width="fill_parent"android:layout_height="fill_parent"android:paddingLeft="60dp"android:paddingTop="10dp"android:paddingBottom="10dp"android:textSize="20sp"android:text="@string/no_data"/>
</LinearLayout>

以上两个文件只有缩进不一样,这样可以明显区分Group和Child

三、在代码中实现

package com.example.expandableList;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.SimpleExpandableListAdapter;
import android.widget.Toast;import com.example.widgetdemo.R;public class expandableList extends ExpandableListActivity {ExpandableListView expandableList = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.expandable);expandableList = getExpandableListView();// 写入当前组数List<Map<String, String>> groups = new ArrayList<Map<String, String>>();Map<String, String> group1 = new HashMap<String, String>();group1.put("group", "广东省");Map<String, String> group2 = new HashMap<String, String>();group2.put("group", "江西省");groups.add(group1);groups.add(group2);// 写入第一组的数据List<Map<String, String>> child1 = new ArrayList<Map<String, String>>();Map<String, String> childData1 = new HashMap<String, String>();childData1.put("child", "广州市");Map<String, String> childData2 = new HashMap<String, String>();childData2.put("child", "深圳市");Map<String, String> childData3 = new HashMap<String, String>();childData3.put("child", "东莞市");child1.add(childData1);child1.add(childData2);child1.add(childData3);// 写入第二组的数据List<Map<String, String>> child2 = new ArrayList<Map<String, String>>();Map<String, String> childData4 = new HashMap<String, String>();childData4.put("child", "赣州市");Map<String, String> childData5 = new HashMap<String, String>();childData5.put("child", "南昌市");Map<String, String> childData6 = new HashMap<String, String>();childData6.put("child", "九江市");Map<String, String> childData7 = new HashMap<String, String>();childData7.put("child", "吉安市");child2.add(childData4);child2.add(childData5);child2.add(childData6);child2.add(childData7);List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>();childs.add(child1);childs.add(child2);SimpleExpandableListAdapter sela = new SimpleExpandableListAdapter(expandableList.this, groups, R.layout.group,new String[] { "group" }, new int[] { R.id.groupTo }, childs,R.layout.child, new String[] { "child" },new int[] { R.id.childTo });setListAdapter(sela);expandableList.setOnChildClickListener(new expandableListListener());}//为ExpandableListView编写监听器class expandableListListener implements OnChildClickListener {@Overridepublic boolean onChildClick(ExpandableListView parent, View v,int groupPosition, int childPosition, long id) {// TODO Auto-generated method stubSystem.out.println("group = " + groupPosition + " child = "+ childPosition + " id = " + id);ExpandableListAdapter exAdapter = parent.getExpandableListAdapter();// 获得选中项的HashMap对象Map<String, String> childMap = (HashMap<String, String>) exAdapter.getChild(groupPosition, childPosition);String contentChild = childMap.get("child");Map<String, String> groupMap = (HashMap<String, String>) exAdapter.getGroup(groupPosition);String contentGroup = groupMap.get("group");Toast.makeText(getApplicationContext(),"你选择了第" + groupPosition + " 个Group, 第 " + childPosition+ " 个Child的值是:" + contentChild + " Group的值是 " + contentGroup,Toast.LENGTH_LONG).show();return false;}}
}

关于如何获取选中的值,我查看文档终于找到ExpandableListAdapter,在这个适配器有获取那个child和group的方法。

下面看下效果

最后还是传上源码

点击打开链接

Android常用控件之ExpandableList的使用相关推荐

  1. Android常用控件有哪些?如何使用?

    Android常用控件介绍及使用 控件 TextView 显示文字,相当于Panel ImageView 显示图片 EditText 输入框,可编辑,可设置软键盘方式 Button 按钮,可附带图片 ...

  2. Android学习--02(猜猜我的星座App源码+Android常用控件TextView+EditText+Button+ImangeView+DatePicker+App间通信+跳转页面)

    猜猜我的星座App 1 Android常用控件 1.1 TextView控件 1.1.1 简介 1.1.2属性 1.1.3 扩展属性 1.1.4 TextView的使用方法 1.1.5总结 1.2 E ...

  3. Android常用控件之Button与ImageButton

    掌握Android常用控件之Button与ImageButton的用法,熟悉它们的常用属性. 1.相关代码: activity_main.xml代码: <?xml version="1 ...

  4. Android常用控件,四大组件,intent应用

    常用控件:TextView. Button. EditText. ImageView. ProgressBar.AlterDailog. ProgressDailog 四大组件: Activity(活 ...

  5. android常用控件实验报告,常用控件的编程实验报告

    实验二 常用控件的编程 一.实验目的和要求 (1)掌握窗口下拉列表框.选择钮.组合框.多行编辑框等常用控件的基本使用方法和编程方法: (2)掌握窗口控件的齐整性操纵方法以及Tab顺序的设置: (3)掌 ...

  6. android的控件常用方法是,Android常用控件属性分析

    1.TextView 单行文本输入框,使用过的事件是onClickListener android:gravity="right':设置内容的对其方式 android:layout_grav ...

  7. android基础 [超级详细android常用控件解析(ScollView控件,ProgressBar进度条,PopupWindow控件)]

    目录 1 章节目录 2 ScollView控件 2.1 ScrollView简介 2.2 ScrollView使用 2.3 常用属性及方法 3 ProgressBar进度条 3.1 简介 3.2 常用 ...

  8. android常用控件实验报告,ui设计实验报告.doc

    ui设计实验报告 ui设计实验报告 篇一:UI设计实验报告 实验项目四:UI设计 一. 实验目的和要求 1.熟练运用Eclipse软件中的swing设计. 2.掌握UI编写的软件. 3.能都熟练的进行 ...

  9. Android常用控件之Fragment仿Android4.0设置界面

    Fragment是Android3.0新增的概念,是碎片的意思,它和Activity很相像,用来在一个Activity中描述一些行为或部分用户界面:使用多个Fragment可以在一个单独的Activi ...

最新文章

  1. 上下位机通讯协议_上位机与下位机的区别通讯
  2. mysql重做日志恢复数据_MySQL中重做日志,回滚日志,以及二进制日志的简单总结...
  3. MyBatis中动态sql实现传递多个参数并使用if进行参数的判断和实现like模糊搜索以及foreach实现in集合
  4. SQL COUNT() 函数
  5. Android-源码剖析CountDownTimer(倒计时类)
  6. (七) DockerUI与Shipyard以及InfluxDB+cAdvisor+Grafana配置监控...
  7. QT乱码总结2.gbk和ANSI和gb2312的区别
  8. 利用梯度下降法求解一元线性回归和多元线性回归
  9. 苹果禁用FaceTime多人聊天功能:漏洞将很快修复
  10. 技术沙龙|实力赋能开发者,助力企业从容应对数字化转型难题
  11. dataframe 切片_NumPy中的ndarray与Pandas的Series和DataFrame之间的区别与转换
  12. 穿越计算机的迷雾--读书笔记五
  13. 自然人税收管理系统服务器,【轻松学个税申报】自然人税收管理系统客户端操作...
  14. Python 数据结构之二叉树的实现
  15. 使用富盛Sbo-Addon程序开发框架轻松开发模态单据选择查询功能实例
  16. HDU 6095 Rikka with Competition
  17. 软件测试基础 - 单元测试理论部分
  18. HR让回去等通知,到底啥意思?
  19. faster r-cnn训练、测试、检测(含批量检测图片)
  20. 【javaScript】获取某年某月的的最后一天(即当月天数) 妙用

热门文章

  1. 脑机接口 脑电波分类_脑机接口的商业应用和脑数据的重要性
  2. 你写的项目管理周报有没有灵魂?【管理有度6】
  3. 1109 擅长C(C语言)
  4. 基于51单片机pwm调光护眼台灯智能检测光强光控灯设计proteus仿真原理图PCB
  5. 奥的斯服务器显示nosc0,西子奥的斯OH-CON8501FOVF货梯电气原理图.pdf
  6. GB/T28181-2016传输要求和Android平台设备接入技术实现
  7. 字节跳动进场,二手车没等来新故事
  8. 山东17地市邮编php数组
  9. 使用PN532复制一张新卡(读卡,写卡)(使用上位机)
  10. asp.net修改html文件,asp.net生成html静态页的多种方法