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. arch linux (manjaro) 下运行tim和qq

    本文通过MetaWeblog自动发布,原文及更新链接:https://extendswind.top/posts/technical/tim_install_wine 基于AUR的安装是没什么难度了, ...

  2. 查看windows操作系统的默认编码【转】

    在Windows平台下,进入DOS窗口,输入:chcp可以得到操作系统的代码页信息,你可以从控制面板的语言选项中查看代码页对应的详细的字符集信息. 例如: 我的活动代码页为:936,它对于的编码格式为 ...

  3. 06 、指令跳转:原来if...else就是goto

    写好的代码编译成指令之后,一般正常流程是一条一条的顺序执行的.但是在程序中总会用到if...else这样的条件判断语句.while和for循环语句,还有函数或者过程调用,所以遇到这些程序编译的指令时是 ...

  4. 使用IDEA运行CAS5.3服务器

    在上节中,我们运行CAS服务器是打成war包在tomcat中进行运行,这节介绍在IDEA中运行CAS服务器. 1.下载CAS 模板 Overlay Template,我这里使用 Apereo CAS ...

  5. ubuntu16.04安装python3.7

    1.安装依赖包 sudo apt-get update sudo apt-get install build-essential python-dev python-setuptools python ...

  6. [教程] Packt - Create a Game Environment with Blender and Unity by Darrin Lile

    学习了解如何使用Blender,photoshop和Unity创建自己的游戏环境!了解如何通过比以往更加集成的方式使用Blender和Unity,将自己的游戏设计变为现实.在Unity中创建测试版本 ...

  7. CMU Database Systems - Sorting,Aggregation,Join

    Sorting 排序如果可在内存里面排,用经典的排序算法就ok,比如快排 问题在于,数据表中的的数据是很多的,没法一下都放到内存里面进行排序 所以就需要用到,外排,多路并归排序 看下最简单的,2路并归 ...

  8. nginx 80 下的一个路径 到 8888

    # For more information on configuration, see:# * Official English Documentation: http://nginx.org/en ...

  9. flutter 日志工具类

    class LogUtils { //dart.vm.product 环境标识位 Release为true debug 为false static const bool isRelease = con ...

  10. 在调用self对象时,本类调用用Win32Info().collect()

    import platform class Test: def test(self): func = getattr(self,'windows') func() @staticmethod def ...