找回密码
 立即注册
首页 业界区 业界 鸿蒙应用开发从入门到实战(十七):ArkUI组件List&列表 ...

鸿蒙应用开发从入门到实战(十七):ArkUI组件List&列表布局

拍棹 昨天 14:14
大家好,我是潘Sir,持续分享IT技术,帮你少走弯路。《鸿蒙应用开发从入门到项目实战》系列文章持续更新中,陆续更新AI+编程、企业级项目实战等原创内容、欢迎关注!
ArkUI提供了丰富的系统组件,用于制作鸿蒙原生应用APP的UI,本文通过简单案例演示如何使用List组件实现列表布局。
一、List布局优化商品列表

上一小节里的商品列表,随着数据增多,当超出界面后,无法滚动查看。List列表布局就可以解决这个问题。
List列表是一种复杂容器,具备下列特点:

  • 列表项ListItem数量过多超出屏幕后,会自动提供滚动功能
  • 列表项ListItem既可以纵向排列,也可以横向排列
再pages/layout下新建list目录,新建ProductList.ets文件,将上一小节里的代码文件ProductListPage.ets文件里的内容拷贝过来进行修改。
将ForEach部分的内容放到List组件里即可
  1. class Item1 { // 复制过来后,即使再不同的文件中,也会提示同名
  2.   name: string //小写
  3.   image: ResourceStr
  4.   price: number
  5.   discount: number
  6.   constructor(name: string, image: ResourceStr, price: number, discount: number = 0) {
  7.     this.name = name
  8.     this.image = image
  9.     this.price = price
  10.     this.discount = discount
  11.   }
  12. }
  13. @Entry
  14. @Component
  15. struct ProductList {
  16.   // 商品数据
  17.   private items: Array<Item1> = [
  18.     new Item1('华为Mate60', $r('app.media.mate60'), 6999, 500),
  19.     new Item1('MateBookProX', $r('app.media.mate60'), 13999),
  20.     new Item1('WatchGT4', $r('app.media.mate60'), 1438),
  21.     new Item1('FreeBuds Pro3', $r('app.media.mate60'), 1499),
  22.     new Item1('FreeBuds Pro3', $r('app.media.mate60'), 199),
  23.     new Item1('Mate X5', $r('app.media.mate60'), 12999)
  24.   ]
  25.   build() {
  26.     Column({ space: 8 }) {
  27.       // 标题
  28.       Row() {
  29.         Text('商品列表')
  30.           .fontSize(30)
  31.           .fontWeight(FontWeight.Bold)
  32.       }
  33.       .width('100%')
  34.       // .height(30) //控制高度
  35.       .margin({ bottom: 20 })
  36.       // 商品列表
  37.       List({ space: 8 }) {
  38.         ForEach(
  39.           this.items,
  40.           (item: Item1) => {
  41.             ListItem() {  //ListItem子元素必须用根元素包裹
  42.               Row({ space: 10 }) {
  43.                 Image(item.image)
  44.                   .width(100)
  45.                 Column({ space: 4 }) {
  46.                   if (item.discount) {
  47.                     Text(item.name)
  48.                       .fontSize(20)
  49.                       .fontWeight(FontWeight.Bold)
  50.                     Text('原价:¥' + item.price)
  51.                       .fontColor('#CCC')
  52.                       .fontSize(14)
  53.                       .decoration({ type: TextDecorationType.LineThrough })
  54.                     Text('折扣价:¥' + (item.price - item.discount))
  55.                       .fontColor('#F36')
  56.                       .fontSize(18)
  57.                     Text('补贴:¥' + item.discount)
  58.                       .fontColor('#F36')
  59.                       .fontSize(18)
  60.                   } else {
  61.                     Text(item.name)
  62.                       .fontSize(20)
  63.                       .fontWeight(FontWeight.Bold)
  64.                     Text('¥' + item.price)
  65.                       .fontColor('#F36')
  66.                       .fontSize(18)
  67.                   }
  68.                 }
  69.                 .height('100%')
  70.                 .alignItems(HorizontalAlign.Start)
  71.               }
  72.               .width('100%')
  73.               .backgroundColor('#FFF')
  74.               .borderRadius(20)
  75.               .height(120)
  76.               .padding(10)
  77.             }
  78.           }
  79.         )
  80.       }
  81.       .width('100%')
  82.       // .layoutWeight(1)
  83.     }
  84.     .width('100%')
  85.     .height('100%')
  86.     .backgroundColor('#EFEFEF')
  87.     .padding(20)
  88.   }
  89. }
