Android : ExpandableListView(折叠列表) +BaseExpandableListAdapter-简单应用

示例图:

实体类DemoData.java

package com.example.myexpandablelistview.entity;public class DemoData {private  String content;private int img;public DemoData(String content, int img) {this.content = content;this.img = img;}public String getContent() {return content;}public void setContent(String content) {this.content = content;}public int getImg() {return img;}public void setImg(int img) {this.img = img;}
}

适配器 写个类继承 BaseExpandableListAdapter

package com.example.myexpandablelistview;import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;import com.example.myexpandablelistview.entity.DemoData;import java.util.ArrayList;
import java.util.List;// BaseAdapter 扩展
public class DemoExpandableListadpter extends BaseExpandableListAdapter {private List<String> groupDataList ;private List<ArrayList<DemoData>> itemDataList;private Context context;//优化static class HolderGroup{TextView titleTextView;}static class HolderChild{TextView contTextView;ImageView imageView;}public DemoExpandableListadpter(List<String> groupDataList, List<ArrayList<DemoData>> itemDataList, Context context) {this.groupDataList = groupDataList;this.itemDataList = itemDataList;this.context = context;}@Overridepublic int getGroupCount() {//返回 外面组个数 3个return groupDataList.size();}@Overridepublic int getChildrenCount(int groupPosition) {//groupPosition 外面组的下标 在获取里面子列表// 里面子列表个数return itemDataList.get(groupPosition).size();}@Overridepublic Object getGroup(int groupPosition) {//返回组每一项对应的数据return groupDataList.get(groupPosition);}@Overridepublic Object getChild(int groupPosition, int childPosition) {// 先获取对应的组下标,在获取子列表数据//返回子列表的每一项对应的数据return itemDataList.get(groupPosition).get(childPosition);}@Overridepublic long getGroupId(int groupPosition) {return groupPosition;}@Overridepublic long getChildId(int groupPosition, int childPosition) {return childPosition;}@Overridepublic boolean hasStableIds() {return false;}@Overridepublic View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {//组布局View 外面的HolderGroup  holderGroup = null;if(holderGroup == null){holderGroup = new HolderGroup();//设置视图布局convertView = LayoutInflater.from(context).inflate(R.layout.group_view,parent,false);holderGroup.titleTextView = convertView.findViewById(R.id.btn_title);convertView.setTag(holderGroup);}else {holderGroup = (HolderGroup) convertView.getTag();}//设置值holderGroup.titleTextView.setText(groupDataList.get(groupPosition));return convertView;}@Overridepublic View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {//里面的子布局HolderChild holderChild =null;if(holderChild == null){holderChild = new HolderChild();//设置视图布局convertView = LayoutInflater.from(context).inflate(R.layout.child_view,parent,false);holderChild.imageView = convertView.findViewById(R.id.btn_img);holderChild.contTextView = convertView.findViewById(R.id.btn_cont);convertView.setTag(holderChild);}else {holderChild = (HolderChild)convertView.getTag();}//设置值holderChild.imageView.setBackgroundResource(itemDataList.get(groupPosition).get(childPosition).getImg());holderChild.contTextView.setText(itemDataList.get(groupPosition).get(childPosition).getContent());return convertView;}//设置子列是否可以选中 true  false@Overridepublic boolean isChildSelectable(int groupPosition, int childPosition) {//false 点击事件无效return true;}
}

MainAcitvity.java

package com.example.myexpandablelistview;import androidx.appcompat.app.AppCompatActivity;import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.Toast;import com.example.myexpandablelistview.entity.DemoData;import java.util.ArrayList;
import java.util.List;public class MainActivity extends AppCompatActivity {//定义组件private ExpandableListView expandableListView;private Context context;private List<String> groupDataList = null;private List<ArrayList<DemoData>> itemDataList = null;private ArrayList<DemoData> itemData =null;//定义图片资源图片private int[] arr = {R.mipmap.e,R.mipmap.f,R.mipmap.g,R.mipmap.h,R.mipmap.i,};private int[] arr2 = {R.mipmap.g,R.mipmap.h,R.mipmap.i,};/*实现ExpandableAdapter的三种方式1.扩展BaseExpandableListAdpter实现ExpandableAdapter。2.使用SimpleExpandableListAdpater将两个List集合包装成ExpandableAdapter3.使用simpleCursorTreeAdapter将Cursor中的数据包装成SimpleCuroTreeAdapter使用的是第一个,扩展BaseExpandableListAdpter,我们需要重写该类中的相关方法*/private DemoExpandableListadpter demoExpandableListadpter = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//传入上下文context = this;expandableListView = findViewById(R.id.expandable_list_view);//添加数据itemDataList = new ArrayList<ArrayList<DemoData>>();groupDataList = new ArrayList<>();for(int i = 0;i < 3; i++){groupDataList.add("我是标题"+i);}//内容 和图片 1 循环添加出错 java.lang.IndexOutOfBoundsExceptionitemData = new ArrayList<DemoData>();for (int i =0 ; i< arr.length; i++){itemData.add(new DemoData("我是文字"+i,arr[i]));}itemDataList.add(itemData);//内容 和图片 2itemData = new ArrayList<DemoData>();for (int i =0 ; i< arr2.length; i++){itemData.add(new DemoData("我是文字"+i,arr2[i]));}itemDataList.add(itemData);//内容 和图片 3itemData = new ArrayList<DemoData>();for (int i =0 ; i< arr.length; i++){itemData.add(new DemoData("我是文字"+i,arr[i]));}itemDataList.add(itemData);//把数据传入demoExpandableListadpter = new DemoExpandableListadpter(groupDataList,itemDataList,context);//往视图容器中添加 适配器expandableListView.setAdapter(demoExpandableListadpter);//列表设置点击事件expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {@Overridepublic boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {Toast.makeText(context,"您选择了"+itemDataList.get(groupPosition).get(childPosition).getContent(),Toast.LENGTH_SHORT).show();return false;}});}
}

