当前位置:   article > 正文

鸿蒙开发学习笔记——渲染控制_鸿蒙怎么做list数据渲染点击每一个listitem跳转到对应数据界面

鸿蒙怎么做list数据渲染点击每一个listitem跳转到对应数据界面

1.ForEach:循环便遍历数组,根据数组内容渲染组件

  1. ForEach(
  2. arr: Array, // 1.要遍历的数据数组
  3. (item: any, index: number) => {
  4. // 2.页面组件生成函数
  5. },
  6. KeyGenerator?: (item:any, index?: number): string => {
  7. // 3.键生成函数,为数组每一项生成一个唯一标示,组件是否重新渲染的判断标准 可以不写直接用默认的
  8. }
  9. )

实例:

已有资源:

都是我在华为官网找的图片和价格,下载到media文件中。 

要实现下面商品展示效果

先分析页面布局,红为列或单个元素,蓝为行

单个商品布局代码:

  1. Row(){
  2. Iamge()
  3. Cloumn(){
  4. Text()
  5. Text()
  6. }
  7. }

定义商品类

  1. // 定义商品类
  2. class Item{
  3. name: string
  4. image: ResourceStr
  5. price: number
  6. constructor(name: string, image: ResourceStr, price: number) {
  7. this.name = name
  8. this.image = image
  9. this.price = price
  10. }
  11. }

 实例化

  1. private items: Array<Item> = [
  2. new Item('Pocket系列',$r('app.media.Pocket'),7499),
  3. new Item('Mate系列',$r('app.media.Mate'),6499),
  4. new Item('P系列',$r('app.media.P'),6988),
  5. new Item('nova系列',$r('app.media.nova'),4699),
  6. new Item('畅想系列',$r('app.media.nav'),1499)
  7. ]

完整代码如下:

  1. // 定义商品类
  2. class Item{
  3. name: string
  4. image: ResourceStr
  5. price: number
  6. constructor(name: string, image: ResourceStr, price: number) {
  7. this.name = name
  8. this.image = image
  9. this.price = price
  10. }
  11. }
  12. @Entry
  13. @Component
  14. struct Index {
  15. private items: Array<Item> = [
  16. new Item('Pocket系列',$r('app.media.Pocket'),7499),
  17. new Item('Mate系列',$r('app.media.Mate'),6499),
  18. new Item('P系列',$r('app.media.P'),6988),
  19. new Item('nova系列',$r('app.media.nova'),4699),
  20. new Item('畅想系列',$r('app.media.nav'),1499)
  21. ]
  22. build() {
  23. Column({space: 8}){
  24. Row(){
  25. Text('商品列表')
  26. .fontSize(30)
  27. .fontWeight(FontWeight.Bold)
  28. }
  29. .width('100%')
  30. .margin({bottom: 20})
  31. // 商品渲染
  32. ForEach(
  33. this.items,
  34. // 也可以写成item => {},但因为传进来的item类型为any,所以没有提示,声明变量类型后就有提示了
  35. (item: Item) => {
  36. Row({space: 10}){
  37. Image(item.image)
  38. .width(100)
  39. Column({space: 4}){
  40. Text(item.name)
  41. .fontSize(20)
  42. .fontWeight(FontWeight.Bold)
  43. Text('¥'+item.price)
  44. .fontColor('#F36')
  45. .fontSize(18)
  46. }
  47. .height('100%')
  48. .alignItems(HorizontalAlign.Start)
  49. }
  50. .width('100%')
  51. .backgroundColor('#FFF')
  52. .borderRadius(20)
  53. .height(120)
  54. .padding(10)
  55. }
  56. )
  57. }
  58. .width('100%')
  59. .height('100%')
  60. }
  61. }

2.if else:条件控制,根据数据状态的不同,渲染不同的页面组件

  1. if(判断条件){
  2.         // 内容A
  3. }else{
  4.         // 内容B
  5. }

在上面的基础上,如果有些商品打折,则需要不同的渲染方式,用if else可实现,效果图如下:

首先修改商品类定义

  1. // 定义商品类
  2. class Item{
  3. name: string
  4. image: ResourceStr
  5. price: number
  6. discount: number
  7. // discount初始化为0
  8. constructor(name: string, image: ResourceStr, price: number, discount: number = 0) {
  9. this.name = name
  10. this.image = image
  11. this.price = price
  12. this.discount = discount
  13. }
  14. }

 实例化也修改

  1. private items: Array<Item> = [
  2. new Item('Pocket系列',$r('app.media.Pocket'),7499,500),
  3. new Item('Mate系列',$r('app.media.Mate'),6499),
  4. new Item('P系列',$r('app.media.P'),6988),
  5. new Item('nova系列',$r('app.media.nova'),4699),
  6. new Item('畅想系列',$r('app.media.nav'),1499)
  7. ]

完整代码如下:

  1. // 定义商品类
  2. class Item{
  3. name: string
  4. image: ResourceStr
  5. price: number
  6. discount: number
  7. // discount初始化为0
  8. constructor(name: string, image: ResourceStr, price: number, discount: number = 0) {
  9. this.name = name
  10. this.image = image
  11. this.price = price
  12. this.discount = discount
  13. }
  14. }
  15. @Entry
  16. @Component
  17. struct Index {
  18. private items: Array<Item> = [
  19. new Item('Pocket系列',$r('app.media.Pocket'),7499,500),
  20. new Item('Mate系列',$r('app.media.Mate'),6499),
  21. new Item('P系列',$r('app.media.P'),6988),
  22. new Item('nova系列',$r('app.media.nova'),4699),
  23. new Item('畅想系列',$r('app.media.nav'),1499)
  24. ]
  25. build() {
  26. Column({space: 8}){
  27. Row(){
  28. Text('商品列表')
  29. .fontSize(30)
  30. .fontWeight(FontWeight.Bold)
  31. }
  32. .width('100%')
  33. .margin({bottom: 20})
  34. // 商品渲染
  35. ForEach(
  36. this.items,
  37. // 也可以写成item => {},但因为传进来的item类型为any,所以没有提示,声明变量类型后就有提示了
  38. (item: Item) => {
  39. Row({space: 10}){
  40. Image(item.image)
  41. .width(100)
  42. Column({space: 4}){
  43. // 如果有折扣 渲染只有这里有变化
  44. if (item.discount) {
  45. Text(item.name)
  46. .fontSize(20)
  47. .fontWeight(FontWeight.Bold)
  48. Text('原价:¥'+item.price)
  49. .fontColor('#CCC')
  50. .fontSize(14)
  51. .decoration({type: TextDecorationType.LineThrough})
  52. Text('折扣价:¥'+(item.price - item.discount))
  53. .fontColor('#F36')
  54. .fontSize(18)
  55. Text('补贴:¥'+item.discount)
  56. .fontColor('#F36')
  57. .fontSize(18)
  58. }else{
  59. Text(item.name)
  60. .fontSize(20)
  61. .fontWeight(FontWeight.Bold)
  62. Text('¥'+item.price)
  63. .fontColor('#F36')
  64. .fontSize(18)
  65. }
  66. }
  67. .height('100%')
  68. .alignItems(HorizontalAlign.Start)
  69. }
  70. .width('100%')
  71. .backgroundColor('#FFF')
  72. .borderRadius(20)
  73. .height(120)
  74. .padding(10)
  75. }
  76. )
  77. }
  78. .width('100%')
  79. .height('100%')
  80. }
  81. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/339328?site
推荐阅读
相关标签
  

闽ICP备14008679号