android ExpandableListActivity简介

2024-06-06 16:38

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

主函数
package com.example.expandlistactivity;import java.util.ArrayList;
import java.util.List;import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.widget.ExpandableListView;public class MainActivity extends ExpandableListActivity  {@Override  public void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.activity_main);  List<String> gl = new ArrayList<String>();  List<List<String>> cl = new ArrayList<List<String>>();  gl.add("gr1");  gl.add("gr2");  gl.add("gr3");  List<String> chil1 = new ArrayList<String>();  chil1.add("c11");  chil1.add("c12");  chil1.add("c13");  chil1.add("c14");  chil1.add("c15");  chil1.add("c16");  chil1.add("c17");  chil1.add("c18");  List<String> chil2 = new ArrayList<String>();  chil2.add("c21");  chil2.add("c22");  chil2.add("c23");  chil2.add("c24");  chil2.add("c25");  chil2.add("c26");  chil2.add("c27");  chil2.add("c28");  List<String> chil3 = new ArrayList<String>();  chil3.add("c31");  chil3.add("c32");  chil3.add("c33");  chil3.add("c34");  chil3.add("c35");  chil3.add("c36");  chil3.add("c37");  chil3.add("c38");  cl.add(chil1);  cl.add(chil2);  cl.add(chil3);  adapter e = new adapter(this, gl, cl);  ExpandableListView expandableListView = getExpandableListView();  // 设置适配器  expandableListView.setAdapter(e);  // 设置组图标 可以使用selector  expandableListView.setGroupIndicator(getResources().getDrawable(  R.drawable.away));  // 设置组图标位置  expandableListView.setIndicatorBounds(0, 180);  // 设置子项图标  expandableListView.setChildIndicator(getResources().getDrawable(  R.drawable.busy));  // 设置子项图标位置  expandableListView.setChildIndicatorBounds(0, 180);  }  
}


显示expandableListView的adapter

package com.example.expandlistactivity;import java.util.ArrayList;
import java.util.List;import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;public class adapter extends BaseExpandableListAdapter {  List<String> GroupList = new ArrayList<String>();  List<List<String>> ChildrenList = new ArrayList<List<String>>();  Context context = null;  public adapter(Context c, List<String> gl, List<List<String>> cl) {  context = c;  GroupList.addAll(gl);  ChildrenList.addAll(cl); }  // 返回组的总数  @Override  public int getGroupCount() {  return GroupList.size();  }  // 返回子项总数  @Override  public int getChildrenCount(int groupPosition) {  return ChildrenList.get(groupPosition).size();  }  // 返回组对象  @Override  public Object getGroup(int groupPosition) {  return GroupList.get(groupPosition);  }  // 返回子项对象  @Override  public Object getChild(int groupPosition, int childPosition) {  return ChildrenList.get(groupPosition).get(childPosition);  }  // 返回组ID  @Override  public long getGroupId(int groupPosition) {  return groupPosition;  }  // 返回子项ID  @Override  public long getChildId(int groupPosition, int childPosition) {  return childPosition;  }  // 是否具有固定ID  @Override  public boolean hasStableIds() {  return false;  }  // 返回组VIEW  @Override  public View getGroupView(int groupPosition, boolean isExpanded,  View convertView, ViewGroup parent) {  TextView tv = new TextView(context);  tv.setText(GroupList.get(groupPosition));  tv.setHeight(40);  return tv;  }  // 返回子项VIEW  @Override  public View getChildView(int groupPosition, int childPosition,  boolean isLastChild, View convertView, ViewGroup parent) {  TextView tv = new TextView(context);  
//        for(int i=0;i<childPosition + 1;i++){//这两种方式都可以在页面显示child
//            tv.setText(ChildrenList.get(groupPosition).get(i));  
//            tv.setHeight(40);  
//        } tv.setText(ChildrenList.get(groupPosition).get(childPosition));  tv.setHeight(40);  return tv;  }  // 子项是否可以被选中  @Override  public boolean isChildSelectable(int groupPosition, int childPosition) {  return true;  }  
}  

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="wrap_content" ></ExpandableListView></LinearLayout>


这篇关于android ExpandableListActivity简介的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Spring Security简介、使用与最佳实践

《SpringSecurity简介、使用与最佳实践》SpringSecurity是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架,本文给大家介绍SpringSec... 目录一、如何理解 Spring Security?—— 核心思想二、如何在 Java 项目中使用?——

Java Stream 并行流简介、使用与注意事项小结

《JavaStream并行流简介、使用与注意事项小结》Java8并行流基于StreamAPI,利用多核CPU提升计算密集型任务效率,但需注意线程安全、顺序不确定及线程池管理,可通过自定义线程池与C... 目录1. 并行流简介​特点:​2. 并行流的简单使用​示例:并行流的基本使用​3. 配合自定义线程池​示

Android协程高级用法大全

《Android协程高级用法大全》这篇文章给大家介绍Android协程高级用法大全,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友跟随小编一起学习吧... 目录1️⃣ 协程作用域(CoroutineScope)与生命周期绑定Activity/Fragment 中手

PostgreSQL简介及实战应用

《PostgreSQL简介及实战应用》PostgreSQL是一种功能强大的开源关系型数据库管理系统,以其稳定性、高性能、扩展性和复杂查询能力在众多项目中得到广泛应用,本文将从基础概念讲起,逐步深入到高... 目录前言1. PostgreSQL基础1.1 PostgreSQL简介1.2 基础语法1.3 数据库

Python库 Django 的简介、安装、用法入门教程

《Python库Django的简介、安装、用法入门教程》Django是Python最流行的Web框架之一,它帮助开发者快速、高效地构建功能强大的Web应用程序,接下来我们将从简介、安装到用法详解,... 目录一、Django 简介 二、Django 的安装教程 1. 创建虚拟环境2. 安装Django三、创

Android 缓存日志Logcat导出与分析最佳实践

《Android缓存日志Logcat导出与分析最佳实践》本文全面介绍AndroidLogcat缓存日志的导出与分析方法,涵盖按进程、缓冲区类型及日志级别过滤,自动化工具使用,常见问题解决方案和最佳实... 目录android 缓存日志(Logcat)导出与分析全攻略为什么要导出缓存日志?按需过滤导出1. 按

MySQL 索引简介及常见的索引类型有哪些

《MySQL索引简介及常见的索引类型有哪些》MySQL索引是加速数据检索的特殊结构,用于存储列值与位置信息,常见的索引类型包括:主键索引、唯一索引、普通索引、复合索引、全文索引和空间索引等,本文介绍... 目录什么是 mysql 的索引?常见的索引类型有哪些?总结性回答详细解释1. MySQL 索引的概念2

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class