鸿蒙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使用Curator进行ZooKeeper操作的详细教程

《Java使用Curator进行ZooKeeper操作的详细教程》ApacheCurator是一个基于ZooKeeper的Java客户端库,它极大地简化了使用ZooKeeper的开发工作,在分布式系统... 目录1、简述2、核心功能2.1 CuratorFramework2.2 Recipes3、示例实践3

Springboot处理跨域的实现方式(附Demo)

《Springboot处理跨域的实现方式(附Demo)》:本文主要介绍Springboot处理跨域的实现方式(附Demo),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不... 目录Springboot处理跨域的方式1. 基本知识2. @CrossOrigin3. 全局跨域设置4.

springboot security使用jwt认证方式

《springbootsecurity使用jwt认证方式》:本文主要介绍springbootsecurity使用jwt认证方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录前言代码示例依赖定义mapper定义用户信息的实体beansecurity相关的类提供登录接口测试提供一

go中空接口的具体使用

《go中空接口的具体使用》空接口是一种特殊的接口类型,它不包含任何方法,本文主要介绍了go中空接口的具体使用,具有一定的参考价值,感兴趣的可以了解一下... 目录接口-空接口1. 什么是空接口?2. 如何使用空接口?第一,第二,第三,3. 空接口几个要注意的坑坑1:坑2:坑3:接口-空接口1. 什么是空接

Spring Boot 3.4.3 基于 Spring WebFlux 实现 SSE 功能(代码示例)

《SpringBoot3.4.3基于SpringWebFlux实现SSE功能(代码示例)》SpringBoot3.4.3结合SpringWebFlux实现SSE功能,为实时数据推送提供... 目录1. SSE 简介1.1 什么是 SSE?1.2 SSE 的优点1.3 适用场景2. Spring WebFlu

基于SpringBoot实现文件秒传功能

《基于SpringBoot实现文件秒传功能》在开发Web应用时,文件上传是一个常见需求,然而,当用户需要上传大文件或相同文件多次时,会造成带宽浪费和服务器存储冗余,此时可以使用文件秒传技术通过识别重复... 目录前言文件秒传原理代码实现1. 创建项目基础结构2. 创建上传存储代码3. 创建Result类4.

SpringBoot日志配置SLF4J和Logback的方法实现

《SpringBoot日志配置SLF4J和Logback的方法实现》日志记录是不可或缺的一部分,本文主要介绍了SpringBoot日志配置SLF4J和Logback的方法实现,文中通过示例代码介绍的非... 目录一、前言二、案例一:初识日志三、案例二:使用Lombok输出日志四、案例三:配置Logback一

springboot security快速使用示例详解

《springbootsecurity快速使用示例详解》:本文主要介绍springbootsecurity快速使用示例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝... 目录创www.chinasem.cn建spring boot项目生成脚手架配置依赖接口示例代码项目结构启用s

Python如何使用__slots__实现节省内存和性能优化

《Python如何使用__slots__实现节省内存和性能优化》你有想过,一个小小的__slots__能让你的Python类内存消耗直接减半吗,没错,今天咱们要聊的就是这个让人眼前一亮的技巧,感兴趣的... 目录背景:内存吃得满满的类__slots__:你的内存管理小助手举个大概的例子:看看效果如何?1.

Python+PyQt5实现多屏幕协同播放功能

《Python+PyQt5实现多屏幕协同播放功能》在现代会议展示、数字广告、展览展示等场景中,多屏幕协同播放已成为刚需,下面我们就来看看如何利用Python和PyQt5开发一套功能强大的跨屏播控系统吧... 目录一、项目概述:突破传统播放限制二、核心技术解析2.1 多屏管理机制2.2 播放引擎设计2.3 专