ui-ts-building-category-list-layout.md 6.0 KB
Newer Older
Z
zengyawen 已提交
1 2 3 4 5
# 构建食物列表List布局

使用List组件和ForEach循环渲染,构建食物列表布局。


Y
yamila 已提交
6 7
1. 在pages目录新建页面FoodCategoryList.ets,将index.ets改名为FoodDetail.ets。
   
Z
zengyawen 已提交
8
2. 新建FoodList组件作为页面入口组件,FoodListItem为其子组件。List组件是列表组件,适用于重复同类数据的展示,其子组件为ListItem,适用于展示列表中的单元。
Y
yamila 已提交
9
   ```ts
Z
zengyawen 已提交
10 11 12 13
   @Component
   struct FoodListItem {
     build() {}
   }
H
HelloCrease 已提交
14

Z
zengyawen 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
   @Entry
   @Component
   struct FoodList {
     build() {
       List() {
         ListItem() {
           FoodListItem()
         }
       }
     }
   }
   ```

3. 引入FoodData类和initializeOnStartup方法。
   ```
   import { FoodData } from '../model/FoodData'
   import { initializeOnStartup } from '../model/FoodDataModels'
   ```

4. FoodList和FoodListItem组件数值传递。在FoodList组件内创建类型为FoodData[]成员变量foodItems,调用initializeOnStartup方法为其赋值。在FoodListItem组件内创建类型为FoodData的成员变量foodItem。将父组件foodItems数组的第一个元素的foodItems[0]作为参数传递给FoodListItem。
Y
yamila 已提交
35
   ```ts
Z
zengyawen 已提交
36 37
   import { FoodData } from '../model/FoodData'
   import { initializeOnStartup } from '../model/FoodDataModels'
H
HelloCrease 已提交
38

Z
zengyawen 已提交
39 40 41 42 43
   @Component
   struct FoodListItem {
     private foodItem: FoodData
     build() {}
   }
H
HelloCrease 已提交
44

Z
zengyawen 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
   @Entry
   @Component
   struct FoodList {
     private foodItems: FoodData[] = initializeOnStartup()
     build() {
       List() {
         ListItem() {
           FoodListItem({ foodItem: this.foodItems[0] })
         }
       }
     }
   }
   ```

5. 声明子组件FoodListItem 的UI布局。创建Flex组件,包含食物图片缩略图,食物名称,和食物对应的卡路里。
Y
yamila 已提交
60
   ```ts
Z
zengyawen 已提交
61 62
   import { FoodData } from '../model/FoodData'
   import { initializeOnStartup } from '../model/FoodDataModels'
H
HelloCrease 已提交
63

Y
yamila 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
      @Component
      struct FoodListItem {
        private foodItem: FoodData
        build() {
          Flex({ justifyContent: FlexAlign.Start, alignItems: ItemAlign.Center }) {
            Image(this.foodItem.image)
              .objectFit(ImageFit.Contain)
              .height(40)
              .width(40)
              .margin({ right: 16 })
            Text(this.foodItem.name)
              .fontSize(14)
              .flexGrow(1)
            Text(this.foodItem.calories + ' kcal')
              .fontSize(14)
          }
          .height(64)
          .margin({ right: 24, left:32 })
        }
      }
   
   @Entry
      @Component
      struct FoodList {
        private foodItems: FoodData[] = initializeOnStartup()
        build() {
          List() {
            ListItem() {
              FoodListItem({ foodItem: this.foodItems[0] })
            }
          }
        }
      }
   ```
   
   
   ![zh-cn_image_0000001204776353](figures/zh-cn_image_0000001204776353.png)
   
