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