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

相关文章

重新对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

Android开发环境配置避坑指南

《Android开发环境配置避坑指南》本文主要介绍了Android开发环境配置过程中遇到的问题及解决方案,包括VPN注意事项、工具版本统一、Gerrit邮箱配置、Git拉取和提交代码、MergevsR... 目录网络环境:VPN 注意事项工具版本统一:android Studio & JDKGerrit的邮

Android实现定时任务的几种方式汇总(附源码)

《Android实现定时任务的几种方式汇总(附源码)》在Android应用中,定时任务(ScheduledTask)的需求几乎无处不在:从定时刷新数据、定时备份、定时推送通知,到夜间静默下载、循环执行... 目录一、项目介绍1. 背景与意义二、相关基础知识与系统约束三、方案一:Handler.postDel

Android使用ImageView.ScaleType实现图片的缩放与裁剪功能

《Android使用ImageView.ScaleType实现图片的缩放与裁剪功能》ImageView是最常用的控件之一,它用于展示各种类型的图片,为了能够根据需求调整图片的显示效果,Android提... 目录什么是 ImageView.ScaleType?FIT_XYFIT_STARTFIT_CENTE

Java学习手册之Filter和Listener使用方法

《Java学习手册之Filter和Listener使用方法》:本文主要介绍Java学习手册之Filter和Listener使用方法的相关资料,Filter是一种拦截器,可以在请求到达Servl... 目录一、Filter(过滤器)1. Filter 的工作原理2. Filter 的配置与使用二、Listen