主布局:activity_main.xml

<?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"tools:context=".MainActivity"><ExpandableListViewandroid:id="@+id/expandable_list_view"android:layout_width="match_parent"android:layout_height="match_parent"/></LinearLayout>

组布局 group_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:orientation="vertical"android:layout_height="match_parent"><TextViewandroid:id="@+id/btn_title"android:layout_width="match_parent"android:layout_height="wrap_content"android:textSize="30sp"android:textStyle="bold"android:paddingLeft="50dp"android:text="123"/>
</LinearLayout>

子布局 child_view.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"><ImageViewandroid:id="@+id/btn_img"android:layout_width="60dp"android:layout_height="60dp"/><TextViewandroid:textSize="24sp"android:id="@+id/btn_cont"android:layout_width="match_parent"android:layout_height="60dp"android:gravity="center_vertical"android:text="1234"/></LinearLayout>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/163793.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

STM32——外部中断

文章目录 0.中断关系映射1.使能 IO 口时钟&#xff0c;初始化 IO 口为输入2.设置 IO 口模式&#xff0c;触发条件&#xff0c;开启 SYSCFG 时钟&#xff0c;设置 IO 口与中断线的映射关系。3.配置NVIC优先级管理&#xff0c;并使能中断4.编写中断服务函数。5.编写中断处理回调函…

springboot多数据源集成

springboot多数据源集成 1、添加依赖2、添加配置3、代码使用4、动态切换数据库 1、添加依赖 <!--多数据源--> <dependency><groupId>com.baomidou</groupId><artifactId>dynamic-datasource-spring-boot-starter</artifactId><version…

[个人笔记] Windows的IT运维笔记

IT技术 - 运维篇 第二章 Windows的IT运维笔记 IT技术 - 运维篇系列文章回顾一、Windows10专业版添加gpedit.msc二、海康威视前端页面导出通道名称参考链接 系列文章回顾 第一章 快速下载微软评估版本镜像的方法 一、Windows10专业版添加gpedit.msc 执行以下bat脚本 echo off…

Rust使用iced构建UI时,如何在界面显示中文字符

注&#xff1a;此文适合于对rust有一些了解的朋友 iced是一个跨平台的GUI库&#xff0c;用于为rust语言程序构建UI界面。 iced的基本逻辑是&#xff1a; UI交互产生消息message&#xff0c;message传递给后台的update&#xff0c;在这个函数中编写逻辑&#xff0c;然后通过…

护法革命:CIMIVO+SOTUY洗前发膜让发丝重获“芯”生

爱美之心人皆有之,经常烫染或者是在太阳下暴晒,都会对发丝造成一定的伤害,一旦发丝受损,就会导致发芯内部角蛋白流失、化学键连接断裂,进而出现各种发质问题。为此,日本知名化妆品集团NABOCUL旗下发芯修护引领品牌ENNEO创新研发两大核心成分:CIMIVO、SOTUY,能够从根源修护发芯内…

EXCEL小技巧

1、两列文本合并显示&#xff1a; CONCATENATE(B6,E6) &#xff08;如果显示公式而非文本&#xff0c;就是公式输错了&#xff0c;比如后缺少空格&#xff09;

mac 终端配置

Mac iTerm2 配置 安装 brew install iTerm2安装完成之后&#xff0c;需要重新打开终端&#xff0c;既可以看见安装 iTerm2 的效果。 iTerm2 美化 使用 oh-my-zsh 美化 iTerm2 终端 安装 brew install wget sh -c "$(wget https://raw.github.com/ohmyzsh/ohmyzsh/mast…

宇宙工具箱:办公娱乐两不误堪称手机的百宝箱

宇宙工具箱 宇宙工具箱&#xff0c;提供大量实用工具&#xff0c;多种类型尽在其中&#xff0c;支持图片/视频处理、计算、查询、娱乐等多方内容&#xff0c;满足用户使用需求&#xff0c;超多工具直接使用。 获取资源 详细获取地址请点击 宇宙工具箱 功能特点 1、计算工具…

