Android学习笔记之ExpandableListView

2024-01-04 02:18

本文主要是介绍Android学习笔记之ExpandableListView,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

<1>简介

android中有一种expandablelistview,可以扩展的listview,就是那种点击一下可以扩展出子项,再点一下收缩回去的显示list。

一个视图显示项目的垂直滚动的两级列表。这不同于ListView,允许有两级列表。

分组能单独地被扩展出到显出它的子项目。各子项目来自ExpandableListAdapter相关的View。

<2>重要方法

   expandGroup(int groupPos) :在分组列表视图中展开一组,

        setSelectedGroup(int groupPosition) :设置选择指定的组。

  setSelectedChild(int groupPosition, int childPosition, boolean shouldExpandGroup) :设置选择指定的子项。

  getPackedPositionGroup(long packedPosition) :返回所选择的组

  getPackedPositionForChild(int groupPosition, int childPosition) :返回所选择的子项

  getPackedPositionType(long packedPosition) :返回所选择项的类型(Child,Group)

  isGroupExpanded(int groupPosition) :判断此组是否展开

<3>范例

package xiaosi.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.widget.SimpleExpandableListAdapter;public class ExpandableLists extends ExpandableListActivity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);//为一级条目提供数据                   //Groups  分组List<Map<String,Object>> Groups = new ArrayList<Map<String,Object>>();Map<String,Object> groupone = new HashMap<String,Object>();groupone.put("groupname", "流行歌曲");Groups.add(groupone);Map<String,Object> grouptwo = new HashMap<String,Object>();grouptwo.put("groupname", "校园歌曲");Groups.add(grouptwo);//定义两个List<Map<String,Object>>  childone和childtwo//为二级条目提供数据//childoneList<Map<String,Object>> childone = new ArrayList<Map<String,Object>>(); Map<String,Object> childonedateone = new HashMap<String,Object>();//childonedateone.put("mp3logo", R.drawable.logo0);childonedateone.put("mp3ID", "1");childonedateone.put("mp3name", "流星雨");childone.add(childonedateone);Map<String,Object> childonedatetwo = new HashMap<String,Object>();//childonedatetwo.put("mp3logo", R.drawable.logo1);childonedatetwo.put("mp3ID", "2");childonedatetwo.put("mp3name", "千里之外");childone.add(childonedatetwo);Map<String,Object> childonedatethree = new HashMap<String,Object>();//childonedatethree.put("mp3logo", R.drawable.logo2);childonedatethree.put("mp3ID", "3");childonedatethree.put("mp3name", "菊花台");childone.add(childonedatethree);//childtwoList<Map<String,Object>> childtwo = new ArrayList<Map<String,Object>>(); Map<String,Object> childtwodateone = new HashMap<String,Object>();//childtwodateone.put("mp3logo", R.drawable.logo0);childtwodateone.put("mp3ID", "1");childtwodateone.put("mp3name", "同桌的你");childtwo.add(childtwodateone);//ChildsList<List<Map<String,Object>>> Childs = new ArrayList<List<Map<String,Object>>>();Childs.add(childone);Childs.add(childtwo);/*** 使用SimpleExpandableListAdapter显示ExpandableListView* 参数1.上下文对象Context* 参数2.一级条目目录集合* 参数3.一级条目对应的布局文件* 参数4.fromto,就是map中的key,指定要显示的对象* 参数5.与参数4对应,指定要显示在groups中的id* 参数6.二级条目目录集合* 参数7.二级条目对应的布局文件* 参数8.fromto,就是map中的key,指定要显示的对象* 参数9.与参数8对应,指定要显示在childs中的id*/SimpleExpandableListAdapter simpleExpandListAdapter = new SimpleExpandableListAdapter(ExpandableLists.this, Groups, R.layout.group, new String[]{"groupname"},new int[]{R.id.group},Childs, R.layout.child, new String[]{"mp3ID","mp3name"},new int[]{R.id.id,R.id.name});setListAdapter(simpleExpandListAdapter);}
}


 

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><ExpandableListViewandroid:id="@id/android:list"android:layout_width="fill_parent"android:layout_height="fill_parent"android:drawSelectorOnTop="false"/><TextViewandroid:id="@id/android:empty"android:layout_width="fill_parent"android:layout_height="fill_parent"android:text="No Data"/>
</LinearLayout>


 

