1.ExpandableListView简介

ExpandableListView是一种用于垂直滚动展示两级列表的视图,和 ListView 的不同之处就是它可以展示两级列表,分组可以单独展开显示子选项。这些选项的数据是通过 ExpandableListAdapter 关联的。

2.xml页面布局

(1)主界面布局(CommonNumberQueryActivity对应布局)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".CommonNumberQueryActivity"> <TextView
style="@style/TitleStyle"
android:text="常用号码查询" /> <!--可以扩展的listview:ExpandableListView-->
<ExpandableListView
android:id="@+id/elv_common_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"> </ExpandableListView>
</LinearLayout>

(2)elv_child_item_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:padding="5dp"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_group_name"
android:text="分组名称"
android:layout_marginLeft="40dp"
android:textSize="16sp"
android:textColor="@color/red"
android:layout_width="match_parent"
android:layout_height="wrap_content"/> </LinearLayout>

(3)elv_child_item_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:padding="5dp"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_name"
android:text="电话名称"
android:textSize="16sp"
android:textColor="#000"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_number"
android:text="电话号码"
android:textSize="16sp"
android:textColor="#000"
android:layout_width="match_parent"
android:layout_height="wrap_content"/> </LinearLayout>

3.java后台代码

package com.example.administrator.test62360safeguard;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView; import com.example.administrator.test62360safeguard.engine.CommonNumberDao; import java.util.List; public class CommonNumberQueryActivity extends AppCompatActivity { ExpandableListView elv_common_number;
List<CommonNumberDao.Group> groupList; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_common_number_query);
initUI();
initData();
} /**
* 给可扩展的listview:ExpandableListView准备数据,并填充
* 首先将对应的数据库文件放入assets目录下
*/
private void initData() {
CommonNumberDao commonNumberDao=new CommonNumberDao();
//获取数据库中的数据
groupList = commonNumberDao.getGroup();
System.out.println("groupList:"+groupList);
//给ExpandableListView设置数据适配器
elv_common_number.setAdapter(new MyAdapter());
} private void initUI() {
elv_common_number = findViewById(R.id.elv_common_number);
} private class MyAdapter extends BaseExpandableListAdapter {
@Override
public int getGroupCount() {
return groupList.size();
} @Override
public int getChildrenCount(int groupPosition) {
return groupList.get(groupPosition).childList.size();
} @Override
public CommonNumberDao.Group getGroup(int groupPosition) {
return groupList.get(groupPosition);
} @Override
public CommonNumberDao.Child getChild(int groupPosition, int childPosition) {
return groupList.get(groupPosition).childList.get(childPosition);
} @Override
public long getGroupId(int groupPosition) {
return groupPosition;
} @Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
} /**
* 固定写法不需要修改
*/
@Override
public boolean hasStableIds() {
return false;
} @Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
View view = View.inflate(getApplicationContext(), R.layout.elv_child_item_group, null);
TextView tv_group_name = view.findViewById(R.id.tv_group_name);
tv_group_name.setText(getGroup(groupPosition).name);
return view;
} @Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
View view = View.inflate(getApplicationContext(), R.layout.elv_child_item_child, null);
TextView tv_name = view.findViewById(R.id.tv_name);
TextView tv_number = view.findViewById(R.id.tv_number);
tv_name.setText(getChild(groupPosition, childPosition).name);
tv_number.setText(getChild(groupPosition, childPosition).number);
return view;
} /**
* @return 孩子节点是否响应事件
*/
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
}

4.效果图

