• 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?: {header?: CustomBuilder, footer?: CustomBuilder, space?: number | string, style?: ListItemGroupStyle})
23
24**参数:**
25
26| 参数名              | 参数类型                                            | 必填 | 参数描述                                                     |
27| ------------------- | --------------------------------------------------- | ---- | ------------------------------------------------------------ |
28| header              | [CustomBuilder](ts-types.md#custombuilder8)         | 否   | 设置ListItemGroup头部组件。                                  |
29| footer              | [CustomBuilder](ts-types.md#custombuilder8)         | 否   | 设置ListItemGroup尾部组件。                                  |
30| space               | number \| string                          | 否   | 列表项间距。只作用于ListItem与ListItem之间,不作用于header与ListItem、footer与ListItem之间。 |
31| style<sup>10+</sup> | [ListItemGroupStyle](#listitemgroupstyle10枚举说明) | 否   | 设置List组件卡片样式。<br/>默认值: ListItemGroupStyle.NONE<br/>设置为ListItemGroupStyle.NONE时无样式。<br/>设置为ListItemStyle.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时,只显示部分卡片样式及功能。 |
32
33## 属性
34
35| 名称 | 参数类型 |  描述 |
36| -------- | -------- | -------- |
37| 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;分割线距离列表侧边结束端的距离。 |
38
39## ListItemGroupStyle<sup>10+</sup>枚举说明
40
41| 名称 | 描述               |
42| ---- | ------------------ |
43| NONE | 无样式。           |
44| CARD | 显示默认卡片样式。 |
45
46> **说明:**
47>
48> ListItemGroup组件不支持设置[通用属性aspectRatio](ts-universal-attributes-layout-constraints.md)。
49>
50> ListItemGroup组件如果主轴方向是垂直方向时,设置[通用属性height](ts-universal-attributes-size.md)属性不生效。
51>
52> ListItemGroup组件如果主轴方向是水平方向时,设置[通用属性width](ts-universal-attributes-size.md)属性不生效。
53
54
55## 示例
56
57```ts
58// xxx.ets
59@Entry
60@Component
61struct ListItemGroupExample {
62  private timetable: TimeTable[] = [
63    {
64      title: '星期一',
65      projects: ['语文', '数学', '英语']
66    },
67    {
68      title: '星期二',
69      projects: ['物理', '化学', '生物']
70    },
71    {
72      title: '星期三',
73      projects: ['历史', '地理', '政治']
74    },
75    {
76      title: '星期四',
77      projects: ['美术', '音乐', '体育']
78    }
79  ]
80
81  @Builder
82  itemHead(text: string) {
83    Text(text)
84      .fontSize(20)
85      .backgroundColor(0xAABBCC)
86      .width("100%")
87      .padding(10)
88  }
89
90  @Builder
91  itemFoot(num: number) {
92    Text('共' + num + "节课")
93      .fontSize(16)
94      .backgroundColor(0xAABBCC)
95      .width("100%")
96      .padding(5)
97  }
98
99  build() {
100    Column() {
101      List({ space: 20 }) {
102        ForEach(this.timetable, (item: TimeTable) => {
103          ListItemGroup({ header: this.itemHead(item.title), footer: this.itemFoot(item.projects.length) }) {
104            ForEach(item.projects, (project: string) => {
105              ListItem() {
106                Text(project)
107                  .width("100%")
108                  .height(100)
109                  .fontSize(20)
110                  .textAlign(TextAlign.Center)
111                  .backgroundColor(0xFFFFFF)
112              }
113            }, (item: string) => item)
114          }
115          .divider({ strokeWidth: 1, color: Color.Blue }) // 每行之间的分界线
116        })
117      }
118      .width('90%')
119      .sticky(StickyStyle.Header | StickyStyle.Footer)
120      .scrollBar(BarState.Off)
121    }.width('100%').height('100%').backgroundColor(0xDCDCDC).padding({ top: 5 })
122  }
123}
124
125interface TimeTable {
126  title: string;
127  projects: string[];
128}
129```
130
131![zh-cn_image_0000001219864159](figures/zh-cn_image_listitemgroup.gif)
132
133- 示例2
134
135```ts
136// xxx.ets
137@Entry
138@Component
139struct ListItemGroupExample2 {
140  private arr: ArrObject[] = [
141    {
142      style: ListItemGroupStyle.CARD,
143      itemStyles: [ListItemStyle.CARD, ListItemStyle.CARD, ListItemStyle.CARD]
144    },
145    {
146      style: ListItemGroupStyle.CARD,
147      itemStyles: [ListItemStyle.CARD, ListItemStyle.CARD, ListItemStyle.NONE]
148    },
149    {
150      style: ListItemGroupStyle.CARD,
151      itemStyles: [ListItemStyle.CARD, ListItemStyle.NONE, ListItemStyle.CARD]
152    },
153    {
154      style: ListItemGroupStyle.NONE,
155      itemStyles: [ListItemStyle.CARD, ListItemStyle.CARD, ListItemStyle.NONE]
156    }
157  ]
158
159  build() {
160    Column() {
161      List({ space: "4vp", initialIndex: 0 }) {
162        ForEach(this.arr, (item: ArrObject, index?: number) => {
163          ListItemGroup({ style: item.style }) {
164            ForEach(item.itemStyles, (itemStyle: number, itemIndex?: number) => {
165              ListItem({ style: itemStyle }) {
166                if (index != undefined && itemIndex != undefined) {
167                  Text("第" + (index + 1) + "个Group中第" + (itemIndex + 1) + "个item")
168                    .width("100%")
169                    .textAlign(TextAlign.Center)
170                }
171              }
172            }, (item: string) => item)
173          }
174        })
175      }
176      .width('100%')
177      .multiSelectable(true)
178      .backgroundColor(0xDCDCDC) // 浅蓝色的List
179    }
180    .width('100%')
181    .padding({ top: 5 })
182  }
183}
184
185interface ArrObject {
186  style: number;
187  itemStyles: number[];
188}
189```
190![ListItemGroupStyle](figures/listItemGroup2.jpeg)