前述

一般我们在使用ExpandableListView的时候,都是把Group和Child的数据都加载到适配器中。
现在有一个问题:那就是假如说Child的数据量过大的时候,全部加载下来是否造成浪费,由此,
我们想到在点击Group对应的分组的时候,再去获取数据,现需现取,比较靠谱。

实现

一些准备工作:
1. UI :MyExpandableListView (由于是嵌套在ScrollView中,所有需要自定义)
2. Adapter:MyMaterialExpandableAdapter
3. 数据源:Group的数据源,Child的数据源

贴代码
1. MyExpandableListView

/*** 重写ExpandableListView以解决ScrollView嵌套ExpandableListView* Created by llay on 2017/9/12.*/public class MyExpandableListView extends ExpandableListView {public MyExpandableListView(Context context) {super(context);}public MyExpandableListView(Context context, AttributeSet attrs) {super(context, attrs);}public MyExpandableListView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);super.onMeasure(widthMeasureSpec, expandSpec);}}
  1. MyMaterialExpandableAdapter
public class MyMaterialExpandableAdapter extends BaseExpandableListAdapter {private List<String> groupMapList;private List<List<MaterialEntity>> childMapList;private Context mContext;public MyMaterialExpandableAdapter(Context context, List<String> groupMapList, List<List<MaterialEntity>> childMapList) {mContext = context;this.groupMapList = groupMapList;this.childMapList = childMapList;}@Overridepublic int getGroupCount() {return groupMapList.size();}@Overridepublic int getChildrenCount(int groupPosition) {return childMapList.get(groupPosition).size();}@Overridepublic Object getGroup(int groupPosition) {return groupMapList.get(groupPosition);}@Overridepublic Object getChild(int groupPosition, int childPosition) {return childMapList.get(groupPosition).get(childPosition);}@Overridepublic long getGroupId(int groupPosition) {return groupPosition;}@Overridepublic long getChildId(int groupPosition, int childPosition) {return childPosition;}@Overridepublic boolean hasStableIds() {return true;}@Overridepublic View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {View view = convertView;GroupHolder holder = null;if (view == null) {holder = new GroupHolder();view = LayoutInflater.from(mContext).inflate(R.layout.item_material_group_expand, null);holder.groupName = view.findViewById(R.id.item_group_left_text);holder.groupRightText = view.findViewById(R.id.item_group_right_text);view.setTag(holder);} else {holder = (GroupHolder) view.getTag();}//判断是否已经打开列表if (isExpanded) {Drawable drawable = mContext.getResources().getDrawable(R.mipmap.icon_arr_up);drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());holder.groupRightText.setCompoundDrawables(null, null, drawable, null);} else {Drawable drawable = mContext.getResources().getDrawable(R.mipmap.icon_arr_down);drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());holder.groupRightText.setCompoundDrawables(null, null, drawable, null);}String _title = groupMapList.get(groupPosition);if (!TextUtils.isEmpty(_title)) {holder.groupName.setText(_title);} else {holder.groupName.setText("");}return view;}@Overridepublic View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {View view = convertView;ChildHolder holder = null;if (view == null) {holder = new ChildHolder();view = LayoutInflater.from(mContext).inflate(R.layout.item_material_child_expand, null);holder.mScope = view.findViewById(R.id.item_material_scope);holder.mName = view.findViewById(R.id.item_material_name);holder.mPhone = view.findViewById(R.id.item_material_phone);holder.mLocaiton = view.findViewById(R.id.item_material_location);holder.mMaterial = view.findViewById(R.id.item_material_material);view.setTag(holder);} else {holder = (ChildHolder) view.getTag();}String _scope = childMapList.get(groupPosition).get(childPosition).getScope();if (!TextUtils.isEmpty(_scope)) {holder.mScope.setText(_scope);} else {holder.mScope.setText("");}String _name = childMapList.get(groupPosition).get(childPosition).getTeamName();if (!TextUtils.isEmpty(_name)) {holder.mName.setText(_name);} else {holder.mName.setText("");}String _phone = childMapList.get(groupPosition).get(childPosition).getTeamPhone();if (!TextUtils.isEmpty(_phone)) {holder.mPhone.setText(_phone);} else {holder.mPhone.setText("");}String _location = childMapList.get(groupPosition).get(childPosition).getStorageLocation();if (!TextUtils.isEmpty(_location)) {holder.mLocaiton.setText(_location);} else {holder.mLocaiton.setText("");}String _material = childMapList.get(groupPosition).get(childPosition).getMaterialName();if (!TextUtils.isEmpty(_material)) {holder.mMaterial.setText(_material);} else {holder.mLocaiton.setText("");}return view;}@Overridepublic boolean isChildSelectable(int groupPosition, int childPosition) {return true;}class GroupHolder {public TextView groupName;public TextView groupRightText;}class ChildHolder {public TextView mScope;public TextView mName;public TextView mPhone;public TextView mLocaiton;public TextView mMaterial;}
}

适配器Group和Child的布局很简单。

