• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# ListItemGroup
2
3The **\<ListItemGroup>** component is used to display list item groups. It must be used with the **[\<List>](ts-container-list.md)** component and, if not otherwise specified, takes up the entire width of the **\<List>**.
4
5> **NOTE**
6>
7> This component is supported since API version 9. Updates will be marked with a superscript to indicate their earliest API version.
8
9## Usage Guidelines
10
11If the **listDirection** attribute of the parent **\<List>** component is set to **Axis.Vertical**, the **height** attribute of the **\<ListItemGroup>** component is fixed at the sum of the component's header height, footer height, and total height of the list items. If the **listDirection** attribute of the parent **\<List>** component is set to **Axis.Horizontal**, the **width** attribute of the **\<ListItemGroup>** component is fixed at the sum of the component's header width, footer width, and total width of the list items.
12
13The list items in the **\<ListItemGroup>** component cannot be edited or dragged. That is, their **editable** attribute does not take effect.
14
15## Child Components
16
17This component supports the **[\<ListItem>](ts-container-listitem.md)** child component.
18
19
20## APIs
21
22ListItemGroup(options?: {header?: CustomBuilder, footer?: CustomBuilder, space?: number | string, style?: ListItemGroupStyle})
23
24**Parameters**
25
26| Name             | Type                                           | Mandatory| Description                                                    |
27| ------------------- | --------------------------------------------------- | ---- | ------------------------------------------------------------ |
28| header              | [CustomBuilder](ts-types.md#custombuilder8)         | No  | Header of the list item group.                                 |
29| footer              | [CustomBuilder](ts-types.md#custombuilder8)         | No  | Footer of the list item group.                                 |
30| space               | number \| string                          | No  | Spacing between list items. This parameter is valid only between list items, but not between the header and list item or between the footer and list item.|
31| style<sup>10+</sup> | [ListItemGroupStyle](#listitemgroupstyle10) | No  | Style of the list item group.<br>Default value: **ListItemGroupStyle.NONE**<br>If this parameter is set to **ListItemGroupStyle.NONE**, no style is applied.<br>If this parameter is set to **ListItemGroupStyle.CARD**, the default card style is applied, but only when **ListItemStyle.CARD** is set for [\<ListItem>](ts-container-listitem.md).<br>In the default card style, list items can be in focus, hover, press, selected, or disable style depending on their state.<br>**NOTE**<br>In the default card style, the parent **\<List>** component has its **listDirection** attribute fixed at **Axis.Vertical** and its **alignListItem** attribute defaulted at **ListItemAlign.Center**; the **header** and **footer** parameters cannot be set for the list item group.|
32
33## Attributes
34
35| Name| Type|  Description|
36| -------- | -------- | -------- |
37| divider | {<br>strokeWidth: [Length](ts-types.md#length),<br>color?: [ResourceColor](ts-types.md#resourcecolor),<br>startMargin?: [Length](ts-types.md#length),<br>endMargin?: [Length](ts-types.md#length)<br>} \| null | Style of the divider for the list items. By default, there is no divider.<br>- **strokeWidth**: stroke width of the divider.<br>- **color**: color of the divider.<br>- **startMargin**: distance between the divider and the start of the list.<br>- **endMargin**: distance between the divider and the end of the list.|
38
39## ListItemGroupStyle<sup>10+</sup>
40
41| Name| Description              |
42| ---- | ------------------ |
43| NONE | No style.          |
44| CARD | Default card style.|
45
46> **NOTE**
47>
48> The **\<ListItemGroup>** component does not support the universal attribute [aspectRatio](ts-universal-attributes-layout-constraints.md).
49>
50> If the main axis of **\<ListItemGroup>** runs in the vertical direction, the [height](ts-universal-attributes-size.md) setting does not take effect.
51>
52> If the main axis of **\<ListItemGroup>** runs in the horizontal direction, the [width](ts-universal-attributes-size.md) setting does not take effect.
53
54
55## Example
56
57### Example 1
58
59```ts
60// xxx.ets
61@Entry
62@Component
63struct ListItemGroupExample {
64  private timetable: TimeTable[] = [
65    {
66      title: 'Monday',
67      projects: ['Language', 'Math', 'English']
68    },
69    {
70      title: 'Tuesday',
71      projects: ['Physics', 'Chemistry', 'Biologics']
72    },
73    {
74      title: 'Wednesday',
75      projects: ['History', 'Geography', 'Politics']
76    },
77    {
78      title: 'Thursday',
79      projects: ['Art', 'Music', 'Sports']
80    }
81  ]
82
83  @Builder
84  itemHead(text: string) {
85    Text(text)
86      .fontSize(20)
87      .backgroundColor(0xAABBCC)
88      .width("100%")
89      .padding(10)
90  }
91
92  @Builder
93  itemFoot(num: number) {
94    Text(''Total lessons:'+ num")
95      .fontSize(16)
96      .backgroundColor(0xAABBCC)
97      .width("100%")
98      .padding(5)
99  }
100
101  build() {
102    Column() {
103      List({ space: 20 }) {
104        ForEach(this.timetable, (item: TimeTable) => {
105          ListItemGroup({ header: this.itemHead(item.title), footer: this.itemFoot(item.projects.length) }) {
106            ForEach(item.projects, (project: string) => {
107              ListItem() {
108                Text(project)
109                  .width("100%")
110                  .height(100)
111                  .fontSize(20)
112                  .textAlign(TextAlign.Center)
113                  .backgroundColor(0xFFFFFF)
114              }
115            }, (item: string) => item)
116          }
117          .divider ({ strokeWidth: 1,color:Color.Blue }) // Divider between lines
118        })
119      }
120      .width('90%')
121      .sticky(StickyStyle.Header | StickyStyle.Footer)
122      .scrollBar(BarState.Off)
123    }.width('100%').height('100%').backgroundColor(0xDCDCDC).padding({ top: 5 })
124  }
125}
126
127interface TimeTable {
128  title: string;
129  projects: string[];
130}
131```
132
133![en-us_image_0000001219864159](figures/en-us_image_listitemgroup.gif)
134
135### Example 2
136
137```ts
138// xxx.ets
139@Entry
140@Component
141struct ListItemGroupExample2 {
142  private arr: ArrObject[] = [
143    {
144      style: ListItemGroupStyle.CARD,
145      itemStyles: [ListItemStyle.CARD, ListItemStyle.CARD, ListItemStyle.CARD]
146    },
147    {
148      style: ListItemGroupStyle.CARD,
149      itemStyles: [ListItemStyle.CARD, ListItemStyle.CARD, ListItemStyle.NONE]
150    },
151    {
152      style: ListItemGroupStyle.CARD,
153      itemStyles: [ListItemStyle.CARD, ListItemStyle.NONE, ListItemStyle.CARD]
154    },
155    {
156      style: ListItemGroupStyle.NONE,
157      itemStyles: [ListItemStyle.CARD, ListItemStyle.CARD, ListItemStyle.NONE]
158    }
159  ]
160
161  build() {
162    Column() {
163      List({ space: "4vp", initialIndex: 0 }) {
164        ForEach(this.arr, (item: ArrObject, index?: number) => {
165          ListItemGroup({ style: item.style }) {
166            ForEach(item.itemStyles, (itemStyle: number, itemIndex?: number) => {
167              ListItem({ style: itemStyle }) {
168                if (index != undefined && itemIndex != undefined) {
169                  Text("Item" + (itemIndex + 1) +" in group" + (index + 1) +" ")
170                    .width("100%")
171                    .textAlign(TextAlign.Center)
172                }
173              }
174            }, (item: string) => item)
175          }
176        })
177      }
178      .width('100%')
179      .multiSelectable(true)
180      .backgroundColor(0xDCDCDC) // List in light blue
181    }
182    .width('100%')
183    .padding({ top: 5 })
184  }
185}
186
187interface ArrObject {
188  style: number;
189  itemStyles: number[];
190}
191```
192![ListItemGroupStyle](figures/listItemGroup2.jpeg)
193