之前使用的SimpleExpandableListAdapter有较大局限性,样式单一,修改难度大,这里不建议使用,而是利用BaseExpandableListAdapter,其实SimpleExpandableListAdapter也是继承自BaseExpandableListAdapter,这里用到的布局和上一个一样,先定义一个内部类MyExpandableListViewAdapter继承自BaseExpandableListAdapter,需要实现几个方法:

1.getGroupCount() 返回父元素的个数

2.getChildrenCount(int groupPosition) 返回当前父元素下的子元素个数

3.getGroup(int groupPosition) 返回当前父元素的数据

4.getChild(int groupPosition, int childPosition) 返回当前父元素下子元素的数据

5.getGroupId(int groupPosition) 返回当前父元素的id

6.getChildId(int groupPosition, int childPosition) 返回当前父元素下的子元素的id

7.hasStableIds() 是否有稳定的id,这里默认false不修改

8.getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) 返回当前父元素的view

9.getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) 返回当前子元素的view

10.isChildSelectable(int groupPosition, int childPosition) 是否可以选择,默认false,这里用不到,不修改

SecondActivity

package com.fitsoft;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView; public class SecondActivity extends AppCompatActivity { ExpandableListView expandableListView; MyExpandableListViewAdapter myExpandableListViewAdapter; String[] groupStringArr = {"腾讯", "百度", "阿里巴巴"}; String[][] childStringArr = {
{"QQ","微信","QQ浏览器"},
{"百度搜索","百度地图","百度外卖"},
{"淘宝","支付宝","天猫商城"}
}; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); expandableListView = findViewById(R.id.expandable_ListView); myExpandableListViewAdapter = new MyExpandableListViewAdapter(); expandableListView.setAdapter(myExpandableListViewAdapter); } class MyExpandableListViewAdapter extends BaseExpandableListAdapter { @Override
public int getGroupCount() {
return groupStringArr.length;
} @Override
public int getChildrenCount(int groupPosition) {
return childStringArr[groupPosition].length;
} @Override
public Object getGroup(int groupPosition) {
return groupStringArr[groupPosition];
} @Override
public Object getChild(int groupPosition, int childPosition) {
return childStringArr[groupPosition][childPosition];
} @Override
public long getGroupId(int groupPosition) {
return groupPosition * 100;
} @Override
public long getChildId(int groupPosition, int childPosition) {
return groupPosition * 100 + childPosition;
} @Override
public boolean hasStableIds() {
return false;
} @Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { View view = convertView;
if(view == null){
view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_group, parent, false);
} Object data = getGroup(groupPosition); TextView textView = view.findViewById(R.id.tv_group);
textView.setText(data.toString()); if(groupPosition % 2 == 1){
textView.setTextColor(Color.RED);
}else{
textView.setTextColor(Color.BLUE);
} return view;
} @Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { View view = convertView;
if(view == null){
view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_child, parent, false);
} TextView textView = view.findViewById(R.id.tv_child); Object data = getChild(groupPosition, childPosition); textView.setText(data.toString()); if(childPosition > 1){
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
}else{
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
} return view;
} @Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
}

这里的数据源用的是上一个数据源,主要实现的方法是getGroupViewgetChildView,另外要注意的是,设置id的时候,为避免父元素和子元素id重复,设置父元素位置*100为其真正id,这样只要子元素不超过100个,就不会重复。

View view = convertView;
if(view == null){
view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_group, parent, false);
}

利用这几句代码判断当前view是否需要新建,否则使用缓存

Object data = getGroup(groupPosition);

从数据源中提取相应数据

TextView textView = view.findViewById(R.id.tv_group);
textView.setText(data.toString());

并为相应的TextView设置文本

if(groupPosition % 2 == 1){
textView.setTextColor(Color.RED);
}else{
textView.setTextColor(Color.BLUE);
}

为了看出自定义效果,在这里为文本设置颜色,其中数组位置为奇数的设置红色,偶数的设置蓝色。

子元素的设置类似父元素,在子元素中只是区分字体的大小,不赘述。

效果图:

