鸿蒙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

相关文章

使用Java将各种数据写入Excel表格的操作示例

《使用Java将各种数据写入Excel表格的操作示例》在数据处理与管理领域,Excel凭借其强大的功能和广泛的应用,成为了数据存储与展示的重要工具,在Java开发过程中,常常需要将不同类型的数据,本文... 目录前言安装免费Java库1. 写入文本、或数值到 Excel单元格2. 写入数组到 Excel表格

redis中使用lua脚本的原理与基本使用详解

《redis中使用lua脚本的原理与基本使用详解》在Redis中使用Lua脚本可以实现原子性操作、减少网络开销以及提高执行效率,下面小编就来和大家详细介绍一下在redis中使用lua脚本的原理... 目录Redis 执行 Lua 脚本的原理基本使用方法使用EVAL命令执行 Lua 脚本使用EVALSHA命令

Python中pywin32 常用窗口操作的实现

《Python中pywin32常用窗口操作的实现》本文主要介绍了Python中pywin32常用窗口操作的实现,pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个... 目录获取窗口句柄获取最前端窗口句柄获取指定坐标处的窗口根据窗口的完整标题匹配获取句柄根据窗口的类别匹配获取句

Java 中的 @SneakyThrows 注解使用方法(简化异常处理的利与弊)

《Java中的@SneakyThrows注解使用方法(简化异常处理的利与弊)》为了简化异常处理,Lombok提供了一个强大的注解@SneakyThrows,本文将详细介绍@SneakyThro... 目录1. @SneakyThrows 简介 1.1 什么是 Lombok?2. @SneakyThrows

在 Spring Boot 中实现异常处理最佳实践

《在SpringBoot中实现异常处理最佳实践》本文介绍如何在SpringBoot中实现异常处理,涵盖核心概念、实现方法、与先前查询的集成、性能分析、常见问题和最佳实践,感兴趣的朋友一起看看吧... 目录一、Spring Boot 异常处理的背景与核心概念1.1 为什么需要异常处理?1.2 Spring B

Python位移操作和位运算的实现示例

《Python位移操作和位运算的实现示例》本文主要介绍了Python位移操作和位运算的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 位移操作1.1 左移操作 (<<)1.2 右移操作 (>>)注意事项:2. 位运算2.1

如何在 Spring Boot 中实现 FreeMarker 模板

《如何在SpringBoot中实现FreeMarker模板》FreeMarker是一种功能强大、轻量级的模板引擎,用于在Java应用中生成动态文本输出(如HTML、XML、邮件内容等),本文... 目录什么是 FreeMarker 模板?在 Spring Boot 中实现 FreeMarker 模板1. 环

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义

使用Python和Pyecharts创建交互式地图

《使用Python和Pyecharts创建交互式地图》在数据可视化领域,创建交互式地图是一种强大的方式,可以使受众能够以引人入胜且信息丰富的方式探索地理数据,下面我们看看如何使用Python和Pyec... 目录简介Pyecharts 简介创建上海地图代码说明运行结果总结简介在数据可视化领域,创建交互式地

SpringMVC 通过ajax 前后端数据交互的实现方法

《SpringMVC通过ajax前后端数据交互的实现方法》:本文主要介绍SpringMVC通过ajax前后端数据交互的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价... 在前端的开发过程中,经常在html页面通过AJAX进行前后端数据的交互,SpringMVC的controll