复制代码
这样,就可以通过拖动呈现超过屏幕区的内容。
二、列表布局详解

2.1 概述

List是一个功能强大的容器组件,使用List可以轻松高效地显示结构化、可滚动的列表信息,例如通讯录、新闻列表等等。
1.webp

List容器的子组件为ListItem或者ListItemGroup,其中,ListItem表示单个列表项,ListItemGroup用于列表数据的分组展示,其子组件也是ListItem,具体用法如下
  1. List() {
  2.   // 列表项
  3.   ListItem() {......}
  4.   ListItem() {......}
  5.   ListItem() {......}
  6.   ListItem() {......}
  7.   ListItem() {......}
  8.   ListItem() {......}
  9.   ListItem() {......}
  10.   ListItem() {......}
  11.   ListItem() {......}
  12.   ListItem() {......}
  13.   ListItem() {......}
  14.   ListItem() {......}
  15. }
复制代码
效果
2.webp
  1. List() {
  2.   // 列表组
  3.   ListItemGroup(){
  4.     //列表项
  5.     ListItem(){......}
  6.     ListItem(){......}
  7.   }
  8.   ListItemGroup(){
  9.     ListItem(){......}
  10.     ListItem(){......}
  11.   }
  12.   
  13.   ListItemGroup(){
  14.     ListItem(){......}
  15.     ListItem(){......}
  16.   }
  17. }
复制代码
效果
3.webp

2.2 参数

List 组件的参数定义如下,下面逐一介绍每个参数
  1. List(value?:{space?: number | string, scroller?: Scroller})
复制代码
2.2.1 列表项间距

space参数用于设置列表项的间距,如下图所示
4.webp

2.2.2 列表滚动控制器

scroller参数用于绑定列表滚动控制器(Scroller),Scroller可以控制列表的滚动,例如令列表返回顶部
5.gif

示例:
拷贝icon-top.png到resources/base/media目录
pages /layout/list新建ScrollerPage.ets
  1. @Entry
  2. @Component
  3. struct ScrollerPage {
  4.   data: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
  5.   scroller: Scroller = new Scroller();
  6.   build() {
  7.     Stack({ alignContent: Alignment.BottomEnd }) {
  8.       List({ space: 20, scroller: this.scroller }) {
  9.         ForEach(this.data, (item) => {
  10.           ListItem() {
  11.             Text(item.toString())
  12.               .itemTextStyle()
  13.           }
  14.         })
  15.       }.listStyle()
  16.       .height('100%')
  17.       .width('100%')
  18.       Button({ type: ButtonType.Circle }) {
  19.         Image($r('app.media.icon_top'))
  20.           .width(40)
  21.           .height(40)
  22.       }
  23.       .width(60)
  24.       .height(60)
  25.       .backgroundColor(Color.Orange)
  26.       .offset({ x: -20, y: -100 })
  27.       .onClick(() => {
  28.         this.scroller.scrollToIndex(0)
  29.       })
  30.     }
  31.   }
  32. }
  33. @Extend(Text) function itemTextStyle() {
  34.   .height(80)
  35.   .width('100%')
  36.   .textAlign(TextAlign.Center)
  37.   .fontColor(Color.White)
  38.   .fontSize(40)
  39.   .fontWeight(FontWeight.Bold)
  40.   .backgroundColor('#008a00')
  41.   .borderRadius(10)
  42. }
  43. @Extend(List) function listStyle() {
  44.   .backgroundColor(Color.White)
  45.   .padding(20)
  46. }
复制代码
2.3 常用属性

2.3.1 主轴方向

使用listDirection()方法可以设置列表的主轴方向(即列表的排列和滚动方向),其参数类型为枚举类型Axis,可选的枚举值如下
6.png

2.3.2 交叉轴对齐方式

使用alignListItem()方法可以设置子组件在交叉轴方向的对齐方式,其参数类型为枚举类型ListItemAlign,可选的枚举值有
7.png

