1# ListItem 2 3用来展示列表具体item,必须配合List来使用。 4 5> **说明:** 6> 7> 该组件从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 8 9 10## 子组件 11 12可以包含单个子组件。 13 14## 接口 15 16从API version 9开始,该接口支持在ArkTS卡片中使用。 17 18### ListItem<sup>10+</sup> 19 20ListItem(value?: ListItemOptions) 21 22**参数:** 23 24| 参数名 | 参数类型 | 必填 | 参数描述 | 25| ------ | --------------------------------------------- | ---- | ------------------------------------------------------------ | 26| value | [ListItemOptions](#listitemoptions10对象说明) | 否 | 为ListItem提供可选参数, 该对象内含有ListItemStyle枚举类型的style参数。 | 27 28### ListItem<sup>(deprecated)</sup> 29 30ListItem(value?: string) 31 32从API version 10开始, 该接口不再维护,推荐使用[ListItem<sup>10+</sup>](#listitem10)。 33 34**参数:** 35 36| 参数名 | 参数类型 | 必填 | 参数描述 | 37| ------ | ----------------------------- | ---- | -------- | 38| value | string | 否 | 无 | 39 40## 属性 41 42除支持[通用属性](ts-universal-attributes-size.md)外,还支持以下属性: 43 44| 名称 | 参数类型 | 描述 | 45| -------- | -------- | -------- | 46| sticky<sup>(deprecated)</sup> | [Sticky](#stickydeprecated枚举说明) | 设置ListItem吸顶效果。<br/>默认值:Sticky.None<br/>从API version9开始废弃不再使用,推荐使用[List组件sticky属性](ts-container-list.md#属性)。 | 47| editable<sup>(deprecated)</sup> | boolean \| [EditMode](#editmodedeprecated枚举说明) | 当前ListItem元素是否可编辑,进入编辑模式后可删除或移动列表项。<br/>从API version9开始废弃不再使用,无替代接口。<br/>默认值:false | 48| selectable<sup>8+</sup> | boolean | 当前ListItem元素是否可以被鼠标框选。<br/>**说明:**<br/>外层List容器的鼠标框选开启时,ListItem的框选才生效。<br/>默认值:true | 49| selected<sup>10+</sup> | boolean | 设置当前ListItem选中状态。该属性支持[$$](../../quick-start/arkts-two-way-sync.md)双向绑定变量。<br/>**说明:**<br/>该属性需要在设置[选中态样式](./ts-universal-attributes-polymorphic-style.md)前使用才能生效选中态样式。<br/>默认值:false| 50| swipeAction<sup>9+</sup> | {<br/>start?: [CustomBuilder](ts-types.md#custombuilder8) \| [SwipeActionItem](#swipeactionitem10对象说明),<br/>end?:[CustomBuilder](ts-types.md#custombuilder8) \| [SwipeActionItem](#swipeactionitem10对象说明),<br/>edgeEffect?: [SwipeEdgeEffect](#swipeedgeeffect9枚举说明),<br/>} | 用于设置ListItem的划出组件。<br/>- start: ListItem向右划动时item左边的组件(List垂直布局时)或ListItem向下划动时item上方的组件(List水平布局时)。<br/>- end: ListItem向左划动时item右边的组件(List垂直布局时)或ListItem向上划动时item下方的组件(List水平布局时)。<br/>- edgeEffect: 滑动效果。<br/>**说明:** <br/>- start和end对应的@builder函数中顶层必须是单个组件,不能是if/else、ForEach、LazyForEach语句。<br/> - 滑动手势只在listItem区域上,如果子组件划出ListItem区域外,在ListItem以外部分不会响应划动手势。所以在多列模式下,建议不要将划出组件设置太宽。 | 51 52## Sticky<sup>(deprecated)</sup>枚举说明 53从API version9开始废弃不再使用,推荐使用[List组件stickyStyle枚举](ts-container-list.md#stickystyle9枚举说明)。 54| 名称 | 枚举值 | 描述 | 55| -------- | -------- | -------- | 56| None | 0 | 无吸顶效果。 | 57| Normal | 1 | 当前item吸顶。 | 58| Opacity | 2 | 当前item吸顶显示透明度变化效果。 | 59 60## EditMode<sup>(deprecated)</sup>枚举说明 61从API version9开始废弃不再使用,无替代接口。 62| 名称 | 枚举值 | 描述 | 63| ------ | ------ | --------- | 64| None | 0 | 编辑操作不限制。 | 65| Deletable | 1 | 可删除。 | 66| Movable | 2 | 可移动。 | 67 68## SwipeEdgeEffect<sup>9+</sup>枚举说明 69| 名称 | 枚举值 | 描述 | 70| ------ | ------ | --------- | 71| Spring | 0 | ListItem划动距离超过划出组件大小后可以继续划动。<br>如果设置了删除区域,ListItem划动距离超过删除阈值后可以继续划动,<br/>松手后按照弹簧阻尼曲线回弹。 | 72| None | 1 | ListItem划动距离不能超过划出组件大小。<br>如果设置了删除区域,ListItem划动距离不能超过删除阈值,<br/>并且在设置删除回调的情况下,达到删除阈值后松手触发删除回调。 | 73 74## SwipeActionItem<sup>10+</sup>对象说明 75List垂直布局,ListItem向右滑动,item左边的长距离滑动删除选项或向左滑动时,item右边的长距离滑动删除选项。 76</br>List水平布局,ListItem向上滑动,item下边的长距离滑动删除选项或向下滑动时,item上边的长距离滑动删除选项。 77 78| 名称 | 参数类型 | 必填 | 描述 | 79| -------------------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 80| actionAreaDistance | [Length](ts-types.md#length) | 否 | 设置组件长距离滑动删除距离阈值。<br/>默认值:56vp <br/>**说明:** <br/>不支持设置百分比。<br/>删除距离阈值大于item宽度减去划出组件宽度,或删除距离阈值小于等于0就不会设置删除区域。| 81| onAction | () => void | 否 | 组件进入长距删除区后删除ListItem时调用,进入长距删除区后抬手时触发。<br/>**说明:** <br/> 滑动后松手的位置超过或等于设置的距离阈值,并且设置的距离阈值有效时才会触发。| 82| onEnterActionArea | () => void | 否 | 在滑动条目进入删除区域时调用,只触发一次,当再次进入时仍触发。 | 83| onExitActionArea | () => void | 否 |当滑动条目退出删除区域时调用,只触发一次,当再次退出时仍触发。 | 84| builder | [CustomBuilder](ts-types.md#custombuilder8) | 否 |当列表项向右或向右滑动(当列表方向为“垂直”时),向下或向下滑动(当列方向为“水平”时)时显示的操作项。 | 85## ListItemOptions<sup>10+</sup>对象说明 86 87| 名称 | 参数类型 | 必填 | 描述 | 88| ----- | ----------------------------------------- | ---- | ------------------------------------------------------------ | 89| style | [ListItemStyle](#listitemstyle10枚举说明) | 否 | 设置List组件卡片样式。<br/>默认值: ListItemStyle.NONE<br/>设置为ListItemStyle.NONE时无样式。<br/>设置为ListItemStyle.CARD时,必须配合[ListItemGroup](ts-container-listitemgroup.md)的ListItemGroupStyle.CARD同时使用,显示默认卡片样式。 <br/>卡片样式下,ListItem默认规格:高度48vp,宽度100%。<br/>卡片样式下, 为卡片内的列表选项提供了默认的focus、hover、press、selected和disable样式。<br/>**说明:**<br/>当前卡片模式下,不支持listDirection属性设置,使用默认Axis.Vertical排列方向。<br/>当前卡片模式下,List属性alignListItem默认为ListItemAlign.Center,居中对齐显示。<br/>若仅设置ListItemStyle.CARD,未设置ListItemGroupStyle.CARD时,只显示部分卡片样式及功能。 | 90 91## ListItemStyle<sup>10+</sup>枚举说明 92 93| 名称 | 枚举值 | 描述 | 94| ---- | ---- | ------------------ | 95| NONE | 0 | 无样式。 | 96| CARD | 1 | 显示默认卡片样式。 | 97 98## 事件 99 100| 名称 | 功能描述 | 101| -------- | -------- | 102| onSelect(event: (isSelected: boolean) => void)<sup>8+</sup> | ListItem元素被鼠标框选的状态改变时触发回调。<br/>isSelected:进入鼠标框选范围即被选中返回true, 移出鼠标框选范围即未被选中返回false。 | 103 104## 示例 105 106### 示例1 107 108```ts 109// xxx.ets 110@Entry 111@Component 112struct ListItemExample { 113 private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 114 115 build() { 116 Column() { 117 List({ space: 20, initialIndex: 0 }) { 118 ForEach(this.arr, (item: number) => { 119 ListItem() { 120 Text('' + item) 121 .width('100%') 122 .height(100) 123 .fontSize(16) 124 .textAlign(TextAlign.Center) 125 .borderRadius(10) 126 .backgroundColor(0xFFFFFF) 127 } 128 }, (item: string) => item) 129 }.width('90%') 130 .scrollBar(BarState.Off) 131 }.width('100%').height('100%').backgroundColor(0xDCDCDC).padding({ top: 5 }) 132 } 133} 134``` 135 136 137 138### 示例2 139 140 141```ts 142// xxx.ets 143@Entry 144@Component 145struct ListItemExample2 { 146 @State arr: number[] = [0, 1, 2, 3, 4] 147 @State enterEndDeleteAreaString: string = "not enterEndDeleteArea" 148 @State exitEndDeleteAreaString: string = "not exitEndDeleteArea" 149 150 @Builder itemEnd() { 151 Row() { 152 Button("Delete").margin("4vp") 153 Button("Set").margin("4vp") 154 }.padding("4vp").justifyContent(FlexAlign.SpaceEvenly) 155 } 156 157 build() { 158 Column() { 159 List({ space: 10 }) { 160 ForEach(this.arr, (item: number) => { 161 ListItem() { 162 Text("item" + item) 163 .width('100%') 164 .height(100) 165 .fontSize(16) 166 .textAlign(TextAlign.Center) 167 .borderRadius(10) 168 .backgroundColor(0xFFFFFF) 169 } 170 .transition({ type: TransitionType.Delete, opacity: 0 }) 171 .swipeAction({ 172 end: { 173 builder: () => { this.itemEnd() }, 174 onAction: () => { 175 animateTo({ duration: 1000 }, () => { 176 let index = this.arr.indexOf(item) 177 this.arr.splice(index, 1) 178 }) 179 }, 180 actionAreaDistance: 56, 181 onEnterActionArea: () => { 182 this.enterEndDeleteAreaString = "enterEndDeleteArea" 183 this.exitEndDeleteAreaString = "not exitEndDeleteArea" 184 }, 185 onExitActionArea: () => { 186 this.enterEndDeleteAreaString = "not enterEndDeleteArea" 187 this.exitEndDeleteAreaString = "exitEndDeleteArea" 188 } 189 } 190 }) 191 }, (item: string) => item) 192 } 193 Text(this.enterEndDeleteAreaString).fontSize(20) 194 Text(this.exitEndDeleteAreaString).fontSize(20) 195 } 196 .padding(10) 197 .backgroundColor(0xDCDCDC) 198 .width('100%') 199 .height('100%') 200 } 201} 202``` 203 204 205### 示例3 206 207```ts 208// xxx.ets 209@Entry 210@Component 211struct ListItemExample3 { 212 build() { 213 Column() { 214 List({ space: "4vp", initialIndex: 0 }) { 215 ListItemGroup({ style: ListItemGroupStyle.CARD }) { 216 ForEach([ListItemStyle.CARD, ListItemStyle.CARD, ListItemStyle.NONE], (itemStyle: number, index?: number) => { 217 ListItem({ style: itemStyle }) { 218 Text("" + index) 219 .width("100%") 220 .textAlign(TextAlign.Center) 221 } 222 }) 223 } 224 ForEach([ListItemStyle.CARD, ListItemStyle.CARD, ListItemStyle.NONE], (itemStyle: number, index?: number) => { 225 ListItem({ style: itemStyle }) { 226 Text("" + index) 227 .width("100%") 228 .textAlign(TextAlign.Center) 229 } 230 }) 231 } 232 .width('100%') 233 .multiSelectable(true) 234 .backgroundColor(0xDCDCDC) 235 } 236 .width('100%') 237 .padding({ top: 5 }) 238 } 239} 240``` 241