027 Android 可扩展的listview:ExpandableListView的使用案例的更多相关文章

  1. [Android]使用RecyclerView替代ListView(二)

    以下内容为原创,转载请注明: 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/4242541.html 以前写过一篇“[Android]使用Adapte ...

  2. [Android]使用RecyclerView替代ListView(四:SeizeRecyclerView)

    以下内容为原创,欢迎转载,转载请注明 来自天天博客:<> [Android]使用RecyclerView替代ListView(四:SeizeRecyclerView) 在RecyclerV ...

  3. Android 自定义Adapter 但listview 只显示第一条数据

    <ScrollView android:layout_width="fill_parent" android:layout_height="wrap_content ...

  4. Android中动态更新ListView(转)

    在使用ListView时,会遇到当ListView列表滑动到最底端时,添加新的列表项的问题,本文通过代码演示如何动态的添加新的列表项到ListView中.实现步骤:调用ListView的setOnSc ...

  5. [Android]使用RecyclerView替代ListView(三)

    以下内容为原创,转载请注明: 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/4268097.html  这次来使用RecyclerView实现Pinn ...

  6. [Android]使用RecyclerView替代ListView(一)

    以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/4232560.html RecyclerView是一个比List ...

  7. Android开发系列之ListView

    上篇博客解决了Android客户端通过WebService与服务器端程序进行交互的问题,这篇博客重点关注两个问题,一个是Android应用程序如何与本机文件型数据库SQLite进行交互,另一问题则是如 ...

  8. 【转】Android自定义Adapter的ListView的思路及代码

    原文网址:http://www.jb51.net/article/37236.htm Android自定义Adapter的ListView的思路及代码,需要的朋友可以参考一下   在开发中,我们经常使 ...

  9. Android数据库信息显示在listview上

    Key Points: 1.使用SimpleCursorAdapter将Android数据库信息显示在listview上 adapter = new SimpleCursorAdapter(this, ...

随机推荐

  1. Pytest权威教程24-Pytest导入机制及系统路径

    目录 Pytest导入机制和sys.path/PYTHONPATH 包中的测试脚本及conftest.py文件 独立测试模块及conftest.py文件 调用通过python -m pytest调用p ...

  2. Java 获取客户端真实IP地址

    本文基于方法 HttpServletRequest.getHeader 和 HttpServletRequest.getRemoteAddr 介绍如何在服务器端获取客户端真实IP地址. 业务背景 服务 ...

  3. 搭建K8S集群

    一.前言 我们将现有的虚拟机称之为Node1,用作主节点.为了减少工作量,在Node1安装Kubernetes后,我们利用VirtualBox的虚拟机复制功能,复制出两个完全一样的虚拟机作为工作节点. ...

  4. kubernetes架构和组件

    一.Kubernetes整体架构 Kubernetes属于主从分布式架构,主要由Master Node和Worker Node组成,以及包括客户端命令行工具kubectl和其它附加项. Master ...

  5. Nginx:fastcgi_param详解

    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;#脚本文件请求的路径 fastcgi_param QUERY_STRI ...

  6. SQL查询练习题

    1.查询学生"百里守约"的基本信息 select * from students where name='百里守约' 2.查询学生百里守约"或"百里玄策&quo ...

  7. Spring Boot 教程系列学习

    Spring Boot基础教程1-Spring Tool Suite工具的安装 Spring Boot基础教程2-RESTful API简单项目的快速搭建 Spring Boot基础教程3-配置文件详 ...

  8. LF: 换行,U+000A VT: 垂直定位,U+000B FF: 换页符,U+000C CR: 回车符,U+000D CR+LF:CR(U+000D)后跟LF(U+000A) NEL: 下一行,U+0085 LS: 分行,U+2028 PS: 分段,U+2029

    https://zh.wikipedia.org/wiki/換行 换行(英语:newline.line ending.end-of-line (EOL).line Feed (LF).line bre ...

  9. bootstrap 输入框只能数字和字母等其他限制

    --输入中文.数字.英文: <input οnkeyup="value=value.replace(/[^\w\u4E00-\u9FA5]/g, '')"> --输入数 ...

  10. Linux记录-Shell自动化部署批量建立用户和批量SSH配置(转载)

    if [ ! $# -eq 2 ] ; then echo "请输入用户名和密码以空格分开!" exit else name="$1" passwd=" ...