前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Android从零单排系列二十一】《Android视图控件——ExpandableListView》

【Android从零单排系列二十一】《Android视图控件——ExpandableListView》

作者头像
再见孙悟空_
发布2023-07-17 20:27:24
2230
发布2023-07-17 20:27:24
举报

前言

小伙伴们,在上文中我们介绍了Android视图组件ListView,本文我们继续盘点,介绍一下视图控件的ExpandableListView。

一 ExpandableListView基本介绍

ExpandableListView是Android中的一个可扩展列表视图,它继承自ListView,并提供了支持展开和折叠的功能。ExpandableListView可以展示带有分组和子项的层次结构数据,让用户可以方便地通过展开和折叠操作来浏览和查看更多的内容。

以下是对ExpandableListView的一些基本特性和用法:

  1. 数据源:ExpandableListView需要一个适配器(ExpandableListAdapter)来提供数据源。适配器为每个分组和子项提供数据,并负责渲染它们的视图。
  2. 分组和子项布局:你可以定义自己的分组项布局和子项布局,包括文本、图像和其他UI元素的组合。通过适配器,将数据绑定到各个视图上。
  3. 分组展开与折叠:用户可以点击分组项来展开或折叠子项。这样可以在有限的空间内显示大量的分组和子项,提供更好的用户体验。
  4. 点击事件处理:可以为分组项和子项设置点击事件监听器,以响应用户的点击操作。例如,可以在用户点击子项时执行某个操作或显示详细信息。
  5. 定制样式和行为:你可以通过样式和属性来自定义ExpandableListView的外观和行为,如分组项的指示箭头、分割线样式等。

二 ExpandableListView使用方法

在 XML 布局文件中添加 ExpandableListView:

代码语言:javascript
复制
<ExpandableListView
    android:id="@+id/expandableListView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

准备数据源:为 ExpandableListView 提供分组项和子项的数据。通常情况下,你可以使用一个适配器(Adapter)来管理数据。

代码语言:javascript
复制
List<String> groupList = new ArrayList<>(); // 分组项数据列表
Map<String, List<String>> childMap = new HashMap<>(); // 子项数据映射

// 添加分组项数据
groupList.add("Group 1");
groupList.add("Group 2");
groupList.add("Group 3");

// 添加子项数据
List<String> childList1 = new ArrayList<>();
childList1.add("Child 1-1");
childList1.add("Child 1-2");
childMap.put(groupList.get(0), childList1);

List<String> childList2 = new ArrayList<>();
childList2.add("Child 2-1");
childList2.add("Child 2-2");
childList2.add("Child 2-3");
childMap.put(groupList.get(1), childList2);

// 继续添加其他分组项和子项数据...

创建适配器(Adapter):创建一个适配器类,并继承自 BaseExpandableListAdapter,实现必要的方法以提供数据和视图绑定。

代码语言:javascript
复制
public class MyExpandableListAdapter extends BaseExpandableListAdapter {

    private List<String> groupList;
    private Map<String, List<String>> childMap;

    public MyExpandableListAdapter(List<String> groupList, Map<String, List<String>> childMap) {
        this.groupList = groupList;
        this.childMap = childMap;
    }

    // 实现必要的方法...

    @Override
    public int getGroupCount() {
        return groupList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        String groupName = groupList.get(groupPosition);
        List<String> childList = childMap.get(groupName);
        return childList != null ? childList.size() : 0;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        String groupName = groupList.get(groupPosition);
        List<String> childList = childMap.get(groupName);
        return childList != null ? childList.get(childPosition) : null;
    }

    // 其他方法...
}

在代码中设置适配器和监听器:

代码语言:javascript
复制
// 获取 ExpandableListView
ExpandableListView expandableListView = findViewById(R.id.expandableListView);

// 创建适配器
MyExpandableListAdapter adapter = new MyExpandableListAdapter(groupList, childMap);

// 设置适配器
expandableListView.setAdapter(adapter);

// 设置分组项点击事件监听器
expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
    @Override
    public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
        // 处理分组项点击事件
        return false; // 返回 true 可拦截事件,不会展开或折叠分组项
    }
});

// 设置子项点击事件监听器
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
    @Override
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
        // 处理子项点击事件
        return true; // 返回 true 可拦截事件,不会触发默认的选择效果
    }
});

三 ExpandableListView常见属性及方法

常用方法:

  1. setAdapter(Adapter adapter):设置 ExpandableListView 的适配器,用于提供数据和视图绑定。
  2. expandGroup(int groupPosition):展开指定位置(groupPosition)的分组项。
  3. collapseGroup(int groupPosition):折叠指定位置(groupPosition)的分组项。
  4. isGroupExpanded(int groupPosition):检查指定位置(groupPosition)的分组项是否处于展开状态。

常用属性:

  1. groupIndicator:用于指示分组项的展开和折叠状态的图标。可以通过设置不同的资源文件或自定义的 Drawable 来改变分组指示器的样式。
  2. childIndicator:用于指示子项的展开和折叠状态的图标。与 groupIndicator 类似,可根据需要进行自定义。
  3. divider:分割线的样式,用于分隔不同的分组项和子项。
  4. dividerHeight:分割线的高度,可以通过设置具体的像素值或使用 wrap_content、match_parent 等来调整大小。
  5. onGroupClickListener:用于设置分组项的点击事件监听器,可以在用户点击分组项时执行相应的操作。
  6. onChildClickListener:用于设置子项的点击事件监听器,可以在用户点击子项时执行相应的操作。

四 总结

ExpandableListView提供了一种方便的方式来展示具有层次结构的列表数据,并允许用户通过展开和折叠操作来浏览更多内容。它通常用于菜单、分类列表、可折叠的评论或帖子等场景。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-06-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 一 ExpandableListView基本介绍
  • 二 ExpandableListView使用方法
  • 三 ExpandableListView常见属性及方法
  • 四 总结
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档