• 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?: ListItemGroupOptions)
23
24**Parameters**
25
26| Name| Type| Mandatory| Description|
27| -------- | -------- | -------- | -------- |
28| options |  [ListItemGroupOptions](#listitemgroupoptions)| Yes| Parameters of the list item group.|
29
30## ListItemGroupOptions
31
32| Name             | Type                                           | Mandatory| Description                                                    |
33| ------------------- | --------------------------------------------------- | ---- | ------------------------------------------------------------ |
34| header              | [CustomBuilder](ts-types.md#custombuilder8)         | No  | Header of the list item group.<br>**NOTE**<br>Only one child component can be placed.                                 |
35| footer              | [CustomBuilder](ts-types.md#custombuilder8)         | No  | Footer of the list item group.<br>**NOTE**<br>Only one child component can be placed.                                 |
36| 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.|
37| 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.<br>If **ListItemGroupStyle.CARD** is set and **ListItemStyle.CARD** is not, only some card styles and functions are available.|
38
39## Attributes
40
41| Name| Type|  Description|
42| -------- | -------- | -------- |
43| 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.<br>**strokeWidth**, **startMargin**, and **endMargin** cannot be set in percentage.|
44
45## ListItemGroupStyle<sup>10+</sup>
46
47| Name| Value | Description              |
48| ---- | ---- | ------------------ |
49| NONE | 0 | No style.          |
50| CARD | 1 | Default card style.|
51
52> **NOTE**
53>
54> The **\<ListItemGroup>** component does not support the universal attribute [aspectRatio](ts-universal-attributes-layout-constraints.md).
55>
56> If the main axis of **\<ListItemGroup>** runs in the vertical direction, the [height](ts-universal-attributes-size.md) setting does not take effect.
57>
58> If the main axis of **\<ListItemGroup>** runs in the horizontal direction, the [width](ts-universal-attributes-size.md) setting does not take effect.
59
60
61## Example
62
63### Example 1
64
65```ts
66// xxx.ets
67@Entry
68@Component
69struct ListItemGroupExample {
70  private timeTable: TimeTable[] = [
71    {
72      title: 'Monday',
73      projects: ['Language', 'Math', 'English']
74    },
75    {
76      title: 'Tuesday',
77      projects: ['Physics', 'Chemistry', 'Biologics']
78    },
79    {
80      title: 'Wednesday',
81      projects: ['History', 'Geography', 'Politics']
82    },
83    {
84      title: 'Thursday',
85      projects: ['Art', 'Music', 'Sports']
86    }
87  ]
88
89  @Builder
90  itemHead(text: string) {
91    Text(text)
92      .fontSize(20)
93      .backgroundColor(0xAABBCC)
94      .width("100%")
95      .padding(10)
96  }
97
98  @Builder
99  itemFoot(num: number) {
100    Text(''Total lessons:'+ num")
101      .fontSize(16)
102      .backgroundColor(0xAABBCC)
103      .width("100%")
104      .padding(5)
105  }
106
107  build() {
108    Column() {
109      List({ space: 20 }) {
110        ForEach(this.timeTable, (item: TimeTable) => {
111          ListItemGroup({ header: this.itemHead(item.title), footer: this.itemFoot(item.projects.length) }) {
112            ForEach(item.projects, (project: string) => {
113              ListItem() {
114                Text(project)
115                  .width("100%")
116                  .height(100)
117                  .fontSize(20)
118                  .textAlign(TextAlign.Center)
119                  .backgroundColor(0xFFFFFF)
120              }
121            }, (item: string) => item)
122          }
123          .divider ({ strokeWidth: 1,color:Color.Blue }) // Divider between lines
124        })
125      }
126      .width('90%')
127      .sticky(StickyStyle.Header | StickyStyle.Footer)
128      .scrollBar(BarState.Off)
129    }.width('100%').height('100%').backgroundColor(0xDCDCDC).padding({ top: 5 })
130  }
131}
132
133interface TimeTable {
134  title: string;
135  projects: string[];
136}
137```
138
139![en-us_image_0000001219864159](figures/en-us_image_listitemgroup.gif)
140
141### Example 2
142
143```ts
144// xxx.ets
145@Entry
146@Component
147struct ListItemGroupExample2 {
148  private arr: ArrObject[] = [
149    {
150      style: ListItemGroupStyle.CARD,
151      itemStyles: [ListItemStyle.CARD, ListItemStyle.CARD, ListItemStyle.CARD]
152    },
153    {
154      style: ListItemGroupStyle.CARD,
155      itemStyles: [ListItemStyle.CARD, ListItemStyle.CARD, ListItemStyle.NONE]
156    },
157    {
158      style: ListItemGroupStyle.CARD,
159      itemStyles: [ListItemStyle.CARD, ListItemStyle.NONE, ListItemStyle.CARD]
160    },
161    {
162      style: ListItemGroupStyle.NONE,
163      itemStyles: [ListItemStyle.CARD, ListItemStyle.CARD, ListItemStyle.NONE]
164    }
165  ]
166
167  build() {
168    Column() {
169      List({ space: "4vp", initialIndex: 0 }) {
170        ForEach(this.arr, (item: ArrObject, index?: number) => {
171          ListItemGroup({ style: item.style }) {
172            ForEach(item.itemStyles, (itemStyle: number, itemIndex?: number) => {
173              ListItem({ style: itemStyle }) {
174                if (index != undefined && itemIndex != undefined) {
175                  Text("Item" + (itemIndex + 1) +" in group" + (index + 1) +" ")
176                    .width("100%")
177                    .textAlign(TextAlign.Center)
178                }
179              }
180            }, (item: string) => item)
181          }
182        })
183      }
184      .width('100%')
185      .multiSelectable(true)
186      .backgroundColor(0xDCDCDC)
187    }
188    .width('100%')
189    .padding({ top: 5 })
190  }
191}
192
193interface ArrObject {
194  style: number;
195  itemStyles: number[];
196}
197```
198![ListItemGroupStyle](figures/listItemGroup2.jpeg)
199