开天辟地 HarmonyOS(鸿蒙) - 组件(列表类): List(列表基础)

源码 https://github.com/webabcd/HarmonyDemo
作者 webabcd

开天辟地 HarmonyOS(鸿蒙) - 组件(列表类): List(列表基础)

示例如下:

pages\component\list\ListDemo.ets

/** List - 列表基础** 注:List 是一个可滚动组件,相关特性请参见 /component/layout/ScrollDemo.ets 中的说明*/import { TitleBar } from '../../TitleBar';
import { ComposeListItem, IconType, LengthMetrics, promptAction } from '@kit.ArkUI';@Entry
@Component
struct ListDemo {build() {Column() {TitleBar()Tabs() {TabContent() { MySample1() }.tabBar('基础1').align(Alignment.Top)TabContent() { MySample2() }.tabBar('基础2').align(Alignment.Top)TabContent() { MySample3() }.tabBar('ComposeListItem').align(Alignment.Top)TabContent() { MySample4() }.tabBar('多行多列').align(Alignment.Top)TabContent() { MySample5() }.tabBar('ListScroller 相关').align(Alignment.Top)}.scrollable(true).barMode(BarMode.Scrollable).layoutWeight(1)}}
}@Component
struct MySample1 {@State message: string = ""private array: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]build() {Stack({ alignContent: Alignment.TopStart }) {/** List - 列表*   space - 主轴方向上每个 item 之间的间隔距离*   initialIndex - 初始时,定位到指定索引位置的 item*   listDirection() - item 们的排列方向(Axis.Vertical 或 Axis.Horizontal)*   chainAnimation() - 拖动时是否启用链式联动效果*     手指拖动过程中,手指拖动的 ListItem 会驱动相邻的 ListItem 做弹簧联动效果(前提是必须是单列模式,且边缘效果为 EdgeEffect.Spring 时)*   onScrollIndex() - item 滚入或滚出可视区时,或者可视区中间显示的 item 发生变化时的回调*     start - 可视区内第一个 item 的索引位置*     end - 可视区内最后一个 item 的索引位置*     center - 可视区内中间 item 的索引位置** ListItem - list 内的每个 item*/List({space: 20,initialIndex: 3,}) {ForEach(this.array, (item: number) => {ListItem() {Text(item.toString()).width('100%').height(100).fontSize(48).textAlign(TextAlign.Center).backgroundColor(Color.Orange).borderRadius(20)}.margin({ left: 20, right: 20 })})}.listDirection(Axis.Vertical).chainAnimation(true).edgeEffect(EdgeEffect.Spring).onScrollIndex((start: number, end: number, center: number) => {this.message = `onScrollIndex start:${start}, end:${end}, center:${center}`}).width('100%').height('100%')Text(this.message).fontSize(16).fontColor(Color.White).backgroundColor(Color.Blue)}.width('100%').height('100%').backgroundColor(Color.Yellow)}
}@Component
struct MySample2 {private array: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]// 实例化 ChildrenMainSize,并指定主轴方向上的默认大小childrenMainSize: ChildrenMainSize = new ChildrenMainSize(100)aboutToAppear(){// 位置 3, 4, 5, 6 的 item 的大小全部指定为 200this.childrenMainSize.splice(3, 4, [200, 200, 200, 200])// 位置 9 的 item 的大小指定为 200this.childrenMainSize.update(9, 200)}build() {Column() {/** List - 列表*   divider() - 每个 item 之间的分隔线*     strokeWidth - 分隔线的画笔宽度*     color - 颜色*     startMargin - 左侧外边距*     endMargin - 右侧外边距*     注:当 chainAnimation(true) 时不会显示分隔线*   contentStartOffset() - 顶部 item 与顶部边缘之间的间距*   contentEndOffset() - 底部 item 与底部边缘之间的间距*   childrenMainSize() - 指定每个 item 的主轴方向上的不同的大小*     value - 主轴方向上的默认大小*     splice(), update() - 更新不同位置的 item 的主轴方向上的大小*     注:当每个 item 在轴方向上的大小不一致时,需要额外通过此方式指定他们的大小,这样在调用 scrollToIndex(), scrollTo(), currentOffset() 等时才不会有问题*/List({ space: 20 }) {ForEach(this.array, (item: number) => {ListItem() {Text(item.toString()).width('100%').fontSize(48).textAlign(TextAlign.Center).backgroundColor(Color.Orange).borderRadius(20).height((item >= 3 && item < 7) || (item == 9) ? 200 : 100)}.margin({ left: 20, right: 20 })})}.divider({strokeWidth: 2,color: Color.Red,startMargin: 20,endMargin: 20}).contentStartOffset(50).contentEndOffset(50).childrenMainSize(this.childrenMainSize).width('100%').height('100%')}.width('100%').height('100%').backgroundColor(Color.Yellow)}
}@Component
struct MySample3 {build() {Column() {List({ space: 10 }) {/** ComposeListItem - 增加了一些内置功能的 ListItem*   contentItem - 中间及左侧的内容*   operateItem - 右侧的内容*/ComposeListItem({contentItem: ({iconStyle: IconType.HEAD_SCULPTURE, // 用于指定图标的大小icon: $r('app.media.app_icon'),primaryText: 'primaryText',secondaryText: 'secondaryText',description: 'description'}),operateItem: ({icon: {value: $r('app.media.ic_settings'),action: () => {promptAction.showToast({ message: 'icon clicked' });}},text: 'text'})})ComposeListItem({operateItem: ({arrow: { value: $r('app.media.ic_arrow_right'), action: () => { } },text: 'text'})})ComposeListItem({operateItem: ({switch: { isCheck: true, onChange: (value: boolean) => { } },})})ComposeListItem({operateItem: ({checkbox: { isCheck: false, onChange: (value: boolean) => { } },})})ComposeListItem({operateItem: ({ image: $r('app.media.ic_settings') })})}}}
}@Component
struct MySample4 {private array: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]build() {Column({ space:20 }) {/** List - 列表*   lanes() - 多行多列的配置,以下以 listDirection(Axis.Vertical) 为例*     value - 列数*       可以指定一个整数,用于指定列数*       可以指定一个 LengthConstrain 对象,用于动态计算列数*         minLength - 最小列宽*         maxLength - 最大列宽*     gutter - 列间距*   alignListItem() - 水平方向上的对齐方式(ListItemAlign 枚举),当 lanes() 指定为 LengthConstrain 对象时有效*     Start, Center, End*/List({ space: 20 }) {ForEach(this.array, (item: number) => {ListItem() {Text(item.toString()).width('100%').height(100).fontSize(24).textAlign(TextAlign.Center)}.border({ width: 2, color: Color.Green })})}.height(300).width("80%").borderWidth(2).borderColor(Color.Red).scrollBar(BarState.Off).lanes(3, 10)List({ space: 20 }) {ForEach(this.array, (item: number) => {ListItem() {Text(item.toString()).width('100%').height(100).fontSize(24).textAlign(TextAlign.Center)}.border({ width: 2, color: Color.Green })})}.height(300).width("80%").borderWidth(2).borderColor(Color.Red).scrollBar(BarState.Off).lanes({minLength: 50,maxLength: 50,}, 10).alignListItem(ListItemAlign.End)}.width('100%').height('100%').backgroundColor(Color.Yellow)}
}@Component
struct MySample5 {@State message: string = ""/** ListScroller 是一个 controller,是用于和 List 交互的,声明式编程通常都会用这种方式* ListScroller 继承自 Scroller(请参见 /component/layout/ScrollDemo.ets 中的说明)*/listScroller: ListScroller = new ListScroller()private array: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]build() {Stack({ alignContent: Alignment.TopStart }) {/** List - 列表*   scroller - 指定 List 需要绑定的 ListScroller*/List({ scroller: this.listScroller }) {ForEach(this.array, (item: number) => {ListItem() {Text(item.toString()).width('100%').height(200).fontSize(48).textAlign(TextAlign.Center).backgroundColor(Color.Orange).borderRadius(20)}.margin(10)})}.width('100%').height('100%')Column({space:10}) {/** ListScroller - 用于和绑定的 List 之间的交互(继承自 Scroller)*   scrollToIndex() - 滚动到指定索引位置的 item(仅 Grid, List, WaterFlow 有效)*     value - 需要滚动到的 item 的索引位置*     smooth - 是否平滑滚动*     align - 滚动到的 item 与可视区的对齐方式(ScrollAlign 枚举)*       START - item 与可视区的顶端对齐*       CENTER - item 与可视区的中间对齐*       END - item 与可视区的底端对齐*     options - 选项*       extraOffset - 在当前位置上的偏移距离*   getItemRect() - 获取指定索引位置的 item 的位置和大小(仅 Grid, List, WaterFlow 有效)*     返回值包括 x, y, width, height*     注:指定的 item 必须要显示在可视区才能获取到正确的值,否则返回的都是 0*/Button('scrollToIndex').onClick(() => {this.listScroller.scrollToIndex(5,true,ScrollAlign.START,{extraOffset: LengthMetrics.vp(0)})})Button('getItemRect(3)').onClick(() => {const itemRect = this.listScroller.getItemRect(3)this.message = `getItemRect(3) x:${itemRect.x}, y:${itemRect.y}, width:${itemRect.width}, height:${itemRect.height}`})Text(this.message).fontSize(16).fontColor(Color.White).backgroundColor(Color.Blue)}.hitTestBehavior(HitTestMode.None).alignItems(HorizontalAlign.Start)}.width('100%').height('100%').backgroundColor(Color.Yellow)}
}

源码 https://github.com/webabcd/HarmonyDemo
作者 webabcd

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/879503.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

美国支付清算体系介绍

美国的支付清算体系是全球最复杂、最多样化的金融基础设施之一,支撑着庞大的国内经济和全球金融活动。本文将详细介绍美国支付清算体系的主要组成部分,包括银行间支付系统(如Fedwire和CHIPS)和零售支付系统(如ACH、Zelle、RTP),并探讨它们的特点和应用场景。通过对比中国…

学习 -人工智能 - AI Agent的理解

浅谈AI Agent的理解 Agents是什么? 大语言模型可以接收输入、可以分析&推理、可以输出文字、代码、媒体。然而其无法像人类一样,拥有规划思考能力、运用各种工具与物理世界互动,以及拥有人类的记忆能力。 AI Agents是基于LLM能够自助理解、自主规划决策、执行复杂任务的…

dotnet LibGit2Sharp 使用笔记

本文记录我对 LibGit2Sharp 库的使用笔记LibGit2Sharp 库开源地址: https://github.com/libgit2/libgit2sharp 本文使用的版本是: 0.31.0 按照 dotnet 的惯例,使用之前先用 NuGet 安装,安装之后的 csproj 文件代码大概如下 <Project Sdk="Microsoft.NET.Sdk"&…

dotnet 9 已知问题 默认开启 CET 导致进程崩溃

本文记录 dotnet 9 的一个已知且当前已修问题。默认开启 CET 导致一些模块执行时触发崩溃官方文档: Breaking change: CET supported by default - .NET Microsoft Learn 表现: 调用 OpenFileDialog 的 ShowDialog 将会异常崩溃,崩溃异常是 FAST_FAIL_SET_CONTEXT_DENIED 或…

读算法简史:从美索不达米亚到人工智能时代08组合优化

读算法简史:从美索不达米亚到人工智能时代08组合优化1. 组合优化 1.1. 蛮力搜索算法会尝试所有可能的组合并从中选择最好的那个 1.2. 旅行商问题是众多组合优化(combinatorial optimization)问题中的一个,它要求许多固定元素以可能的最佳方式进行组合1.2.1. 固定元素可以有无…

[Vie] 依赖预构建

使用Vite模板vue-ts. https://github.com/vitejs/vite/tree/main/packages/create-vite 一个组件在没加入Lodash之前:当加入lodash之后,会自动把它加入到.vite/deps中去: 假如不用依赖构建 vite.config.tsreturn {plugins: [vue()],// ...optimizeDeps: {exclude: [lodash-…

从源码分析arm64中断与GIC

本文以树莓派4b(armv8)来实现,4b支持两种传统的中断控制器 gic-400 但是使用的qemu和实际的板子都是默认支持gic-400的,所以主要是借助gic-400实现中断的功能异常处理 相关寄存器PSTATE 就是cpu状态DAIF 调试异常 SError(系统异常) IRQ(中断) FIQ(快速中断)esr_elx 用来保存返…

《gm/ID设计法基本介绍》翻译

最近流片很累很焦虑,放松心情找篇讲\(g_m/I_D\)设计法的文档翻译一下: 《A Basic Introduction to the gm/ID-Based Design Methodology》 1. 摘要 该文章向读者介绍了基于\(g_m/I_D\)的设计方法学,用于帮助CMOS模拟电路设计者将晶体管物理参数与小信号模型联系起来,文章的…

个人英语学习笔记基于B站英语的平行世界语法课程

导读 语言学习没有捷径,只要听说读写这四大行长期日复一日的练习就行了,兴趣是最重要的,兴趣就是高效学习的基础和长期坚持下去的动力。 0基础开始痛苦学习大半年英语,没兴趣的结果就是词汇量是上去了,但是英语的听说读写水平还不如学了一年的日语。😅 该笔记基于此课程…

PostgreSQL:数据库迁移与版本控制

title: PostgreSQL:数据库迁移与版本控制 date: 2025/2/6 updated: 2025/2/6 author: cmdragon excerpt: 在现代软件开发中,数据库作为应用程序的核心组件之一,数据的结构和内容必须能够随着业务需求的变化而调整。因此,数据库迁移和版本控制成为了确保数据一致性、完整性…

Servlet基础

什么是Servlet、Servlet的架构、Servlet任务、Servlet的基本使用、Servlet的生命周期、Servlet API中主要接口及实现类、Servlet的部署(注册与映射)、缺省Servlet与启动时加载配置、ServletConfig与ServletContext、request和response什么是Servlet基础 Java Servlet 是运行在…

GNURadio模块学习——Source and Sink类

介绍GNU Radio中常见的 Source 与 Sink 模块,包括流程图端口、音频输入输出、虚拟连接、文件读写、ZMQ跨流程图通信,以及随机信号源、固定信号源、噪声源等常见信号源和时域、频域、星座图等信号展示工具。Source and Sink Pad(流程图端口) 当该流程图是hierarchical block…