• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# ListItemGroup
2
3该组件用来展示列表item分组,宽度默认充满[List](ts-container-list.md)组件,必须配合List组件来使用。
4
5> **说明:**
6>
7> 该组件从API Version 9开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
8
9## 使用说明
10
11当ListItemGroup的父组件List的listDirection属性为Axis.Vertical时,不允许设置ListItemGroup组件的height属性。ListItemGroup的高度为header高度、footer高度和所有ListItem布局后总高度之和。当父组件List的listDirection属性为Axis.Horizontal时,不允许设置ListItemGroup组件的width属性。ListItemGroup的宽度为header宽度、footer宽度和所有ListItem布局后总宽度之和。
12
13当前ListItemGroup内部的ListItem组件不支持编辑、拖拽功能,即ListItem组件的editable属性不生效。
14
15## 子组件
16
17包含[ListItem](ts-container-listitem.md)子组件。
18
19
20## 接口
21
22ListItemGroup(options?: ListItemGroupOptions)
23
24**参数:**
25
26| 参数名 | 参数类型 | 必填 | 参数描述 |
27| -------- | -------- | -------- | -------- |
28| options |  [ListItemGroupOptions](#listitemgroupoptions对象说明)| 是 | 列表item分组组件参数。 |
29
30## ListItemGroupOptions对象说明:
31
32| 参数名              | 参数类型                                            | 必填 | 参数描述                                                     |
33| ------------------- | --------------------------------------------------- | ---- | ------------------------------------------------------------ |
34| header              | [CustomBuilder](ts-types.md#custombuilder8)         | 否   | 设置ListItemGroup头部组件。<br/>**说明:**<br/>只能放单个子组件。                                  |
35| footer              | [CustomBuilder](ts-types.md#custombuilder8)         | 否   | 设置ListItemGroup尾部组件。<br/>**说明:**<br/>只能放单个子组件。                                  |
36| space               | number&nbsp;\|&nbsp;string                          | 否   | 列表项间距。只作用于ListItem与ListItem之间,不作用于header与ListItem、footer与ListItem之间。 |
37| style<sup>10+</sup> | [ListItemGroupStyle](#listitemgroupstyle10枚举说明) | 否   | 设置List组件卡片样式。<br/>默认值: ListItemGroupStyle.NONE<br/>设置为ListItemGroupStyle.NONE时无样式。<br/>设置为ListItemGroupStyle.CARD时,必须配合[ListItem](ts-container-listitem.md)的ListItemStyle.CARD同时使用,显示默认卡片样式。 <br/>卡片样式下, 为卡片内的列表选项提供了默认的focus、hover、press、selected和disable样式。<br/>**说明:**<br/>当前卡片模式下,不支持listDirection属性设置,使用默认Axis.Vertical排列方向。<br/>当前卡片模式下,List属性alignListItem默认为ListItemAlign.Center,居中对齐显示。<br/>当前卡片模式下,ListItemGroup不支持设置头部组件header和尾部组件footer。<br/>若仅设置ListItemGroupStyle.CARD,未设置ListItemStyle.CARD时,只显示部分卡片样式及功能。 |
38
39## 属性
40
41| 名称 | 参数类型 |  描述 |
42| -------- | -------- | -------- |
43| divider | {<br/>strokeWidth:&nbsp;[Length](ts-types.md#length),<br/>color?:&nbsp;[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/>startMargin:&nbsp;分割线距离列表侧边起始端的距离。<br/>endMargin:&nbsp;分割线距离列表侧边结束端的距离。<br/>strokeWidth, startMargin和endMargin不支持设置百分比。 |
44
45## ListItemGroupStyle<sup>10+</sup>枚举说明
46
47| 名称 | 枚举值  | 描述               |
48| ---- | ---- | ------------------ |
49| NONE | 0 | 无样式。           |
50| CARD | 1 | 显示默认卡片样式。 |
51
52> **说明:**
53>
54> ListItemGroup组件不支持设置[通用属性aspectRatio](ts-universal-attributes-layout-constraints.md)。
55>
56> ListItemGroup组件如果主轴方向是垂直方向时,设置[通用属性height](ts-universal-attributes-size.md)属性不生效。
57>
58> ListItemGroup组件如果主轴方向是水平方向时,设置[通用属性width](ts-universal-attributes-size.md)属性不生效。
59
60
61## 示例
62
63```ts
64// xxx.ets
65@Entry
66@Component
67struct ListItemGroupExample {
68  private timeTable: TimeTable[] = [
69    {
70      title: '星期一',
71      projects: ['语文', '数学', '英语']
72    },
73    {
74      title: '星期二',
75      projects: ['物理', '化学', '生物']
76    },
77    {
78      title: '星期三',
79      projects: ['历史', '地理', '政治']
80    },
81    {
82      title: '星期四',
83      projects: ['美术', '音乐', '体育']
84    }
85  ]
86
87  @Builder
88  itemHead(text: string) {
89    Text(text)
90      .fontSize(20)
91      .backgroundColor(0xAABBCC)
92      .width("100%")
93      .padding(10)
94  }
95
96  @Builder
97  itemFoot(num: number) {
98    Text('共' + num + "节课")
99      .fontSize(16)
100      .backgroundColor(0xAABBCC)
101      .width("100%")
102      .padding(5)
103  }
104
105  build() {
106    Column() {
107      List({ space: 20 }) {
108        ForEach(this.timeTable, (item: TimeTable) => {
109          ListItemGroup({ header: this.itemHead(item.title), footer: this.itemFoot(item.projects.length) }) {
110            ForEach(item.projects, (project: string) => {
111              ListItem() {
112                Text(project)
113                  .width("100%")
114                  .height(100)
115                  .fontSize(20)
116                  .textAlign(TextAlign.Center)
117                  .backgroundColor(0xFFFFFF)
118              }
119            }, (item: string) => item)
120          }
121          .divider({ strokeWidth: 1, color: Color.Blue }) // 每行之间的分界线
122        })
123      }
124      .width('90%')
125      .sticky(StickyStyle.Header | StickyStyle.Footer)
126      .scrollBar(BarState.Off)
127    }.width('100%').height('100%').backgroundColor(0xDCDCDC).padding({ top: 5 })
128  }
129}
130
131interface TimeTable {
132  title: string;
133  projects: string[];
134}
135```
136
137![zh-cn_image_0000001219864159](figures/zh-cn_image_listitemgroup.gif)
138
139- 示例2
140
141```ts
142// xxx.ets
143@Entry
144@Component
145struct ListItemGroupExample2 {
146  private arr: ArrObject[] = [
147    {
148      style: ListItemGroupStyle.CARD,
149      itemStyles: [ListItemStyle.CARD, ListItemStyle.CARD, ListItemStyle.CARD]
150    },
151    {
152      style: ListItemGroupStyle.CARD,
153      itemStyles: [ListItemStyle.CARD, ListItemStyle.CARD, ListItemStyle.NONE]
154    },
155    {
156      style: ListItemGroupStyle.CARD,
157      itemStyles: [ListItemStyle.CARD, ListItemStyle.NONE, ListItemStyle.CARD]
158    },
159    {
160      style: ListItemGroupStyle.NONE,
161      itemStyles: [ListItemStyle.CARD, ListItemStyle.CARD, ListItemStyle.NONE]
162    }
163  ]
164
165  build() {
166    Column() {
167      List({ space: "4vp", initialIndex: 0 }) {
168        ForEach(this.arr, (item: ArrObject, index?: number) => {
169          ListItemGroup({ style: item.style }) {
170            ForEach(item.itemStyles, (itemStyle: number, itemIndex?: number) => {
171              ListItem({ style: itemStyle }) {
172                if (index != undefined && itemIndex != undefined) {
173                  Text("第" + (index + 1) + "个Group中第" + (itemIndex + 1) + "个item")
174                    .width("100%")
175                    .textAlign(TextAlign.Center)
176                }
177              }
178            }, (item: string) => item)
179          }
180        })
181      }
182      .width('100%')
183      .multiSelectable(true)
184      .backgroundColor(0xDCDCDC)
185    }
186    .width('100%')
187    .padding({ top: 5 })
188  }
189}
190
191interface ArrObject {
192  style: number;
193  itemStyles: number[];
194}
195```
196![ListItemGroupStyle](figures/listItemGroup2.jpeg)