当前位置: 首页 > news >正文

【android】用 ExpandableListView 来实现 TreeView树形菜单视图

使用 ExpandableListView 来实现 TreeView

创建一个 ExpandableListAdapter 来为其提供数据。以下演示了如何使用 ExpandableListView 来展示树形结构的数据:

首先,在布局文件中添加 ExpandableListView:

<ExpandableListViewandroid:id="@+id/expandableListView"android:layout_width="match_parent"android:layout_height="match_parent"/>

接下来,创建一个 ExpandableListAdapter 类来管理树形结构的数据:

public class MyExpandableListAdapter extends BaseExpandableListAdapter {private Context context;private List<String> groupList; // 父项数据private Map<String, List<String>> childMap; // 子项数据public MyExpandableListAdapter(Context context, List<String> groupList, Map<String, List<String>> childMap) {this.context = context;this.groupList = groupList;this.childMap = childMap;}@Overridepublic Object getChild(int groupPosition, int childPosition) {return childMap.get(groupList.get(groupPosition)).get(childPosition);}@Overridepublic long getChildId(int groupPosition, int childPosition) {return childPosition;}@Overridepublic View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {String childText = (String) getChild(groupPosition, childPosition);if (convertView == null) {LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);convertView = inflater.inflate(R.layout.list_item, null);}TextView textView = convertView.findViewById(R.id.listItem);textView.setText(childText);return convertView;}@Overridepublic int getChildrenCount(int groupPosition) {return childMap.get(groupList.get(groupPosition)).size();}@Overridepublic Object getGroup(int groupPosition) {return groupList.get(groupPosition);}@Overridepublic int getGroupCount() {return groupList.size();}@Overridepublic long getGroupId(int groupPosition) {return groupPosition;}@Overridepublic View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {String groupText = (String) getGroup(groupPosition);if (convertView == null) {LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);convertView = inflater.inflate(R.layout.list_group, null);}TextView textView = convertView.findViewById(R.id.listGroup);textView.setText(groupText);return convertView;}@Overridepublic boolean hasStableIds() {return false;}@Overridepublic boolean isChildSelectable(int groupPosition, int childPosition) {return true;}
}

在上述代码中,我们创建了一个自定义的 ExpandableListAdapter,用于管理父项和子项的数据展示。在 getChildView 和 getGroupView 方法中,我们根据数据来填充父项和子项的视图。

最后,在 Activity 或者 Fragment 中设置 ExpandableListView 的适配器并提供数据:

ExpandableListView expandableListView = findViewById(R.id.expandableListView);
MyExpandableListAdapter adapter = new MyExpandableListAdapter(getContext(), groupList, childMap); // groupList 和 childMap 是你的数据
expandableListView.setAdapter(adapter);

list_item为名称或名称+logo

如果你想在 ExpandableListView 中实现第一级为名称,第二级为名称+logo的效果,你可以修改 MyExpandableListAdapter 类的 getChildView 方法来实现这个需求。以下是一个简单的示例代码,展示了如何为第二级添加名称和 logo:

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {String childText = (String) getChild(groupPosition, childPosition);if (convertView == null) {LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);convertView = inflater.inflate(R.layout.list_item, null);}TextView textView = convertView.findViewById(R.id.listItem);ImageView imageView = convertView.findViewById(R.id.listLogo); // 假设你在布局文件中有一个 ImageView 用于显示 logotextView.setText(childText);// 设置对应的 logoif (childText.equals("名称1")) {imageView.setImageResource(R.drawable.logo1);} else if (childText.equals("名称2")) {imageView.setImageResource(R.drawable.logo2);}return convertView;
}

在上述代码中,我们修改了 getChildView 方法,为每个子项设置了对应的 logo。假设你在布局文件中有一个 ImageView 用于显示 logo,我们根据子项的名称来设置对应的 logo。

这样,当你设置 ExpandableListView 的适配器后,每个第二级子项将会显示名称和对应的 logo。

布局文件

以下是一个简单的布局文件示例,用于在 ExpandableListView 中展示第一级为名称,第二级为名称+logo的效果:

list_group.xml(用于展示第一级名称):

<TextViewxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/listGroup"android:layout_width="match_parent"android:layout_height="wrap_content"android:padding="16dp"android:textSize="20sp"android:textColor="@android:color/black"/>

list_item.xml(用于展示第二级名称和logo):

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:padding="16dp"><TextViewandroid:id="@+id/listItem"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="18sp"android:textColor="@android:color/black"/><ImageViewandroid:id="@+id/listLogo"android:layout_width="24dp"android:layout_height="24dp"android:layout_marginStart="16dp"/>
</LinearLayout>
http://www.lryc.cn/news/385265.html

相关文章:

  • 策略模式与函数式编程应用
  • docker原理记录C-N-A
  • 【LeetCode】每日一题:二叉树的层次遍历
  • 单体架构改造为微服务架构之痛点解析
  • 马面裙的故事:汉服如何通过直播电商实现产业跃迁
  • SaaS产品运营:维护四个不同类型的合作伙伴的实战指南
  • 【监控】3.配置 Grafana 以使用 Prometheus 数据源
  • 【LinuxC语言】网络编程中粘包问题
  • Docker之jekins的安装
  • # bash: chkconfig: command not found 解决方法
  • Linux线程互斥锁
  • 展开说说:Android列表之RecyclerView
  • 等保2.0时,最常见的挑战是什么?
  • 基于Vue 3.x与TypeScript的PPTIST本地部署与无公网IP远程演示文稿
  • PHP的基本语法有哪些?
  • CSS的媒体查询:响应式布局的利器
  • 汇聚荣做拼多多运营第一步是什么?
  • NeRF从入门到放弃4: NeuRAD-针对自动驾驶场景的优化
  • docker环境部署ruoyi系统前后端分离项目
  • UI(二)控件
  • 【图像分类】Yolov8 完整教程 |分类 |计算机视觉
  • PyCharm 2024.1最新变化
  • 金融行业专题|某头部期货基于 K8s 原生存储构建自服务数据库云平台
  • DELL服务器 OpenManage监控指标解读
  • vscode下无法识别node、npm的问题
  • C语言之字符串处理函数
  • 昇思25天学习打卡营第4天|onereal
  • restTemplate使用总结
  • 【云服务器介绍】选择指南 腾讯云 阿里云全配置对比 搭建web 个人开发 app 游戏服务器
  • PostgreSQL 高级SQL查询(三)