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

相关文章

Android Mainline基础简介

《AndroidMainline基础简介》AndroidMainline是通过模块化更新Android核心组件的框架,可能提高安全性,本文给大家介绍AndroidMainline基础简介,感兴趣的朋... 目录关键要点什么是 android Mainline?Android Mainline 的工作原理关键

如何解决idea的Module:‘:app‘platform‘android-32‘not found.问题

《如何解决idea的Module:‘:app‘platform‘android-32‘notfound.问题》:本文主要介绍如何解决idea的Module:‘:app‘platform‘andr... 目录idea的Module:‘:app‘pwww.chinasem.cnlatform‘android-32

Android实现打开本地pdf文件的两种方式

《Android实现打开本地pdf文件的两种方式》在现代应用中,PDF格式因其跨平台、稳定性好、展示内容一致等特点,在Android平台上,如何高效地打开本地PDF文件,不仅关系到用户体验,也直接影响... 目录一、项目概述二、相关知识2.1 PDF文件基本概述2.2 android 文件访问与存储权限2.

Android Studio 配置国内镜像源的实现步骤

《AndroidStudio配置国内镜像源的实现步骤》本文主要介绍了AndroidStudio配置国内镜像源的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、修改 hosts,解决 SDK 下载失败的问题二、修改 gradle 地址,解决 gradle

在Android平台上实现消息推送功能

《在Android平台上实现消息推送功能》随着移动互联网应用的飞速发展,消息推送已成为移动应用中不可或缺的功能,在Android平台上,实现消息推送涉及到服务端的消息发送、客户端的消息接收、通知渠道(... 目录一、项目概述二、相关知识介绍2.1 消息推送的基本原理2.2 Firebase Cloud Me

Android中Dialog的使用详解

《Android中Dialog的使用详解》Dialog(对话框)是Android中常用的UI组件,用于临时显示重要信息或获取用户输入,本文给大家介绍Android中Dialog的使用,感兴趣的朋友一起... 目录android中Dialog的使用详解1. 基本Dialog类型1.1 AlertDialog(

Android Kotlin 高阶函数详解及其在协程中的应用小结

《AndroidKotlin高阶函数详解及其在协程中的应用小结》高阶函数是Kotlin中的一个重要特性,它能够将函数作为一等公民(First-ClassCitizen),使得代码更加简洁、灵活和可... 目录1. 引言2. 什么是高阶函数?3. 高阶函数的基础用法3.1 传递函数作为参数3.2 Lambda

Android自定义Scrollbar的两种实现方式

《Android自定义Scrollbar的两种实现方式》本文介绍两种实现自定义滚动条的方法,分别通过ItemDecoration方案和独立View方案实现滚动条定制化,文章通过代码示例讲解的非常详细,... 目录方案一:ItemDecoration实现(推荐用于RecyclerView)实现原理完整代码实现

Android App安装列表获取方法(实践方案)

《AndroidApp安装列表获取方法(实践方案)》文章介绍了Android11及以上版本获取应用列表的方案调整,包括权限配置、白名单配置和action配置三种方式,并提供了相应的Java和Kotl... 目录前言实现方案         方案概述一、 androidManifest 三种配置方式

Android WebView无法加载H5页面的常见问题和解决方法

《AndroidWebView无法加载H5页面的常见问题和解决方法》AndroidWebView是一种视图组件,使得Android应用能够显示网页内容,它基于Chromium,具备现代浏览器的许多功... 目录1. WebView 简介2. 常见问题3. 网络权限设置4. 启用 JavaScript5. D