1# ListItemGroup
2
3该组件用来展示列表item分组,宽度默认充满[List](ts-container-list.md)组件,必须配合List组件来使用。
4
5> **说明:**
6>
7> - 该组件从API Version 9开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
8> - 该组件的父组件只能是[List](ts-container-list.md)。
9> - ListItemGroup组件不支持设置[通用属性aspectRatio](ts-universal-attributes-layout-constraints.md)。
10> - 当ListItemGroup的父组件List的listDirection属性为Axis.Vertical时,设置[通用属性height](ts-universal-attributes-size.md)属性不生效。ListItemGroup的高度为header高度、footer高度和所有ListItem布局后总高度之和。
11> - 当父组件List的listDirection属性为Axis.Horizontal时,设置[通用属性width](ts-universal-attributes-size.md)属性不生效。ListItemGroup的宽度为header宽度、footer宽度和所有ListItem布局后总宽度之和。
12> - 当前ListItemGroup内部的ListItem组件不支持编辑、拖拽功能,即ListItem组件的editable属性不生效。
13> - ListItemGroup使用direction属性设置布局方向不生效,ListItemGroup组件布局方向跟随父容器List组件的布局方向。
14
15## 子组件
16
17包含[ListItem](ts-container-listitem.md)子组件。
18
19
20## 接口
21
22ListItemGroup(options?: ListItemGroupOptions)
23
24**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
25
26**系统能力:** SystemCapability.ArkUI.ArkUI.Full
27
28**参数:**
29
30| 参数名 | 类型 | 必填 | 说明 |
31| -------- | -------- | -------- | -------- |
32| options |  [ListItemGroupOptions](#listitemgroupoptions对象说明)| 否 | 列表item分组组件参数。 |
33
34## ListItemGroupOptions对象说明
35
36**系统能力:** SystemCapability.ArkUI.ArkUI.Full
37
38| 名称              | 类型                                            | 必填 | 说明                                                     |
39| ------------------- | --------------------------------------------------- | ---- | ------------------------------------------------------------ |
40| header              | [CustomBuilder](ts-types.md#custombuilder8) &nbsp;   | 否   | 设置ListItemGroup头部组件。<br/>**说明:**<br/>可以放单个子组件或不放子组件。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。               |
41| headerComponent<sup>13+</sup>              | [ComponentContent](../js-apis-arkui-ComponentContent.md)       | 否   | 使用ComponentContent类型参数设置ListItemGroup头部组件。<br/>**说明:**<br/>可以放单个子组件或不放子组件。 该参数的优先级高于参数header。即同时设置header和headerComponent时,以headerComponent设置的值为准。<br/>同一个headerComponent不推荐同时给不同的ListItemGroup使用,否则会导致显示问题。<br/>**原子化服务API:** 从API version 13开始,该接口支持在原子化服务中使用。              |
42| footer              | [CustomBuilder](ts-types.md#custombuilder8) &nbsp;     | 否   | 设置ListItemGroup尾部组件。<br/>**说明:**<br/>可以放单个子组件或不放子组件。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。               |
43| footerComponent<sup>13+</sup>              | [ComponentContent](../js-apis-arkui-ComponentContent.md)       | 否   | 使用ComponentContent类型参数设置ListItemGroup尾部组件。<br/>**说明:**<br/>可以放单个子组件或不放子组件。该参数的优先级高于参数footer。 即同时设置footer和footerComponent时,以footerComponent设置的值为准。<br/>同一个footerComponent不推荐同时给不同的ListItemGroup使用,否则会导致显示问题。<br/>**原子化服务API:** 从API version 13开始,该接口支持在原子化服务中使用。                           |
44| space               | number&nbsp;\|&nbsp;string                          | 否   | 列表项间距。只作用于ListItem与ListItem之间,不作用于header与ListItem、footer与ListItem之间。<br/>默认值:0<br/>单位:vp <br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。  |
45| style<sup>10+</sup> | [ListItemGroupStyle](#listitemgroupstyle10枚举说明) | 否   | 设置List组件卡片样式。<br/>默认值: ListItemGroupStyle.NONE<br/>设置为ListItemGroupStyle.NONE时无样式。<br/>设置为ListItemGroupStyle.CARD时,建议配合[ListItem](ts-container-listitem.md)的ListItemStyle.CARD同时使用,显示默认卡片样式。 <br/>卡片样式下,ListItemGroup默认规格:左右外边距12vp,上下左右内边距4vp。<br/>卡片样式下, 为卡片内的列表选项提供了默认的focus、hover、press、selected和disable样式。<br/>**说明:**<br/>当前卡片模式下,使用默认Axis.Vertical排列方向,如果listDirection属性设置为Axis.Horizontal,会导致显示混乱;List属性alignListItem默认为ListItemAlign.Center,居中对齐显示。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
46
47## 属性
48
49### divider
50
51divider(value: {strokeWidth: Length; color?: ResourceColor; startMargin?: Length; endMargin?: Length;} | null,)
52
53设置ListItem分割线样式,默认无分割线。
54
55strokeWidth, startMargin和endMargin不支持设置百分比。
56
57ListItem设置[多态样式](ts-universal-attributes-polymorphic-style.md)时,被按压的子组件上下的分割线不绘制。
58
59**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
60
61**系统能力:** SystemCapability.ArkUI.ArkUI.Full
62
63**参数:**
64
65| 参数名 | 类型                                                         | 必填 | 说明                                                         |
66| ------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
67| value  | {<br/>strokeWidth:&nbsp;[Length](ts-types.md#length),<br/>color?:[ResourceColor](ts-types.md#resourcecolor),<br/>startMargin?:&nbsp;[Length](ts-types.md#length),<br/>endMargin?:&nbsp;[Length](ts-types.md#length)<br/>}&nbsp;\|&nbsp;null | 是   | ListItem分割线样式。<br/>- strokeWidth:&nbsp;分割线的线宽。<br/>- color:&nbsp;分割线的颜色。<br/> 默认值:0x08000000<br/>- startMargin:&nbsp;分割线与列表侧边起始端的距离。<br/>默认值:0,单位:vp<br/>- endMargin:&nbsp;分割线与列表侧边结束端的距离。<br/>默认值:0,单位:vp |
68
69### childrenMainSize<sup>12+</sup>
70
71childrenMainSize(value: ChildrenMainSize)
72
73设置ListItemGroup组件的子组件在主轴方向的大小信息。
74
75**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
76
77**系统能力:** SystemCapability.ArkUI.ArkUI.Full
78
79**参数:**
80
81| 参数名     | 类型   | 必填 | 说明                            |
82| ---------- | ------ | ---- | ------------------------------- |
83| value | [ChildrenMainSize](ts-container-scrollable-common.md#childrenmainsize12对象说明) | 是   | 1.通过ChildrenMainSize对象向ListItemGroup组件准确提供所有ListItem在主轴方向的大小信息。<br/>2.提供的主轴方向大小必须与子组件实际在主轴方向的大小一致。<br/>3. 必须同时设置List的childrenMainSize属性使用。<br/>4.子组件主轴方向大小变化或者增删子组件时都必须通过ChildrenMainSize对象方法通知ListItemGroup组件。|
84
85## ListItemGroupStyle<sup>10+</sup>枚举说明
86
87**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
88
89**系统能力:** SystemCapability.ArkUI.ArkUI.Full
90
91| 名称 | 值  | 说明             |
92| ---- | ---- | ------------------ |
93| NONE | 0 | 无样式。           |
94| CARD | 1 | 显示默认卡片样式。 |
95
96
97
98## 示例
99
100### 示例1(设置吸顶/吸底)
101
102该示例通过stick实现了Header吸顶和Footer吸底的效果。
103
104```ts
105// xxx.ets
106@Entry
107@Component
108struct ListItemGroupExample {
109  private timeTable: TimeTable[] = [
110    {
111      title: '星期一',
112      projects: ['语文', '数学', '英语']
113    },
114    {
115      title: '星期二',
116      projects: ['物理', '化学', '生物']
117    },
118    {
119      title: '星期三',
120      projects: ['历史', '地理', '政治']
121    },
122    {
123      title: '星期四',
124      projects: ['美术', '音乐', '体育']
125    }
126  ]
127
128  @Builder
129  itemHead(text: string) {
130    Text(text)
131      .fontSize(20)
132      .backgroundColor(0xAABBCC)
133      .width("100%")
134      .padding(10)
135  }
136
137  @Builder
138  itemFoot(num: number) {
139    Text('共' + num + "节课")
140      .fontSize(16)
141      .backgroundColor(0xAABBCC)
142      .width("100%")
143      .padding(5)
144  }
145
146  build() {
147    Column() {
148      List({ space: 20 }) {
149        ForEach(this.timeTable, (item: TimeTable) => {
150          ListItemGroup({ header: this.itemHead(item.title), footer: this.itemFoot(item.projects.length) }) {
151            ForEach(item.projects, (project: string) => {
152              ListItem() {
153                Text(project)
154                  .width("100%")
155                  .height(100)
156                  .fontSize(20)
157                  .textAlign(TextAlign.Center)
158                  .backgroundColor(0xFFFFFF)
159              }
160            }, (item: string) => item)
161          }
162          .divider({ strokeWidth: 1, color: Color.Blue }) // 每行之间的分界线
163        })
164      }
165      .width('90%')
166      .sticky(StickyStyle.Header | StickyStyle.Footer)
167      .scrollBar(BarState.Off)
168    }.width('100%').height('100%').backgroundColor(0xDCDCDC).padding({ top: 5 })
169  }
170}
171
172interface TimeTable {
173  title: string;
174  projects: string[];
175}
176```
177
178![zh-cn_image_0000001219864159](figures/zh-cn_image_listitemgroup.gif)
179
180### 示例2(设置卡片样式)
181
182该示例展示了ListItemGroup的卡片样式效果。
183
184```ts
185// xxx.ets
186@Entry
187@Component
188struct ListItemGroupExample2 {
189  private arr: ArrObject[] = [
190    {
191      style: ListItemGroupStyle.CARD,
192      itemStyles: [ListItemStyle.CARD, ListItemStyle.CARD, ListItemStyle.CARD]
193    },
194    {
195      style: ListItemGroupStyle.CARD,
196      itemStyles: [ListItemStyle.CARD, ListItemStyle.CARD, ListItemStyle.NONE]
197    },
198    {
199      style: ListItemGroupStyle.CARD,
200      itemStyles: [ListItemStyle.CARD, ListItemStyle.NONE, ListItemStyle.CARD]
201    },
202    {
203      style: ListItemGroupStyle.NONE,
204      itemStyles: [ListItemStyle.CARD, ListItemStyle.CARD, ListItemStyle.NONE]
205    }
206  ]
207
208  build() {
209    Column() {
210      List({ space: "4vp", initialIndex: 0 }) {
211        ForEach(this.arr, (item: ArrObject, index?: number) => {
212          ListItemGroup({ style: item.style }) {
213            ForEach(item.itemStyles, (itemStyle: number, itemIndex?: number) => {
214              ListItem({ style: itemStyle }) {
215                if (index != undefined && itemIndex != undefined) {
216                  Text("第" + (index + 1) + "个Group中第" + (itemIndex + 1) + "个item")
217                    .width("100%")
218                    .textAlign(TextAlign.Center)
219                }
220              }
221            }, (item: string) => item)
222          }
223        })
224      }
225      .width('100%')
226      .multiSelectable(true)
227      .backgroundColor(0xDCDCDC)
228    }
229    .width('100%')
230    .padding({ top: 5 })
231  }
232}
233
234interface ArrObject {
235  style: number;
236  itemStyles: number[];
237}
238```
239![ListItemGroupStyle](figures/listItemGroup2.jpeg)
240
241### 示例3(设置Header/Footer242
243该示例通过ComponentContent设置Header/Footer244
245```ts
246// xxx.ets
247import { ComponentContent } from '@kit.ArkUI';
248
249interface TimeTable {
250  title: string;
251  projects: string[];
252}
253
254class HeadBuilderParams {
255  text: string | Resource;
256  constructor(text: string | Resource) {
257    this.text = text;
258  }
259}
260
261class FootBuilderParams {
262  num: number | Resource;
263  constructor(num: number | Resource) {
264    this.num = num;
265  }
266}
267
268@Builder
269function itemHead(params: HeadBuilderParams) {
270  Text(params.text)
271    .fontSize(20)
272    .height('48vp')
273    .width("100%")
274    .padding(10)
275    .backgroundColor($r('sys.color.background_tertiary'))
276}
277
278@Builder
279function itemFoot(params: FootBuilderParams) {
280  Text('共' + params.num + '节课')
281    .fontSize(20)
282    .height('48vp')
283    .width("100%")
284    .padding(10)
285    .backgroundColor($r('sys.color.background_tertiary'))
286}
287
288@Component
289struct MyItemGroup {
290  item: TimeTable = { title: "", projects: [] }
291  header?: ComponentContent<HeadBuilderParams> = undefined
292  footer?: ComponentContent<FootBuilderParams> = undefined
293  headerParam = new HeadBuilderParams(this.item.title)
294  footerParam = new FootBuilderParams(this.item.projects.length)
295
296  aboutToAppear(): void {
297    this.header = new ComponentContent(this.getUIContext(), wrapBuilder(itemHead), this.headerParam)
298    this.footer = new ComponentContent(this.getUIContext(), wrapBuilder(itemFoot), this.footerParam)
299  }
300  GetHeader() {
301    this.header?.update(new HeadBuilderParams(this.item.title));
302    return this.header;
303  }
304
305  GetFooter() {
306    this.footer?.update(new FootBuilderParams(this.item.projects.length));
307    return this.footer;
308  }
309
310  build() {
311    ListItemGroup({
312      headerComponent: this.GetHeader(),
313      footerComponent: this.GetFooter()
314    }) {
315      ForEach(this.item.projects, (project: string) => {
316        ListItem() {
317          Text(project)
318            .width("100%")
319            .height(100)
320            .fontSize(20)
321            .textAlign(TextAlign.Center)
322        }
323      }, (item: string) => item)
324    }
325    .divider({ strokeWidth: 1, color: Color.Blue }) // 每行之间的分界线
326  }
327}
328
329@Entry
330@Component
331struct ListItemGroupExample {
332  @State timeTable: TimeTable[] = [
333    {
334      title: '星期一',
335      projects: ['语文', '数学', '英语']
336    },
337    {
338      title: '星期二',
339      projects: ['物理', '化学', '生物']
340    },
341    {
342      title: '星期三',
343      projects: ['历史', '地理', '政治', '体育']
344    },
345    {
346      title: '星期四',
347      projects: ['美术', '音乐']
348    }
349  ]
350
351  build() {
352    Column() {
353      Button("update").width(100).height(50).onClick(() => {
354        this.timeTable[0] = {
355          title: '更新后的星期一',
356          projects: ['语文', '物理', '历史', '美术']
357        }
358      })
359      List({ space: 20 }) {
360        ForEach(this.timeTable, (item: TimeTable) => {
361          MyItemGroup({ item: item })
362        })
363      }
364      .layoutWeight(1)
365      .sticky(StickyStyle.Header | StickyStyle.Footer)
366      .scrollBar(BarState.Off)
367    }
368    .backgroundColor($r('sys.color.background_primary'))
369  }
370}
371```
372
373![zh-cn_image_listitemgroup_example03](figures/zh-cn_image_listitemgroup_example03.gif)