HarmonyOS 用List组件实现组合列表项

界面分析:

由于整体UX设计图符合从上至下的布局,所以可以采用Column组件作为外层容器

简介:

  1. 最上方的标题可以使用Text组件进行加载渲染
  2. 中间的Banner图和简介分别可以使用Image组件和Text组件进行加载
  3. 最后一部分可以看作是一个列表,它由若干个列表项ListItem构成

实现标题、Banner、简介:

  1. 标题栏可以采用Text组件进行加载渲染

    1. Column() {
    2. Text('标题')
    3. .fontFamily('HarmonyHeiTi-Bold')
    4. .fontSize(24)
    5. .fontColor(Color.Black)
    6. .textAlign(TextAlign.Start)
    7. .lineHeight(33)
    8. .fontWeight(700)
    9. .width('100%')
    10. }
    11. .padding({ top: 12, right: 16, bottom: 12, left: 16})
    12. .backgroundColor('#F1F3F5')
  2. 放入图片资源:./src/main/resources/base/media目录中

  3. 使用Image组件加载放入的Banner图片,设置其宽度百分百,并设置圆角。同时注意到banner图与上方的title间有19单位的空隙,与下方的组件有8单位的空隙

    1. Image($r('app.media.yourpicture'))
    2. .width('100%')
    3. .borderRadius(16)
    4. .margin({ top: 19, bottom: 8 })
  4. 简介的部分也可以采用Text组件来进行数据的渲染

    1. Text('简介')
    2. .fontFamily('HarmonyHeiTi')
    3. .fontSize(14)
    4. .fontColor('rgba(0,0,0,0.60)')
    5. .fontWeight(400)
    6. .textAlign(TextAlign.Start)
  5. 实现List的列表项

    // @Component功能更加多样,有自己的生命周期,还能支持预览效果
    // 而@Builder更加轻量,能满足基础的组件封装,性能更好,但是不支持预览

    1. @Component

    2. export struct Item {

    3. build() {

    4. Row(){

    5. }

    6. .width('100%')

    7. .height(48)

    8. .borderRadius(16)

    9. .alignItems(VerticalAlign.Center)

    10. .padding({ left: 12, right: 12 })

    11. .backgroundColor('#F1F3F5')

    12. }

    13. }

  6. 导出数据接口

    1. export struct Item {
    2. @State navBarItem: NavBarItemType = {
    3. order: '01',
    4. title: '标题'
    5. };
    6. ...
    7. }
  7. 实现序号

    1. @Component

    2. export struct Item {

    3. ...

    4. build() {

    5. Row() {

    6. Text(this.navBarItem.order)

    7. .margin({ right: 6 })

    8. .fontFamily('HarmonyHeiTi-Bold')

    9. .fontSize(21)

    10. .fontColor('#182431')

    11. .textAlign(TextAlign.Start)

    12. .lineHeight(22)

    13. .fontWeight(700)

    14. }

    15. ...

    16. }

    17. }

  8. 实现标题

    1. @Component

    2. export struct Item {

    3. ...

    4. build() {

    5. Row() {

    6. Text(this.navBarItem.order)

    7. ...

    8. Text(this.navBarItem.title)

    9. .fontFamily('HarmonyHeiTi-Medium')

    10. .fontSize(16)

    11. .fontColor('#182431')

    12. .textAlign(TextAlign.Start)

    13. .lineHeight(22)

    14. .fontWeight(500)

    15. }

    16. }

    17. }

  9. 将指向下一页的图标放置于 ./src/main/resources/base/media目录下,并填充

    1. @Component

    2. export struct Item {

    3. ...

    4. build() {

    5. Row() {

    6. Text(this.navBarItem.order)

    7. ...

    8. Text(this.navBarItem.title)

    9. ...

    10. Blank()

    11. Image($r('app.media.ic_arrow'))

    12. .width(12)

    13. .height(24)

    14. }

    15. ...

    16. }

    17. }

组合列表项:

  1. 定义数据集,在分栏Tab的首页进行定义

    1. @State navBarList: NavBarItemType[] = [
    2. { order: '01', title: '准备与学习' },
    3. { order: '02', title: '构建应用' },
    4. { order: '03', title: '应用测试' },
    5. { order: '04', title: '上架' },
    6. { order: '05', title: '运营增长' },
    7. { order: '06', title: '商业变现' },
    8. { order: '07', title: '更多' }
    9. ];
  2. 循环渲染

    1. List({ space: 12 }) {
    2. ForEach(this.navBarList, (item: NavBarItemType, index: number) => {
    3. ListItem() {
    4. NavBarItem({ navBarItem: item })
    5. }
    6. .width('100%')
    7. }, (item: NavBarItemType): string => item.title)
    8. }
    9. .width('100%')
    10. .margin({ top: 24 })
  3. 在build()函数中,加入Scroll组件。可以解决当内容条数不足时,Scroll组件滚动时会出现空白区域的错误效果。edgeEffect用于设置边缘滑动效果,设置为EdgeEffect.Spring表示设置为弹性物理动效

相关推荐
贾伟康15 分钟前
HarmonyOS API 26 嵌套 Navigation:getParent、父子栈边界与返回分发
harmonyos·arkts·navigation·嵌套路由·navpathstack
行者-全栈开发1 小时前
【鸿蒙心迹】多线程并发实战——TaskPool 与 Worker 选型与踩坑(HarmonyOS 7.x)
华为·harmonyos·多线程并发·worker·taskpool·线程通信·harmonyos 并发
打工仔折腾 AI2 小时前
Changedetection.io 网页变动监控:Docker 部署、通知配置与公网暴露实践
运维·人工智能·后端·python·docker·容器·性能优化
OH_TPC3 小时前
@ohos/aki 源码解读(一):一行绑定背后,宏、模板与索引分发
harmonyos
贾伟康4 小时前
【解压时刻|13】HarmonyOS ArkTS 启动链路审计:从 EntryAbility、AppStore 到 Navigation 首屏
harmonyos·arkts·appstorage·应用启动·entryability
贾伟康5 小时前
【古诗词闯关|12】HarmonyOS ArkTS 单页组件拆分实战:降低题库、闯关与结果弹层耦合
harmonyos·arkts·状态管理·arkui·组件化
nice_lcj5206 小时前
LRU 缓存入门:它是什么、怎么用、面试怎么手写
java·数据结构·缓存·面试·lru
꧁细听勿语情꧂6 小时前
直接选择排序,堆排序,冒泡排序,递归快速排序、hoare版本、挖坑法、lomuto前后指针,非递归快速排序
c语言·开发语言·数据结构·算法
威哥爱编程6 小时前
模拟面试智能体开发实战 07:答完一题就点评一题,结构化输出给分建议,让每道题都变成复盘
python·agent·harmonyos
威哥爱编程7 小时前
模拟面试智能体开发实战 08:总评报告先过三道闸,防泄题防夸大防错建议,才敢发到候选人手上
python·agent·harmonyos