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. git添加公钥后报错sign_and_send_pubkey: signing failed: agent refused operation的解决办法

    在服务器添加完公钥后报错 sign_and_send_pubkey: signing failed: agent refused operation 这个时候我们只要执行下 eval "$( ...

  2. LOJ#565. 「LibreOJ Round #10」mathematican 的二进制 分治,FFT,概率期望

    原文链接www.cnblogs.com/zhouzhendong/p/LOJ565.html 前言 标算真是优美可惜这题直接暴力FFT算一算就solved了. 题解 首先,假装没有进位,考虑解决这个问 ...

  3. hbase 在线修复集群命令

    前提:HDFS fsck确保hbase根目录下文件没有损坏丢失,如果有,则先进行corrupt block移除. 切记:一定要在所有Region都上线之后再修复,否则修复之后可能出现重复Region. ...

  4. Linux远程传输文件免密码

    首先为什么Linux远程传输要免密码?手动使用scp命令传输每次都要输密码太过麻烦了. 开发中有一句话,能复制粘贴尽量不要手打. 运维中有一句话,能脚本化实现尽量不要手动执行. 远程传输文件免密码的目 ...

  5. html5表单重写

    html5表单重写 一.总结 一句话总结: 表单重写用于在提交按钮上指定表单提交的各种信息,比如action <input type="submit" value=" ...

  6. Tomcat学习四步走:内核、集群、参数及性能

    主题简介: 内核实现原理 分布式集群 生产部署关键参数 性能监控和分析 一.内核实现原理 HTTP Web服务器与浏览器之间以HTTP协议通信,浏览器要访问服务器即向服务器发送HTTP请求报文. 如图 ...

  7. python No module named 'urlparse'

    python3中,取消了urlparse 引用方式改为了: from urllib import parse

  8. Qualcomm Audio HAL 音频通路设置【转】

    本文转载自:https://blog.csdn.net/azloong/article/details/79383323 1. 音频框图概述| Front End PCMs | SoC DSP | B ...

  9. 页面的Tab选项卡 简单实例

    <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="C ...

  10. shell脚本将gbk文件转化为utf-8

    使用注意项: 原来文件格式gbk的,否则可能出现utf-8转utf-8乱码. #!/bin/bash function gbk2utf(){ file="$1" echo &quo ...