6. 创建两个FoodListItem。在List组件创建两个FoodListItem,分别给FoodListItem传递foodItems数组的第一个元素this.foodItems[0]和第二个元素foodItem: this.foodItems[1]。

   ```ts
   import { FoodData } from '../model/FoodData'
   import { initializeOnStartup } from '../model/FoodDataModels'
   
Z
zengyawen 已提交
108 109 110
   @Component
   struct FoodListItem {
     private foodItem: FoodData
Y
yamila 已提交
111
   
Z
zengyawen 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125
     build() {
       Flex({ justifyContent: FlexAlign.Start, alignItems: ItemAlign.Center }) {
         Image(this.foodItem.image)
           .objectFit(ImageFit.Contain)
           .height(40)
           .width(40)
           .margin({ right: 16 })
         Text(this.foodItem.name)
           .fontSize(14)
           .flexGrow(1)
         Text(this.foodItem.calories + ' kcal')
           .fontSize(14)
       }
       .height(64)
Y
yamila 已提交
126
       .margin({ right: 24, left: 32 })
Z
zengyawen 已提交
127 128
     }
   }
Y
yamila 已提交
129
   
Z
zengyawen 已提交
130 131 132 133
   @Entry
   @Component
   struct FoodList {
     private foodItems: FoodData[] = initializeOnStartup()
Y
yamila 已提交
134
   
Z
zengyawen 已提交
135 136 137 138 139
     build() {
       List() {
         ListItem() {
           FoodListItem({ foodItem: this.foodItems[0] })
         }
Y
yamila 已提交
140
   
Z
zengyawen 已提交
141 142 143 144 145 146 147
         ListItem() {
           FoodListItem({ foodItem: this.foodItems[1] })
         }
       }
     }
   }
   ```
Y
yamila 已提交
148 149 150 151 152
   
   
      ![zh-cn_image1_0000001204776353](figures/zh-cn_image1_0000001204776353.png)
   
7. 单独创建每一个FoodListItem肯定是不合理的,这就需要引入[ForEach循环渲染](../quick-start/arkts-rendering-control.md#循环渲染)
Z
zengyawen 已提交
153

Y
yamila 已提交
154
   ```ts
Z
zengyawen 已提交
155 156
   import { FoodData } from '../model/FoodData'
   import { initializeOnStartup } from '../model/FoodDataModels'
Y
yamila 已提交
157
   
Z
zengyawen 已提交
158 159 160 161 162 163 164 165
   @Component
   struct FoodListItem {
     private foodItem: FoodData
     build() {
       Flex({ justifyContent: FlexAlign.Start, alignItems: ItemAlign.Center }) {
         Image(this.foodItem.image)
           .objectFit(ImageFit.Contain)
           .height(40)
Y
yamila 已提交
166
           .width(40)     
Z
zengyawen 已提交
167 168 169 170 171 172 173 174 175 176 177
           .margin({ right: 16 })
         Text(this.foodItem.name)
           .fontSize(14)
           .flexGrow(1)
         Text(this.foodItem.calories + ' kcal')
           .fontSize(14)
       }
       .height(64)
       .margin({ right: 24, left:32 })
     }
   }
Y
yamila 已提交
178
   
Z
zengyawen 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
   @Entry
   @Component
   struct FoodList {
     private foodItems: FoodData[] = initializeOnStartup()
     build() {
       List() {
         ForEach(this.foodItems, item => {
           ListItem() {
             FoodListItem({ foodItem: item })
           }
         }, item => item.id.toString())
       }
     }
   }
   ```
Y
yamila 已提交
194
   
Z
zengyawen 已提交
195
8. 添加FoodList标题。
Y
yamila 已提交
196

Z
zengyawen 已提交
197 198 199 200 201
   ```
   @Entry
   @Component
   struct FoodList {
     private foodItems: FoodData[] = initializeOnStartup()
Y
yamila 已提交
202
   
Z
zengyawen 已提交
203 204
     build() {
       Column() {
Y
yamila 已提交
205
         Flex({ justifyContent: FlexAlign.Start, alignItems: ItemAlign.Center }) {
Z
zengyawen 已提交
206 207
           Text('Food List')
             .fontSize(20)
Y
yamila 已提交
208
             .margin({ left: 20 })
Z
zengyawen 已提交
209 210 211
         }
         .height('7%')
         .backgroundColor('#FFf1f3f5')
Y
yamila 已提交
212
   
Z
zengyawen 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225
         List() {
           ForEach(this.foodItems, item => {
             ListItem() {
               FoodListItem({ foodItem: item })
             }
           }, item => item.id.toString())
         }
         .height('93%')
       }
     }
   }
   ```

Y
yamila 已提交
226
     ![zh-cn_image_0000001169678922](figures/zh-cn_image_0000001169678922.png)