  1. 数据源自己上哦

重点到了

1.mHistoryExpandMaterialListView.setOnGroupClickListener(this);
监听ExpandListView的Group的点击事件。

@Overridepublic boolean onGroupClick(ExpandableListView _expandableListView, View _view, int _i, long _l) {//如果分组被打开 直接关闭if (mHistoryExpandMaterialListView.isGroupExpanded(_i)) {mHistoryExpandMaterialListView.collapseGroup(_i);} else {String _groupString = (String) mMyMaterialExpandableAdapter.getGroup(_i);if (!TextUtils.isEmpty(_groupString)) {//记录点击的第几个mClickIndex = _i;//Engine中请求数据mHistoryDetailEngine.getCailiaoInfo(_groupString);}}return true;}

2.加载完成数据后,需要打开Group,显示加载的数据

@Overridepublic void getCailiaoOnSuccess(List<MaterialEntity> _listEntities) {mLists.get(mClickIndex).clear();mLists.get(mClickIndex).addAll(_listEntities);//打开记录的第几个Group进行显示mHistoryExpandMaterialListView.expandGroup(mClickIndex, true);}

ExpandableListView点击Group动态获取Child数据源相关推荐

  1. Android中列表动态删除item,如何删除Android ExpandableListView中某个group item的child item?...

    自定义了一个expandablelistview 想要删除其中的子项 list使用remove方法把list中的指定项删除了 11-09 21:04:20.585: I/MainActivity(13 ...

  2. java获取spring数据源_Spring动态注册多数据源的实现方法

    最近在做SaaS应用,数据库采用了单实例多schema的架构(详见参考资料1),每个租户有一个独立的schema,同时整个数据源有一个共享的schema,因此需要解决动态增删.切换数据源的问题. 在网 ...

  3. 一文读懂Spring动态配置多数据源---源码详细分析

    Spring动态多数据源源码分析及解读 一.为什么要研究Spring动态多数据源 代云小说网 https://www.3187.info ​ 期初,最开始的原因是:想将答题服务中发送主观题答题数据给批 ...

  4. Spring Boot 如何动态切换多数据源?

    大约在19年的这个时候,老同事公司在做医疗系统,需要和HIS系统对接一些信息,比如患者.医护.医嘱.科室等信息.但是起初并不知道如何与HIS无缝对接,于是向我取经. 最终经过讨论采用了视图对接的方式, ...

  5. android 动态获取权限有哪些,Android 6.0+ 动态获取权限

    Android 6.0+ 动态获取权限 这里有一个现成的库,可以直接拿来用.方便简单 1.向app下的gradle添加依赖: dependencies{ // android 6.0+ 动态获取权限 ...

  6. ajax怎样获得表头信息,layui.table动态获取表头和列表数据示例

    layui.table动态获取表头和列表数据示例 2020年07月14日 | 萬仟网IT编程 | 我要评论 ```javascript//表格layui.use('table', function() ...

  7. 【Android 安装包优化】移除无用资源 ( 自动移除无用资源 | 直接引用资源 | 动态获取资源 id | Lint 检查资源 )

    文章目录 一.自动移除无用资源 ( 不推荐使用 ) 二.直接引用资源与动态获取资源 1.直接引用资源 2.动态获取资源 id 三.Lint 检查资源 四.参考资料 一.自动移除无用资源 ( 不推荐使用 ...

  8. vue-preview动态获取图片宽高并增加旋转功能

    vue-preview是一个常用的图片查看器,微博网页版就是用的这个插件: 我在项目中也用过这个插件,总体来说,还是比较满意.但是缺少一个图片旋转功能. 安装使用 第一步:安装 npm i vue-p ...

  9. struts2原理分析之反射技术动态获取属性

    反射技术动态获取属性 知道struts2的流程的乡亲们都知道.struts2采用了动态获取属性的方法, 将表单里的数据传给了Action. 例如; 在struts2里有如下配置文件 <actio ...

  10. 小程序点击地图气泡获取气泡_气泡上的气泡

    小程序点击地图气泡获取气泡 Combining two colors that are two steps apart on the Color Wheel creates a Diad Color ...

最新文章

  1. Web前端经典面试试题(二)
  2. 学python是看书还是看视频-自学Python是看书还是看视频?
  3. 用ASP实现在线压缩与解压缩
  4. 小师妹学JVM之:GC的垃圾回收算法
  5. shell字体颜色应用
  6. fluent瞬态_Java中的瞬态关键字及其使用
  7. lodash 学习资料
  8. 转型个股赚钱机会最大--封起“345”选股
  9. Excel多表头导出(.net)
  10. 华为odjava机试题_华为机试题及答案
  11. quartus仿真24:数据选择器MUX四选一74153八选一74151
  12. envi精度评定_利用ArcGIS+envi实现遥感分类精度评价(分层抽样法),ArcGISENVI,评估,的...
  13. 基于ENVI下的土地利用信息提取(三)
  14. 使用 Entrust 扩展包在 Laravel 5 中实现 RBAC 权限管理与安装配置
  15. Java去除首尾指定字符串
  16. Linux系统ssd硬盘擦除,如何实现安全擦除 _固态硬盘小Z聊固态-中关村在线
  17. Linux 安装rabbitMQ guest账号登录总是提示失败
  18. Java开发常用软件列表——持续更新
  19. 计算机的击键方法教学教案,2.2 敲击键盘 教案
  20. 监控电脑屏幕python

热门文章

  1. 传输层协议(7):滑动窗口(1)
  2. Dlib-人脸识别API说明
  3. Linux 和 Android 系统性能分析
  4. sofia-sip帮助文档
  5. cgroup学习(三)——伪文件
  6. 内核调试神器SystemTap — 更多功能与原理(三)
  7. 事物(二)之客户端事务应答匹配
  8. missing arguments for method toArray in trait Collection
  9. 【图论】用匈牙利算法找女朋友(纯爱党的大胜利)
  10. NYOJ题目106-背包问题(贪心)