示例代码
pages /layout/list新建AlignPage.ets
  1. @Entry
  2. @Component
  3. struct AlignPage {
  4.   data: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
  5.   build() {
  6.     List({ space: 20 }) {
  7.       ForEach(this.data, (item) => {
  8.         ListItem() {
  9.           Text(item.toString())
  10.             .height(80)
  11.             .width(320)
  12.             .itemTextStyle1()
  13.         }
  14.       })
  15.     }.listStyle1()
  16.     .height('100%')
  17.     .width('100%')
  18.     .alignListItem(ListItemAlign.Center)
  19.   }
  20. }
  21. @Extend(Text) function itemTextStyle1() {  //同一个命名空间下不能重复,否则会报错
  22.   .textAlign(TextAlign.Center)
  23.   .fontColor(Color.White)
  24.   .fontSize(40)
  25.   .fontWeight(FontWeight.Bold)
  26.   .backgroundColor('#008a00')
  27.   .borderRadius(10)
  28. }
  29. @Extend(List) function listStyle1() {
  30.   .backgroundColor(Color.White)
  31.   .padding({ top: 20, bottom: 20 })
  32. }
复制代码
2.3.3 元素分割线

使用divider()属性可设置列表元素分割线样式,该方法的参数定义如下
  1. divider(value: {strokeWidth: Length, color?: ResourceColor, startMargin?: Length, endMargin?: Length})
复制代码
各参数的含义如下
参数含义strokeWidth分割线线宽color分割线颜色startMargin分割线起始端到列表侧边距离(如下图所示)endMargin分割线末端到列表侧边距离(如下图所示)
8.webp

示例代码
pages /layout/list新建DividerPage.ets
  1. @Entry
  2. @Component
  3. struct DividerPage {
  4.   data: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
  5.   build() {
  6.     List({ space: 20 }) {
  7.       ForEach(this.data, (item) => {
  8.         ListItem() {
  9.           Text(item.toString())
  10.             .height(80)
  11.             .width(320)
  12.             .itemTextStyle2()
  13.         }
  14.       })
  15.     }
  16.     .listStyle2()
  17.     .height('100%')
  18.     .width('100%')
  19.     .alignListItem(ListItemAlign.Center)
  20.     .divider({
  21.       strokeWidth: 1,
  22.       color: Color.Orange,
  23.       startMargin: 30,
  24.       endMargin: 30
  25.     })
  26.   }
  27. }
  28. @Extend(Text) function itemTextStyle2() {
  29.   .textAlign(TextAlign.Center)
  30.   .fontColor(Color.White)
  31.   .fontSize(40)
  32.   .fontWeight(FontWeight.Bold)
  33.   .backgroundColor('#008a00')
  34.   .borderRadius(10)
  35. }
  36. @Extend(List) function listStyle2() {
  37.   .backgroundColor(Color.White)
  38.   .padding({ top: 20, bottom: 20 })
  39. }
复制代码
2.3.4 滚动条样式

使用scrollBar()方法可以设置滚动条状态,该方法的参数类型为枚举类型BarState,可选的枚举值如下
名称描述BarState.Off不显示BarState.On常驻显示BarState.Auto按需显示(触摸时显示,2s后消失)示例代码
pages /layout/list新建ScrollBarPage.ets
  1. @Entry
  2. @Component
  3. struct ScrollBarPage {
  4.   data: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
  5.   build() {
  6.     List({ space: 20 }) {
  7.       ForEach(this.data, (item) => {
  8.         ListItem() {
  9.           Text(item.toString())
  10.             .height(80)
  11.             .width(320)
  12.             .itemTextStyle3()
  13.         }
  14.       })
  15.     }.listStyle3()
  16.     .height('100%')
  17.     .width('100%')
  18.     .alignListItem(ListItemAlign.Center)
  19.     .scrollBar(BarState.Auto)
  20.   }
  21. }
  22. @Extend(Text) function itemTextStyle3() {
  23.   .textAlign(TextAlign.Center)
  24.   .fontColor(Color.White)
  25.   .fontSize(40)
  26.   .fontWeight(FontWeight.Bold)
  27.   .backgroundColor('#008a00')
  28.   .borderRadius(10)
  29. }
  30. @Extend(List) function listStyle3() {
  31.   .backgroundColor(Color.White)
  32.   .padding({ top: 20, bottom: 20 })
  33. }
复制代码
《鸿蒙应用开发从入门到项目实战》系列文章持续更新中,陆续更新AI+编程、企业级项目实战等原创内容,防止迷路,欢迎关注!

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

您需要登录后才可以回帖 登录 | 立即注册