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

相关文章

Android DataBinding 与 MVVM使用详解

《AndroidDataBinding与MVVM使用详解》本文介绍AndroidDataBinding库,其通过绑定UI组件与数据源实现自动更新,支持双向绑定和逻辑运算,减少模板代码,结合MV... 目录一、DataBinding 核心概念二、配置与基础使用1. 启用 DataBinding 2. 基础布局

Android ViewBinding使用流程

《AndroidViewBinding使用流程》AndroidViewBinding是Jetpack组件,替代findViewById,提供类型安全、空安全和编译时检查,代码简洁且性能优化,相比Da... 目录一、核心概念二、ViewBinding优点三、使用流程1. 启用 ViewBinding (模块级

Go学习记录之runtime包深入解析

《Go学习记录之runtime包深入解析》Go语言runtime包管理运行时环境,涵盖goroutine调度、内存分配、垃圾回收、类型信息等核心功能,:本文主要介绍Go学习记录之runtime包的... 目录前言:一、runtime包内容学习1、作用:① Goroutine和并发控制:② 垃圾回收:③ 栈和

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

重新对Java的类加载器的学习方式

《重新对Java的类加载器的学习方式》:本文主要介绍重新对Java的类加载器的学习方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、介绍1.1、简介1.2、符号引用和直接引用1、符号引用2、直接引用3、符号转直接的过程2、加载流程3、类加载的分类3.1、显示

Android NDK版本迭代与FFmpeg交叉编译完全指南

《AndroidNDK版本迭代与FFmpeg交叉编译完全指南》在Android开发中,使用NDK进行原生代码开发是一项常见需求,特别是当我们需要集成FFmpeg这样的多媒体处理库时,本文将深入分析A... 目录一、android NDK版本迭代分界线二、FFmpeg交叉编译关键注意事项三、完整编译脚本示例四

Android与iOS设备MAC地址生成原理及Java实现详解

《Android与iOS设备MAC地址生成原理及Java实现详解》在无线网络通信中,MAC(MediaAccessControl)地址是设备的唯一网络标识符,本文主要介绍了Android与iOS设备M... 目录引言1. MAC地址基础1.1 MAC地址的组成1.2 MAC地址的分类2. android与I

Android 实现一个隐私弹窗功能

《Android实现一个隐私弹窗功能》:本文主要介绍Android实现一个隐私弹窗功能,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 效果图如下:1. 设置同意、退出、点击用户协议、点击隐私协议的函数参数2. 《用户协议》、《隐私政策》设置成可点击的,且颜色要区分出来res/l

Android实现一键录屏功能(附源码)

《Android实现一键录屏功能(附源码)》在Android5.0及以上版本,系统提供了MediaProjectionAPI,允许应用在用户授权下录制屏幕内容并输出到视频文件,所以本文将基于此实现一个... 目录一、项目介绍二、相关技术与原理三、系统权限与用户授权四、项目架构与流程五、环境配置与依赖六、完整

Android 12解决push framework.jar无法开机的方法小结

《Android12解决pushframework.jar无法开机的方法小结》:本文主要介绍在Android12中解决pushframework.jar无法开机的方法,包括编译指令、框架层和s... 目录1. android 编译指令1.1 framework层的编译指令1.2 替换framework.ja