【硬核HeyGen平替】在window平台上使用MyHeyGen

最近在研究HeyGen的平替开源项目&#xff0c;然后发现了MyHeyGen这个项目&#xff0c;但是文档上面并没有说明如果在window平台上使用&#xff0c;考虑到非window平台安装显卡驱动什么的比较繁琐&#xff0c;所以尝试硬着头皮干... 前提 开源项目中所需的环境准备要先准备好 1…

测试15k薪资第1步 —— 自动化测试理论基础

目录 1、自动化测试定义 2、自动化测试分类&工具 3、未来发展趋势 1.1、什么是自动化测试 自动化测试指的是利用软件工具或脚本来执行测试任务&#xff0c;以替代手动测试过程的一种测试方法。它的主要目的是通过自动化执行、验证和评估软件应用的功能、稳定性、性能等方面…

Kotlin(十一) 标准函数with、run和apply

with with函数接收两个参数&#xff1a;第一个参数可以是一个任意类型的对象&#xff0c;第二个参数是一个Lambda表达式。with函数会在Lambda表达式中提供第一个参数对象的上下文&#xff0c;并使用Lambda表达式中的最后一行代码作为返回值返回。示例代码如下&#xff1a; va…

python常用第三方模块 --- Pyinstaller(把程序打包成可执行程序)

打包方法&#xff1a; 在cmd下进行 pyinstaller -F 路径/程序文件名 注意&#xff1a;使用pyinstaller之前需要安装 pip install pyinstaller

ELK架构

经典的ELK 经典的ELK主要是由Filebeat Logstash Elasticsearch Kibana组成&#xff0c;如下图&#xff1a;&#xff08;早期的ELK只有Logstash Elasticsearch Kibana&#xff09; 此架构主要适用于数据量小的开发环境&#xff0c;存在数据丢失的危险。 整合消息队列Ngin…

如何用网格交易做ETF套利

ETF套利是指利用ETF基金的交易机制&#xff0c;通过短期的买卖差价或组合投资来获取利润。 具体来说&#xff0c;ETF套利最常用的套利方法则是&#xff1a;价格套利和波动套利。 1. 价格套利&#xff1a;当ETF二级市场的价格与一级市场的净值出现偏差时&#xff0c;投资者可以通…

【SwiftUI】7.预览及其内部机制

上一篇讲到了组件及组件化&#xff0c;从概念和优/缺点两个方向说明了组件化的意义&#xff0c;更为重要的是&#xff0c;组件和组件化是一个在编程领域&#xff0c;放之四海皆可以的概念&#xff0c;理解和运用它是非常必要的&#xff0c;希望大家能掌握。今天我们介绍另一个特…

Element UI的Tabs 标签页位置导航栏去除线条

在实际开发中&#xff0c;我们调整了相关样式&#xff0c;导致导航栏的相关样式跟随不上&#xff0c;如下图所示&#xff1a; 因为我跳转了前边文字的样式并以在导航栏添加了相关头像&#xff0c;导致右边的线条定位出现问题&#xff0c;我在想&#xff0c;要不我继续调整右边…

开发B2B商城的意义

开发B2B商城的意义主要体现在以下几个方面&#xff1a; 采购成本低&#xff1a;利用互联网采购&#xff0c;B2B商城的采购商可直接通过线上完成全部流程操作&#xff0c;在提高采购效率的同时&#xff0c;大大降低了B2B工业品企业采购成本。推广优势大&#xff1a;B2B商城的曝…

YM5411 WIFI 5模块 完美替代AP6256

YM5411是沃特沃德推出的一款低成本&#xff0c;低功耗的模块&#xff0c;该模块具有Wi-Fi&#xff08;2.4GHz和5GHz IEEE 802.11 a/b/g/n/ac&#xff09;蓝牙&#xff08;BT5.0&#xff09;功能&#xff0c;并通过了SRRC认证&#xff0c;带mesh&#xff0c;完美替换AP6256。高度…

OpenHarmony之NAPI框架介绍

张志成 诚迈科技高级技术专家 NAPI是什么 NAPI的概念源自Nodejs&#xff0c;为了实现javascript脚本与C库之间的相互调用&#xff0c;Nodejs对V8引擎的api做了一层封装&#xff0c;称为NAPI。可以在Nodejs官网&#xff08;https://nodejs.org/dist/latest-v20.x/docs/api/n-api…

【python爬虫】scrapy在pycharm 调试

scrapy在pycharm 调试 1、使用scrapy创建一个项目 scrapy startproject tutorial 2、在朋友pycharm中调试scrapy 2.1 通过文件run.py调试 在根目录下新建一个文件run.py(与scrapy.cfg文件的同一目录下), debug ‘run’即可 # -*- coding:utf-8 -*- from scrapy import c…