![](https://i.loli.net/2019/09/10/JqDkh9C42dob5YW.gif)

ExpandableListView之BaseExpandableListAdapter的更多相关文章

  1. 可展开的列表组件——ExpandableListView深入解析

    可展开的列表组件--ExpandableListView深入解析 一.知识点 1.ExpandableListView常用XML属性 2.ExpandableListView继承BaseExpanda ...

  2. android适配器Adapter

    一.什么是适配器,适配器有什么用? 适配器是AdapterView视图(如ListView - 列表视图控件.Gallery - 缩略图浏览器控件.GridView - 网格控件.Spinner - ...

  3. android 编程小技巧(持续中)

    first:     Intent跳转一般存用于Activity类,可是若要在非activity类里跳转的话,解决方法是在startActivity(intent)前加mContext即上下文,终于为 ...

  4. Android ExpandableListView BaseExpandableListAdapter (类似QQ分组列表)

    分组列表视图(ExpandableListView) 和ListView不同的是它是一个两级的滚动列表视图,每一个组可以展开,显示一些子项,类似于QQ列表,这些项目来至于ExpandableListA ...

  5. Android开发之ExpandableListView扩展(BaseExpandableListAdapter的使用)(完整版)

    Android开发之ExpandableListView扩展(BaseExpandableListAdapter的使用)(完整版)

  6. Android中使用ExpandableListView实现微信通讯录界面(完善仿微信APP)

    之前的博文<Android中使用ExpandableListView实现好友分组>我简单介绍了使用ExpandableListView实现简单的好友分组功能,今天我们针对之前的所做的仿微信 ...

  7. Android中使用ExpandableListView实现好友分组

    一个视图显示垂直滚动两级列表中的条目.这不同于列表视图,允许两个层次,类似于QQ的好友分组.要实现这个效果的整体思路为: 1.要给ExpandableListView 设置适配器,那么必须先设置数据源 ...

  8. 安卓开发树形控件之ExpandableListView(一)

    这个例子非常简单,简单到一个初学者都能随便开发出来,今天的目的仅仅只是为了将效果实现出来,如果想深入这里有几篇非常不错的博客: Android 之ExpandableListView几个特殊的属性 h ...

  9. ExpandableListView实现展开更多和收起更多

    [需求]: 如上面图示 当点开某个一级菜单的时候,其他菜单收起: 子级菜单默认最多5个: 多于5个的显示"展开更多" 点击"展开更多",展开该级所有子级菜单,同 ...

随机推荐

  1. 使用 .NET CORE 创建 项目模板,模板项目,Template

    场景:日常工作中,你可能会碰到需要新建一个全新的解决方案的情况(如公司新起了一个新项目,需要有全新配套的后台程序),如果公司内部基础框架较多.解决方案需要DDD模式等,那么从新起项目到各种依赖引用到能 ...

  2. Sqlserver 锁表查询代码记录

    --方法1WITH CTE_SID ( BSID, SID, sql_handle ) AS ( SELECT blocking_session_id , session_id , sql_handl ...

  3. 开源题材征集 + MVC&EF Core 完整教程小结

    到目前为止,我们的MVC+EF Core 完整教程的理论部分就全部结束了,共20篇,覆盖了核心的主要知识点. 下一阶段是实战部分,我们将会把这些知识点串联起来,用10篇(天)来完成一个开源项目. 现向 ...

  4. Docker系列之AspNetCore Runtime VS .NetCore Runtime VS .NET Core SDK(四)

    前言 接下来我们就要慢慢步入在.NET Core中使用Docker的殿堂了,在开始之前如题,我们需要搞清楚一些概念,要不然看到官方提供如下一系列镜像,我们会一脸懵逼,不知道到底要使用哪一个. AspN ...

  5. Linux设备驱动程序学习----3.模块的编译和装载

    模块的编译和装载 更多内容请参考Linux设备驱动程序学习----目录 1. 设置测试系统 第1步,要先从kernel.org的镜像网站上获取一个主线内核,并安装到自己的系统中,因为学习驱动程序的编写 ...

  6. 学习 Object-C: 简史

    对于一门语言的历史,我认为写一本书可能都不为过,关键是看你如何介绍和表达.当然每一个人的理解也大相径庭.本文阐述也仅仅只是冰山一角,如果需要深入了解,自己可能需要多花费一些心思. 这里也不会给大家说太 ...

  7. docker An error occurred 虚拟化错误解决

    问题: 本人电脑上装了VMware和docker,系统是win10专业版,然后今天想用下docker,打开报错,Hyper-V未开启,开启之后再次报错 An error occurred Hardwa ...

  8. 人员考勤,MySQLl数据库一个表自动生成3表筛选人员迟到早退缺勤

    前言:漂亮的人事小姐姐找我帮忙弄考勤:由于人员考勤和门禁一起,打卡记录太多,打卡机只能导出一个打卡Excel总表,不容易人工筛选. Excel表的格式是这样的:(这里101代替真实人名) 实现目标: ...

  9. PHP面相对象编程-重载、覆盖(重写) 多态、接口

    http://www.ctolib.com/topics-21262.html http://cnn237111.blog.51cto.com/2359144/1284085 http://blog. ...

  10. 原生js之Ajax

    1.什么是Ajax? 全称:Asynchronous JavaScript and XML  (异步的 JavaScript和 xml),是前后端数据交互的一种技术(前端通过 Ajax 发送http请 ...