1e41f4b71Sopenharmony_ci# Repeat
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciRepeat组件不开启virtualScroll开关时,Repeat基于数组类型数据来进行循环渲染,需要与容器组件配合使用,且接口返回的组件应当是允许包含在Repeat父容器组件中的子组件。Repeat循环渲染和ForEach相比有两个区别,一是优化了部分更新场景下的渲染性能,二是组件生成函数的索引index由框架侧来维护。
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ciRepeat组件开启virtualScroll开关时,Repeat将从提供的数据源中按需迭代数据,并在每次迭代过程中创建相应的组件。当在滚动容器中使用了Repeat,框架会根据滚动容器可视区域按需创建组件,当组件滑出可视区域外时,框架会缓存组件,并在下一次迭代中使用。
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci>**说明:**
8e41f4b71Sopenharmony_ci>
9e41f4b71Sopenharmony_ci> 本模块首批接口从API version 12开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
10e41f4b71Sopenharmony_ci>
11e41f4b71Sopenharmony_ci> 当前状态管理(V2试用版)仍在逐步开发中,相关功能尚未成熟,建议开发者尝鲜试用。
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci**卡片能力:** 从API version 12开始,该接口支持在ArkTS卡片中使用。
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci**参数:**
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ci| 参数名 | 类型       | 必填 | 说明      |
20e41f4b71Sopenharmony_ci| ------ | ---------- | -------- | -------- |
21e41f4b71Sopenharmony_ci| arr    | Array\<T\> | 是 | 数据源,为`Array<T>`类型的数组,由开发者决定数据类型。 |
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci**返回值:**
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci| 类型 | 说明 |
26e41f4b71Sopenharmony_ci| --- | --- |
27e41f4b71Sopenharmony_ci| [RepeatAttribute](#repeatattribute)\<T\> | Repeat组件属性 |
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_ci## RepeatAttribute
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ci### each
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_cieach(itemGenerator: (repeatItem: RepeatItem\<T\>) => void): RepeatAttribute\<T\>
34e41f4b71Sopenharmony_ci
35e41f4b71Sopenharmony_ci组件生成函数。template和templateId匹配不上的数据项走默认生成函数each。
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci**说明:**
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ci- `each`属性必须有,否则运行时会报错。
40e41f4b71Sopenharmony_ci- `itemGenerator`的参数为`RepeatItem`,该参数将`item`和`index`结合到了一起,请勿将`RepeatItem`参数拆开使用。
41e41f4b71Sopenharmony_ci
42e41f4b71Sopenharmony_ci**卡片能力:** 从API version 12开始,该接口支持在ArkTS卡片中使用。
43e41f4b71Sopenharmony_ci
44e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
45e41f4b71Sopenharmony_ci
46e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full
47e41f4b71Sopenharmony_ci
48e41f4b71Sopenharmony_ci**参数:**
49e41f4b71Sopenharmony_ci
50e41f4b71Sopenharmony_ci| 参数名 | 类型   | 必填 | 说明 |
51e41f4b71Sopenharmony_ci| ------ | ---------- | -------- | -------- |
52e41f4b71Sopenharmony_ci| repeatItem  | [RepeatItem](#repeatitem)\<T\> | 是 | repeat数据项 |
53e41f4b71Sopenharmony_ci
54e41f4b71Sopenharmony_ci### key
55e41f4b71Sopenharmony_ci
56e41f4b71Sopenharmony_cikey(keyGenerator: (item: T, index: number) => string): RepeatAttribute\<T\>
57e41f4b71Sopenharmony_ci
58e41f4b71Sopenharmony_ci键值生成函数。
59e41f4b71Sopenharmony_ci
60e41f4b71Sopenharmony_ci**卡片能力:** 从API version 12开始,该接口支持在ArkTS卡片中使用。
61e41f4b71Sopenharmony_ci
62e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
63e41f4b71Sopenharmony_ci
64e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full
65e41f4b71Sopenharmony_ci
66e41f4b71Sopenharmony_ci**参数:**
67e41f4b71Sopenharmony_ci
68e41f4b71Sopenharmony_ci| 参数名 | 类型   | 必填 | 说明  |
69e41f4b71Sopenharmony_ci| ------ | ---------- | -------- | -------- |
70e41f4b71Sopenharmony_ci| item  | T | 是 | `arr`数组中的数据项 |
71e41f4b71Sopenharmony_ci| index  | number | 是 | `arr`数组中的数据项索引 |
72e41f4b71Sopenharmony_ci
73e41f4b71Sopenharmony_ci### virtualScroll
74e41f4b71Sopenharmony_ci
75e41f4b71Sopenharmony_civirtualScroll(virtualScrollOptions?: VirtualScrollOptions): RepeatAttribute\<T\>
76e41f4b71Sopenharmony_ci
77e41f4b71Sopenharmony_ci`Repeat`开启虚拟滚动。
78e41f4b71Sopenharmony_ci
79e41f4b71Sopenharmony_ci**卡片能力:** 从API version 12开始,该接口支持在ArkTS卡片中使用。
80e41f4b71Sopenharmony_ci
81e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
82e41f4b71Sopenharmony_ci
83e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full
84e41f4b71Sopenharmony_ci
85e41f4b71Sopenharmony_ci**参数:**
86e41f4b71Sopenharmony_ci
87e41f4b71Sopenharmony_ci| 参数名 | 类型   | 必填 | 说明  |
88e41f4b71Sopenharmony_ci| ------ | ---------- | -------- | -------- |
89e41f4b71Sopenharmony_ci| virtualScrollOptions  | [VirtualScrollOptions](#virtualscrolloptions)  | 否 | 虚拟滚动配置项 |
90e41f4b71Sopenharmony_ci
91e41f4b71Sopenharmony_ci### template
92e41f4b71Sopenharmony_ci
93e41f4b71Sopenharmony_citemplate(type: string, itemBuilder: RepeatItemBuilder\<T\>, templateOptions?: TemplateOptions): RepeatAttribute\<T\>
94e41f4b71Sopenharmony_ci
95e41f4b71Sopenharmony_ci复用模板。
96e41f4b71Sopenharmony_ci
97e41f4b71Sopenharmony_ci**卡片能力:** 从API version 12开始,该接口支持在ArkTS卡片中使用。
98e41f4b71Sopenharmony_ci
99e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
100e41f4b71Sopenharmony_ci
101e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full
102e41f4b71Sopenharmony_ci
103e41f4b71Sopenharmony_ci**参数:**
104e41f4b71Sopenharmony_ci
105e41f4b71Sopenharmony_ci| 参数名 | 类型   | 必填 | 说明  |
106e41f4b71Sopenharmony_ci| ------ | ---------- | -------- | -------- |
107e41f4b71Sopenharmony_ci| type | string | 是 | 当前模板类型 |
108e41f4b71Sopenharmony_ci| itemBuilder  | [RepeatItemBuilder](#repeatitembuilder)\<T\> | 是 | 组件生成函数 |
109e41f4b71Sopenharmony_ci| templateOptions | [TemplateOptions](#templateoptions) | 否 | 当前模板配置项 |
110e41f4b71Sopenharmony_ci
111e41f4b71Sopenharmony_ci### templateId
112e41f4b71Sopenharmony_ci
113e41f4b71Sopenharmony_citemplateId(typedFunc: TemplateTypedFunc\<T\>): RepeatAttribute\<T\>
114e41f4b71Sopenharmony_ci
115e41f4b71Sopenharmony_ci为当前数据项分配templateId。
116e41f4b71Sopenharmony_ci
117e41f4b71Sopenharmony_ci**卡片能力:** 从API version 12开始,该接口支持在ArkTS卡片中使用。
118e41f4b71Sopenharmony_ci
119e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
120e41f4b71Sopenharmony_ci
121e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full
122e41f4b71Sopenharmony_ci
123e41f4b71Sopenharmony_ci**参数:**
124e41f4b71Sopenharmony_ci
125e41f4b71Sopenharmony_ci| 参数名 | 类型   | 必填 | 说明  |
126e41f4b71Sopenharmony_ci| ------ | ---------- | -------- | -------- |
127e41f4b71Sopenharmony_ci| typedFunc | [TemplateTypedFunc](#templatetypedfunc)\<T\> | 是 | 生成当前数据项对应的templateId |
128e41f4b71Sopenharmony_ci
129e41f4b71Sopenharmony_ci## RepeatItem
130e41f4b71Sopenharmony_ci
131e41f4b71Sopenharmony_ci**卡片能力:** 从API version 12开始,该接口支持在ArkTS卡片中使用。
132e41f4b71Sopenharmony_ci
133e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
134e41f4b71Sopenharmony_ci
135e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full
136e41f4b71Sopenharmony_ci
137e41f4b71Sopenharmony_ci**参数:**
138e41f4b71Sopenharmony_ci
139e41f4b71Sopenharmony_ci| 参数名 | 类型   | 必填 | 说明                                         |
140e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------------------------------------------- |
141e41f4b71Sopenharmony_ci| item   | T      | 是   | arr中每一个数据项。T为开发者传入的数据类型。 |
142e41f4b71Sopenharmony_ci| index  | number | 是   | 当前数据项对应的索引。                       |
143e41f4b71Sopenharmony_ci
144e41f4b71Sopenharmony_ci## VirtualScrollOptions
145e41f4b71Sopenharmony_ci
146e41f4b71Sopenharmony_ci**卡片能力:** 从API version 12开始,该接口支持在ArkTS卡片中使用。
147e41f4b71Sopenharmony_ci
148e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
149e41f4b71Sopenharmony_ci
150e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full
151e41f4b71Sopenharmony_ci
152e41f4b71Sopenharmony_ci**参数:**
153e41f4b71Sopenharmony_ci
154e41f4b71Sopenharmony_ci| 参数名     | 类型   | 必填 | 说明                                                         |
155e41f4b71Sopenharmony_ci| ---------- | ------ | ---- | ------------------------------------------------------------ |
156e41f4b71Sopenharmony_ci| totalCount | number | 否   | 数据源的总长度,可以大于已加载数据项的数量 |
157e41f4b71Sopenharmony_ci
158e41f4b71Sopenharmony_ci## RepeatItemBuilder
159e41f4b71Sopenharmony_ci
160e41f4b71Sopenharmony_citype RepeatItemBuilder\<T\> = (repeatItem: RepeatItem\<T\>) => void
161e41f4b71Sopenharmony_ci
162e41f4b71Sopenharmony_ci**卡片能力:** 从API version 12开始,该接口支持在ArkTS卡片中使用。
163e41f4b71Sopenharmony_ci
164e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
165e41f4b71Sopenharmony_ci
166e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full
167e41f4b71Sopenharmony_ci
168e41f4b71Sopenharmony_ci**参数:**
169e41f4b71Sopenharmony_ci
170e41f4b71Sopenharmony_ci| 参数名     | 类型          | 必填      | 说明                                    |
171e41f4b71Sopenharmony_ci| ---------- | ------------- | --------------------------------------- | --------------------------------------- |
172e41f4b71Sopenharmony_ci| repeatItem | [RepeatItem](#repeatitem)\<T\> | 是 | 将item和index结合到一起的一个状态变量。 |
173e41f4b71Sopenharmony_ci
174e41f4b71Sopenharmony_ci## TemplateOptions
175e41f4b71Sopenharmony_ci
176e41f4b71Sopenharmony_ci**卡片能力:** 从API version 12开始,该接口支持在ArkTS卡片中使用。
177e41f4b71Sopenharmony_ci
178e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
179e41f4b71Sopenharmony_ci
180e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full
181e41f4b71Sopenharmony_ci
182e41f4b71Sopenharmony_ci**参数:**
183e41f4b71Sopenharmony_ci
184e41f4b71Sopenharmony_ci| 参数名      | 类型   | 必填 | 说明                                                         |
185e41f4b71Sopenharmony_ci| ----------- | ------ | ---- | ------------------------------------------------------------ |
186e41f4b71Sopenharmony_ci| cachedCount | number | 否   | 当前模板在Repeat的缓存池中可缓存子节点的最大数量,仅在开启virtualScroll后生效。 |
187e41f4b71Sopenharmony_ci
188e41f4b71Sopenharmony_ci## TemplateTypedFunc
189e41f4b71Sopenharmony_ci
190e41f4b71Sopenharmony_citype TemplateTypedFunc\<T\> = (item : T, index : number) => string
191e41f4b71Sopenharmony_ci
192e41f4b71Sopenharmony_ci**卡片能力:** 从API version 12开始,该接口支持在ArkTS卡片中使用。
193e41f4b71Sopenharmony_ci
194e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
195e41f4b71Sopenharmony_ci
196e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full
197e41f4b71Sopenharmony_ci
198e41f4b71Sopenharmony_ci**参数:**
199e41f4b71Sopenharmony_ci
200e41f4b71Sopenharmony_ci| 参数名 | 类型   | 必填 | 说明                                         |
201e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------------------------------------------- |
202e41f4b71Sopenharmony_ci| item   | T      | 是   | arr中每一个数据项。T为开发者传入的数据类型。 |
203e41f4b71Sopenharmony_ci| index  | number | 是   | 当前数据项对应的索引。                       |