鸿蒙HarmonyOS之使用ArkTs语言实现层级树状目录选择UI

本文主要是介绍鸿蒙HarmonyOS之使用ArkTs语言实现层级树状目录选择UI,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、实现效果

在这里插入图片描述
在这里插入图片描述

二、实现步骤

代码示例中用到的颜色、图片等资源可以自行替换设置

1、Index.ets 里面调用

import { CategoryView} from './CategoryView';//主页面
@Entry
@Component
struct Index {@State tabsIndex: number = 0;build() {...//层级目录ViewCategoryView()...}   
}

3、DirectoryItem.ets 目录数据存储对象

//目录Item
@Observed
export class DirectoryItem {id: string = ""; //idname: string = ""; //名称type: number;//类型:几级目录children: DirectoryItem[]; //子目录isExpand: boolean = false;constructor(id: string, name: string,type: number, children?: DirectoryItem[]) {this.id = id;this.name = name;this.type = type;this.children = children || [];}
}

3、DirectoryData.ets 目录示例数据

import { DirectoryItem } from '../bean/DirectoryItem';//目录数据
export const directoryStructure: DirectoryItem[] = [new DirectoryItem('1','默认目录1',1, [new DirectoryItem('2','1.1',2, [new DirectoryItem('3','1.1.1',3),new DirectoryItem('4','1.1.2',3, [new DirectoryItem('5','(1)',4), new DirectoryItem('6','(2)',4)]),new DirectoryItem('7','1.1.3',3)]),new DirectoryItem('8','1.2',2),new DirectoryItem('9','1.3',2, [new DirectoryItem('10','1.3.1',3), new DirectoryItem('11','1.3.2',3)])])];

4、CategoryView.ets 目录UI

import { DirectoryItem } from '../bean/DirectoryItem'
import { directoryStructure } from '../data/DirectoryData'//云传页
@Preview
@Component
export struct Tab_CloudUploadContent {build() {Column() {Row() {Text($r('目录')).fontSize(30).fontFamily('HarmonyHeiTi-Medium').fontColor($r('app.color.font_color_dark'))}.width('100%').height(80)//选择目录UIif (directoryStructure.length > 0){List() {ForEach(directoryStructure, (data: DirectoryItem, index: number) => {ListItem() {DirectoryComponent({ item: directoryStructure[index] })}})}.width('100%').height('100%').padding({ left: 10, right: 10 }).divider({ strokeWidth: 1, color: $r('app.color.background_shallow_grey') })}}.width('100%').height('100%').padding(10)}
}//目录组件
@Component
struct DirectoryComponent {@ObjectLink item: DirectoryItem; //当前目录Item对象@State selectId: string = ''; //选中目录Id@State isOpen: boolean = false; //记录目录是否展开状态build() {Column() {Row(){Row() {Text("|").fontSize(15).fontWeight(FontWeight.Bold).fontColor($r('app.color.tab_bar_select'))//目录名称Text(this.item.name).fontSize(15).fontWeight(FontWeight.Bold).margin({ left: 8 }).fontColor($r('app.color.font_color_dark'))}.layoutWeight(1).margin({ left: (this.item.type - 1) * 20 })//目录是否展开图标Image(this.isOpen ? $r('app.media.ic_select_live') : $r('app.media.ic_unselect_live')).width('20vp').height('20vp').objectFit(ImageFit.Contain)}.height(50).width('100%').onClick(() => {if (this.item.children != undefined) {//打开子目录 更新目录打开状态this.item.isExpand = !this.item.isExpand;this.isOpen = this.item.isExpand;} else {//记录选中目录Idthis.selectId = this.item.id;}})List() {ForEach(this.item.children, (children: DirectoryItem,index: number) => {ListItem() {if (children.children != undefined && children.children.length > 0) {//存在子目录,去循环生成层级目录DirectoryComponent({item: this.item.children[index]})} else {//没有子目录了,直接生成单个目录项this.renderDirectoryItem(children)}}}, (item: DirectoryItem) => item.name)}.visibility(this.isOpen ? Visibility.Visible : Visibility.None).width('100%').divider({ strokeWidth: 1, color: $r('app.color.background_shallow_grey') })}}@Builderprivate renderDirectoryItem(item: DirectoryItem) {Row() {Text(item.name).fontWeight(FontWeight.Bold).fontSize(15).fontColor($r('app.color.font_color_dark')).layoutWeight(1).margin({ left: (item.type - 1) * 20 + 11})}.width('100%').height('50vp').onClick(() => {//记录点击选中目录Idthis.selectId = item.id;})}
}

5、预览运行查看index.ets文件即可查看效果

这篇关于鸿蒙HarmonyOS之使用ArkTs语言实现层级树状目录选择UI的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1094493

相关文章

C++中assign函数的使用

《C++中assign函数的使用》在C++标准模板库中,std::list等容器都提供了assign成员函数,它比操作符更灵活,支持多种初始化方式,下面就来介绍一下assign的用法,具有一定的参考价... 目录​1.assign的基本功能​​语法​2. 具体用法示例​​​(1) 填充n个相同值​​(2)

Spring StateMachine实现状态机使用示例详解

《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命

Spring Boot 结合 WxJava 实现文章上传微信公众号草稿箱与群发

《SpringBoot结合WxJava实现文章上传微信公众号草稿箱与群发》本文将详细介绍如何使用SpringBoot框架结合WxJava开发工具包,实现文章上传到微信公众号草稿箱以及群发功能,... 目录一、项目环境准备1.1 开发环境1.2 微信公众号准备二、Spring Boot 项目搭建2.1 创建

IntelliJ IDEA2025创建SpringBoot项目的实现步骤

《IntelliJIDEA2025创建SpringBoot项目的实现步骤》本文主要介绍了IntelliJIDEA2025创建SpringBoot项目的实现步骤,文中通过示例代码介绍的非常详细,对大家... 目录一、创建 Spring Boot 项目1. 新建项目2. 基础配置3. 选择依赖4. 生成项目5.

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

深入理解Go语言中二维切片的使用

《深入理解Go语言中二维切片的使用》本文深入讲解了Go语言中二维切片的概念与应用,用于表示矩阵、表格等二维数据结构,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧... 目录引言二维切片的基本概念定义创建二维切片二维切片的操作访问元素修改元素遍历二维切片二维切片的动态调整追加行动态

Linux下删除乱码文件和目录的实现方式

《Linux下删除乱码文件和目录的实现方式》:本文主要介绍Linux下删除乱码文件和目录的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux下删除乱码文件和目录方法1方法2总结Linux下删除乱码文件和目录方法1使用ls -i命令找到文件或目录

prometheus如何使用pushgateway监控网路丢包

《prometheus如何使用pushgateway监控网路丢包》:本文主要介绍prometheus如何使用pushgateway监控网路丢包问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录监控网路丢包脚本数据图表总结监控网路丢包脚本[root@gtcq-gt-monitor-prome

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并

mybatis执行insert返回id实现详解

《mybatis执行insert返回id实现详解》MyBatis插入操作默认返回受影响行数,需通过useGeneratedKeys+keyProperty或selectKey获取主键ID,确保主键为自... 目录 两种方式获取自增 ID:1. ​​useGeneratedKeys+keyProperty(推