027 Android 可扩展的listview:ExpandableListView的使用案例
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的使用案例的更多相关文章
- [Android]使用RecyclerView替代ListView(二)
以下内容为原创,转载请注明: 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/4242541.html 以前写过一篇“[Android]使用Adapte ...
- [Android]使用RecyclerView替代ListView(四:SeizeRecyclerView)
以下内容为原创,欢迎转载,转载请注明 来自天天博客:<> [Android]使用RecyclerView替代ListView(四:SeizeRecyclerView) 在RecyclerV ...
- Android 自定义Adapter 但listview 只显示第一条数据
<ScrollView android:layout_width="fill_parent" android:layout_height="wrap_content ...
- Android中动态更新ListView(转)
在使用ListView时,会遇到当ListView列表滑动到最底端时,添加新的列表项的问题,本文通过代码演示如何动态的添加新的列表项到ListView中.实现步骤:调用ListView的setOnSc ...
- [Android]使用RecyclerView替代ListView(三)
以下内容为原创,转载请注明: 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/4268097.html 这次来使用RecyclerView实现Pinn ...
- [Android]使用RecyclerView替代ListView(一)
以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/4232560.html RecyclerView是一个比List ...
- Android开发系列之ListView
上篇博客解决了Android客户端通过WebService与服务器端程序进行交互的问题,这篇博客重点关注两个问题,一个是Android应用程序如何与本机文件型数据库SQLite进行交互,另一问题则是如 ...
- 【转】Android自定义Adapter的ListView的思路及代码
原文网址:http://www.jb51.net/article/37236.htm Android自定义Adapter的ListView的思路及代码,需要的朋友可以参考一下 在开发中,我们经常使 ...
- Android数据库信息显示在listview上
Key Points: 1.使用SimpleCursorAdapter将Android数据库信息显示在listview上 adapter = new SimpleCursorAdapter(this, ...
随机推荐
- MySQL:服务无法启动(1067)问题
打开安装文件下的my.ini 找到: #Path to the database rootdatadir="C:/ProgramData/MySQL/MySQL Server 5.5/dat ...
- POJ1635 Subway tree systems ——(判断树的同构,树的最小表示法)
给两棵有根树,判断是否同构.因为同构的树的最小表示法唯一,那么用最小表示法表示这两棵树,即可判断同构.顺便如果是无根树的话可以通过选出重心以后套用之前的方法. AC代码如下: #include < ...
- Java 线程之间的通讯,等待唤醒机制
public class ThreadNotifySample { public static void main(String[] args) { // Res res = new Res(); / ...
- c标签简单应用
<pager:column property="ly" title="任务类型" width="10%"> ...
- Fish Lang
fish lang是一门基于函数的,可定制语法的编程语言. 为什么要设计fish这门语言 目前的编程语言语法都是固定死的,无法很容易的移除一种语法.fish语言一切语法由函数定义,语言只提供一个函数的 ...
- 系统假死——系统频繁Full gc问题分析
主要可能的原因: 1,eden区太小,eden和survivor默认比例是8:12,old区太小,新生代和老年代的比例也可以调节的.3,是否程序会分配很多短期存活的大对象,程序本身是否有问题? 进入老 ...
- postgresql大数据查询加索引和不加索引耗时总结
1.创建测试表 CREATE TABLE big_data( id character varying(50) NOT NULL, name character varying(50), dat ...
- html常用标签详解
html常用标签详解 一.总结 一句话总结: 这些资料没必要自己总结,可以直接网上找,简单方便,再根据需求改一下 二.HTML常用标签详解 转自或参考:HTML常用标签详解https://blog.c ...
- AOSP---"Android Open-Source Project"
定义 编辑 "Android Open-Source Project"的缩写 中文意为"Android 开放源代码项目" 内容 编辑 在Android Open ...
- Centos7迁移fastdfs文件系统
系统从一个地方迁移到另一个地方,数据保持不变,但是ip地址和网络情况不一样了,最困难的是要迁移的那个地方还么有互联网,这TM就坑了,所以想到将FastDFS存储的目录整体拷贝过去,这个方法简单粗暴,这 ...