group.xml

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent" ><TextView android:id="@+id/group" android:layout_width="fill_parent"android:layout_height="fill_parent" android:paddingLeft="60px" android:textSize="20sp" android:text="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="fill_parent"android:layout_height="fill_parent"android:orientation="horizontal" ><TextViewandroid:id="@+id/id"android:layout_width="wrap_content"android:layout_height="wrap_content"android:paddingLeft="50px"/><TextViewandroid:id="@+id/name"android:layout_width="wrap_content"android:layout_height="wrap_content" android:paddingLeft="50px" android:text="No data"/></LinearLayout>


 

这篇关于Android学习笔记之ExpandableListView的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/567854

相关文章

利用Python快速搭建Markdown笔记发布系统

《利用Python快速搭建Markdown笔记发布系统》这篇文章主要为大家详细介绍了使用Python生态的成熟工具,在30分钟内搭建一个支持Markdown渲染、分类标签、全文搜索的私有化知识发布系统... 目录引言:为什么要自建知识博客一、技术选型:极简主义开发栈二、系统架构设计三、核心代码实现(分步解析

在Android平台上实现消息推送功能

《在Android平台上实现消息推送功能》随着移动互联网应用的飞速发展,消息推送已成为移动应用中不可或缺的功能,在Android平台上,实现消息推送涉及到服务端的消息发送、客户端的消息接收、通知渠道(... 目录一、项目概述二、相关知识介绍2.1 消息推送的基本原理2.2 Firebase Cloud Me

Android中Dialog的使用详解

《Android中Dialog的使用详解》Dialog(对话框)是Android中常用的UI组件,用于临时显示重要信息或获取用户输入,本文给大家介绍Android中Dialog的使用,感兴趣的朋友一起... 目录android中Dialog的使用详解1. 基本Dialog类型1.1 AlertDialog(

Android Kotlin 高阶函数详解及其在协程中的应用小结

《AndroidKotlin高阶函数详解及其在协程中的应用小结》高阶函数是Kotlin中的一个重要特性,它能够将函数作为一等公民(First-ClassCitizen),使得代码更加简洁、灵活和可... 目录1. 引言2. 什么是高阶函数?3. 高阶函数的基础用法3.1 传递函数作为参数3.2 Lambda

Android自定义Scrollbar的两种实现方式

《Android自定义Scrollbar的两种实现方式》本文介绍两种实现自定义滚动条的方法,分别通过ItemDecoration方案和独立View方案实现滚动条定制化,文章通过代码示例讲解的非常详细,... 目录方案一:ItemDecoration实现(推荐用于RecyclerView)实现原理完整代码实现

Android App安装列表获取方法(实践方案)

《AndroidApp安装列表获取方法(实践方案)》文章介绍了Android11及以上版本获取应用列表的方案调整,包括权限配置、白名单配置和action配置三种方式,并提供了相应的Java和Kotl... 目录前言实现方案         方案概述一、 androidManifest 三种配置方式

Java进阶学习之如何开启远程调式

《Java进阶学习之如何开启远程调式》Java开发中的远程调试是一项至关重要的技能,特别是在处理生产环境的问题或者协作开发时,:本文主要介绍Java进阶学习之如何开启远程调式的相关资料,需要的朋友... 目录概述Java远程调试的开启与底层原理开启Java远程调试底层原理JVM参数总结&nbsMbKKXJx

Android WebView无法加载H5页面的常见问题和解决方法

《AndroidWebView无法加载H5页面的常见问题和解决方法》AndroidWebView是一种视图组件,使得Android应用能够显示网页内容,它基于Chromium,具备现代浏览器的许多功... 目录1. WebView 简介2. 常见问题3. 网络权限设置4. 启用 JavaScript5. D

Android如何获取当前CPU频率和占用率

《Android如何获取当前CPU频率和占用率》最近在优化App的性能,需要获取当前CPU视频频率和占用率,所以本文小编就来和大家总结一下如何在Android中获取当前CPU频率和占用率吧... 最近在优化 App 的性能,需要获取当前 CPU视频频率和占用率,通过查询资料,大致思路如下:目前没有标准的

Android开发中gradle下载缓慢的问题级解决方法

《Android开发中gradle下载缓慢的问题级解决方法》本文介绍了解决Android开发中Gradle下载缓慢问题的几种方法,本文给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、网络环境优化二、Gradle版本与配置优化三、其他优化措施针对android开发中Gradle下载缓慢的问