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.pswp.cn/news/163793.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/163793.shtml
英文地址,請注明出處:http://en.pswp.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…