• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# ListItem
2
3The **ListItem** component displays specific items in the list. It must be used together with **List**.
4
5> **NOTE**
6>
7> - This component is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
8> - The parent of this component can only be [List](ts-container-list.md) or [ListItemGroup](ts-container-listitemgroup.md).
9> - When this component is used with **LazyForEach**, its child components are created when it is created. When this component is used with **if/else** or **ForEach**, or when the parent component is **List** or **ListItemGroup**, its child components are created when it is laid out.
10
11## Child Components
12
13This component can contain a single child component.
14
15## APIs
16
17### ListItem<sup>10+</sup>
18
19ListItem(value?: ListItemOptions)
20
21**Widget capability**: This API can be used in ArkTS widgets since API version 10.
22
23**Atomic service API**: This API can be used in atomic services since API version 11.
24
25**System capability**: SystemCapability.ArkUI.ArkUI.Full
26
27**Parameters**
28
29| Name| Type                                     | Mandatory| Description                                                    |
30| ------ | --------------------------------------------- | ---- | ------------------------------------------------------------ |
31| value  | [ListItemOptions](#listitemoptions10) | No  | Value of the list item, containing the **style** parameter of the **ListItemStyle** enum type.|
32
33### ListItem<sup>(deprecated)</sup>
34
35ListItem(value?: string)
36
37This API is deprecated since API version 10. You are advised to use [ListItem<sup>10+</sup>](#listitem10) instead.
38
39**Widget capability**: Since API version 9, this API is supported in ArkTS widgets.
40
41**System capability**: SystemCapability.ArkUI.ArkUI.Full
42
43**Parameters**
44
45| Name| Type                     | Mandatory| Description|
46| ------ | ----------------------------- | ---- | -------- |
47| value  | string | No  | N/A      |
48
49## Attributes
50
51In addition to the [universal attributes](ts-component-general-attributes.md), the following attributes are supported.
52
53### sticky<sup>(deprecated)</sup>
54
55sticky(value: Sticky)
56
57Sets the sticky effect of the list item.
58
59This attribute is deprecated since API version 9. You are advised to use [the sticky attribute of the List component](ts-container-list.md#sticky9) instead.
60
61**System capability**: SystemCapability.ArkUI.ArkUI.Full
62
63**Parameters**
64
65| Name| Type                               | Mandatory| Description                                      |
66| ------ | ----------------------------------- | ---- | ------------------------------------------ |
67| value  | [Sticky](#stickydeprecated) | Yes  | Sticky effect of the list item.<br>Default value: **Sticky.None**|
68
69### editable<sup>(deprecated)</sup>
70
71editable(value: boolean | EditMode)
72
73Sets whether to enable edit mode, where the list item can be deleted or moved.
74
75This API is deprecated since API version 9. There is no substitute API.
76
77**System capability**: SystemCapability.ArkUI.ArkUI.Full
78
79**Parameters**
80
81| Name| Type                                                        | Mandatory| Description                                      |
82| ------ | ------------------------------------------------------------ | ---- | ------------------------------------------ |
83| value  | boolean \| [EditMode](#editmodedeprecated) | Yes  | Whether to enable edit mode.<br>Default value: **false**|
84
85### selectable<sup>8+</sup>
86
87selectable(value: boolean)
88
89Sets whether the list item is selectable for multiselect. This attribute takes effect only when mouse frame selection is enabled for the parent **List** container.
90
91**Widget capability**: Since API version 9, this API is supported in ArkTS widgets.
92
93**Atomic service API**: This API can be used in atomic services since API version 11.
94
95**System capability**: SystemCapability.ArkUI.ArkUI.Full
96
97**Parameters**
98
99| Name| Type   | Mandatory| Description                                             |
100| ------ | ------- | ---- | ------------------------------------------------- |
101| value  | boolean | Yes  | Whether the list item is selectable for multiselect.<br>Default value: **true**|
102
103### selected<sup>10+</sup>
104
105selected(value: boolean)
106
107Sets whether the list item is selected. This attribute supports two-way binding through [$$](../../../quick-start/arkts-two-way-sync.md). This attribute must be used before the [style for the selected state](./ts-universal-attributes-polymorphic-style.md) is set. Otherwise, the style settings will not take effect.
108
109**Widget capability**: This API can be used in ArkTS widgets since API version 10.
110
111**Atomic service API**: This API can be used in atomic services since API version 11.
112
113**System capability**: SystemCapability.ArkUI.ArkUI.Full
114
115**Parameters**
116
117| Name| Type   | Mandatory| Description                                    |
118| ------ | ------- | ---- | ---------------------------------------- |
119| value  | boolean | Yes  | Whether the list item is selected.<br>Default value: **false**|
120
121### swipeAction<sup>9+</sup>
122
123swipeAction(value: SwipeActionOptions)
124
125Sets the swipe action item displayed when the list item is swiped out from the screen edge.
126
127**Atomic service API**: This API can be used in atomic services since API version 11.
128
129**System capability**: SystemCapability.ArkUI.ArkUI.Full
130
131**Parameters**
132
133| Name| Type                                             | Mandatory| Description                |
134| ------ | ------------------------------------------------- | ---- | -------------------- |
135| value  | [SwipeActionOptions](#swipeactionoptions9) | Yes  | Swipe action item displayed when the list item is swiped out from the screen edge.|
136
137## Sticky<sup>(deprecated)</sup>
138This API is deprecated since API version 9. You are advised to use [the stickyStyle enum of the List component](ts-container-list.md#stickystyle9) instead.
139
140**System capability**: SystemCapability.ArkUI.ArkUI.Full
141
142| Name| Value| Description|
143| -------- | -------- | -------- |
144| None |  0  | The list item is not sticky.|
145| Normal |  1  | The list item is sticky with no special effects.|
146| Opacity |  2  | The list item is sticky with opacity changes.|
147
148## EditMode<sup>(deprecated)</sup>
149This API is deprecated since API version 9. There is no substitute API.
150
151**System capability**: SystemCapability.ArkUI.ArkUI.Full
152
153| Name    | Value| Description     |
154| ------ | ------ | --------- |
155| None   |  0  | The editing operation is not restricted.   |
156| Deletable |  1  | The list item can be deleted.|
157| Movable |  2  | The list item can be moved.|
158
159## SwipeEdgeEffect<sup>9+</sup>
160
161**Atomic service API**: This API can be used in atomic services since API version 11.
162
163**System capability**: SystemCapability.ArkUI.ArkUI.Full
164
165| Name    | Value| Description     |
166| ------ | ------ | --------- |
167|   Spring   |    0    | When the list item scrolls to the edge of the list, it can continue to scroll for a distance.<br>If the delete area is set, the list item can continue to scroll after the scroll distance exceeds the delete threshold and,<br>after being released, rebound following the spring curve.|
168|   None   |    1    | The list item cannot scroll beyond the edge of the list.<br>If the delete area is set, the list item cannot continue to scroll after the scroll distance exceeds the delete threshold.<br>If the delete callback is set, it is triggered when the delete threshold is reached and the list item is released.|
169
170## SwipeActionOptions<sup>9+</sup>
171
172The top level of the @builder function corresponding to **start** and **end** must be a single component and cannot be an **if/else**, **ForEach**, or **LazyForEach** statement.
173
174The swipe gesture works only in the list item area. If a swipe causes a child component to extend beyond the list item area, the portion outside the area does not respond to the swipe. In light of this, avoid setting **swipeAction** to a component too wide in a multi-column list.
175
176**System capability**: SystemCapability.ArkUI.ArkUI.Full
177
178| Name                        | Type                                                        | Mandatory| Description                                                        |
179| ---------------------------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
180| start                        | [CustomBuilder](ts-types.md#custombuilder8) \| [SwipeActionItem](#swipeactionitem10) | No  | Swipe action item displayed on the left of the list item when the item is swiped right (in vertical list layout) or above the list item when the item is swiped down (in horizontal list layout).<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
181| end                          | [CustomBuilder](ts-types.md#custombuilder8) \| [SwipeActionItem](#swipeactionitem10) | No  | Swipe action item displayed on the right of the list item when the item is swiped left (in vertical list layout) or below the list item when the item is swiped up (in horizontal list layout).<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
182| edgeEffect                   | [SwipeEdgeEffect](#swipeedgeeffect9)                 | No  | Scroll effect.<br>**Atomic service API**: This API can be used in atomic services since API version 11.                                               |
183| onOffsetChange<sup>11+</sup> | (offset: number) => void                                     | No  | Callback invoked when the scroll offset changes.<br>**NOTE**<br> Specifically, this callback is invoked when the location of the list item changes, in vp, when it is swiped left or right (in vertical list layout) or up or down (in horizontal list layout).<br>**Atomic service API**: This API can be used in atomic services since API version 12.|
184
185## SwipeActionItem<sup>10+</sup>
186
187Describes the swipe action item.<br>For a list in vertical layout, it refers to the delete option displayed on the left (or right) of the list item when the list item is swiped right (or left).
188<br>For a list in horizontal layout, it refers to the delete option displayed below (or above) the list item when the list item is swiped up (or down).
189
190**System capability**: SystemCapability.ArkUI.ArkUI.Full
191
192| Name                | Type                                                    | Mandatory| Description                                                        |
193| -------------------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
194| actionAreaDistance | [Length](ts-types.md#length) | No| Swipe distance threshold for deleting the list item.<br>Default value: **56vp**<br>**NOTE**<br>This parameter cannot be set in percentage.<br>If the value is greater than the list item width minus the width of **swipeAction**, or is less than or equal to 0, the delete area will not be set.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
195| onAction | () => void | No| Callback invoked when the list item is released while in the delete area.<br>**NOTE**<br> This callback is invoked only when the list item is released in a position that meets or goes beyond the specified swipe distance threshold (which must be valid) for deleting the list item.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
196| onEnterActionArea | () => void | No| Callback invoked each time the list item enters the delete area.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
197| onExitActionArea | () => void | No|Callback invoked each time the list item exits the delete area.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
198| builder |  [CustomBuilder](ts-types.md#custombuilder8) | No|Swipe action item displayed when the list item is swiped left or right (in vertical list layout) or up or down (in horizontal list layout).<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
199| builderComponent<sup>18+</sup> |  [ComponentContent](../js-apis-arkui-ComponentContent.md) | No|Swipe action item displayed when the list item is swiped left or right (in vertical list layout) or up or down (in horizontal list layout).<br>**NOTE**<br>This parameter takes precedence over the **builder** parameter. This means that, if both **builder** and **builderComponent** are set, the value of **builderComponent** is used.<br> To avoid display issues, do not assign the same **builderComponent** to different swipe action items specified by **start** and **end**.<br>**Atomic service API**: This API can be used in atomic services since API version 18.|
200| onStateChange<sup>11+</sup> | (state:[SwipeActionState](#swipeactionstate11)) => void | No|Callback invoked when the swipe state of the list item changes.<br>**Atomic service API**: This API can be used in atomic services since API version 12.|
201## ListItemOptions<sup>10+</sup>
202
203**Atomic service API**: This API can be used in atomic services since API version 11.
204
205**System capability**: SystemCapability.ArkUI.ArkUI.Full
206
207| Name | Type                                 | Mandatory| Description                                                        |
208| ----- | ----------------------------------------- | ---- | ------------------------------------------------------------ |
209| style | [ListItemStyle](#listitemstyle10) | No  | Style of the list item.<br>Default value: **ListItemStyle.NONE**<br>If this parameter is set to **ListItemStyle.NONE**, no style is applied.<br>When **ListItemStyle.CARD** is used, you are advised to pair it with **ListItemGroupStyle.CARD** from [ListItemGroup](ts-container-listitemgroup.md) to apply the default card style.<br>In the card style, the default specifications for a list item are as follows: a height of 48 vp, a width of 100%, and horizontal padding of 8 vp on both the left and right sides. If you want to implement an adaptive height for the list item, you can set the **height** attribute to **undefined**.<br>It can be in focus, hover, press, selected, or disable style depending on the state.<br>**NOTE**<br>In the card style, by default, the list runs along the vertical axis, that is, **listDirection** is at **Axis.Vertical**. If **listDirection** is set to **Axis.Horizontal**, a disordered display may result. The **alignListItem** attribute of the list is set to **ListItemAlign.Center** by default, which aligns the list items in the center.|
210
211## ListItemStyle<sup>10+</sup>
212
213**Atomic service API**: This API can be used in atomic services since API version 11.
214
215**System capability**: SystemCapability.ArkUI.ArkUI.Full
216
217| Name| Value | Description            |
218| ---- | ---- | ------------------ |
219| NONE | 0 | No style.          |
220| CARD | 1 | Default card style.|
221
222## SwipeActionState<sup>11+</sup>
223
224**Atomic service API**: This API can be used in atomic services since API version 12.
225
226**System capability**: SystemCapability.ArkUI.ArkUI.Full
227
228| Name     | Value    | Description                                                      |
229| --------- | --------- | ------------------------------------------------------------ |
230| COLLAPSED | 0 | Collapsed state.<br>When the list item is swiped left or right (in vertical list layout) or up or down (in horizontal list layout), the swipe action item is hidden.|
231| EXPANDED  | 1 | Expanded state.<br>When the list item is swiped left or right (in vertical list layout) or up or down (in horizontal list layout), the swipe action item is shown.<br>**NOTE**<br>When the list item is swiped left or right (in vertical list layout)<br>or up or down (in horizontal list layout), the swipe action item is shown.|
232| ACTIONING | 2 | In-action state. The list item is in this state when it enters the delete area.<br>**NOTE**<br>A list item can enter this state only when it is released in a position that meets or goes beyond the specified swipe distance threshold (which must be valid) for deleting the list item.|
233
234## Events
235
236### onSelect<sup>8+</sup>
237
238onSelect(event: (isSelected: boolean) =&gt; void)
239
240Triggered when the selected state of the list item for multiselect changes.
241
242**Widget capability**: This API can be used in ArkTS widgets since API version 9.
243
244**Atomic service API**: This API can be used in atomic services since API version 11.
245
246**System capability**: SystemCapability.ArkUI.ArkUI.Full
247
248**Parameters**
249
250| Name    | Type   | Mandatory| Description                                                        |
251| ---------- | ------- | ---- | ------------------------------------------------------------ |
252| isSelected | boolean | Yes  | Returns **true** if the list item is selected for multselect; returns **false** otherwise.|
253
254## Example
255
256### Example 1: Creating a List Item
257This example demonstrates the basic usage of creating a list item.
258
259```ts
260// xxx.ets
261@Entry
262@Component
263struct ListItemExample {
264  private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
265
266  build() {
267    Column() {
268      List({ space: 20, initialIndex: 0 }) {
269        ForEach(this.arr, (item: number) => {
270          ListItem() {
271            Text('' + item)
272              .width('100%')
273              .height(100)
274              .fontSize(16)
275              .textAlign(TextAlign.Center)
276              .borderRadius(10)
277              .backgroundColor(0xFFFFFF)
278          }
279        }, (item: string) => item)
280      }.width('90%')
281      .scrollBar(BarState.Off)
282    }.width('100%').height('100%').backgroundColor(0xDCDCDC).padding({ top: 5 })
283  }
284}
285```
286
287![en-us_image_0000001219864159](figures/en-us_image_0000001219864159.gif)
288
289### Example 2: Setting the Swipe Action Item
290This example shows how to set the swipe action item for a list item using **swipeAction**.
291
292```ts
293// xxx.ets
294@Entry
295@Component
296struct ListItemExample2 {
297  @State arr: number[] = [0, 1, 2, 3, 4]
298  @State enterEndDeleteAreaString: string = "not enterEndDeleteArea"
299  @State exitEndDeleteAreaString: string = "not exitEndDeleteArea"
300  private scroller: ListScroller = new ListScroller()
301
302  @Builder itemEnd() {
303    Row() {
304      Button("Delete").margin("4vp")
305      Button("Set").margin("4vp").onClick(() => {
306        this.scroller.closeAllSwipeActions()
307      })
308    }.padding("4vp").justifyContent(FlexAlign.SpaceEvenly)
309  }
310
311  build() {
312    Column() {
313      List({ space: 10, scroller: this.scroller }) {
314        ForEach(this.arr, (item: number) => {
315          ListItem() {
316            Text("item" + item)
317              .width('100%')
318              .height(100)
319              .fontSize(16)
320              .textAlign(TextAlign.Center)
321              .borderRadius(10)
322              .backgroundColor(0xFFFFFF)
323          }
324          .transition({ type: TransitionType.Delete, opacity: 0 })
325          .swipeAction({
326            end: {
327              builder: () => { this.itemEnd() },
328              onAction: () => {
329                animateTo({ duration: 1000 }, () => {
330                  let index = this.arr.indexOf(item)
331                  this.arr.splice(index, 1)
332                })
333              },
334              actionAreaDistance: 56,
335              onEnterActionArea: () => {
336                this.enterEndDeleteAreaString = "enterEndDeleteArea"
337                this.exitEndDeleteAreaString = "not exitEndDeleteArea"
338              },
339              onExitActionArea: () => {
340                this.enterEndDeleteAreaString = "not enterEndDeleteArea"
341                this.exitEndDeleteAreaString = "exitEndDeleteArea"
342              }
343            }
344          })
345        }, (item: string) => item)
346      }
347      Text(this.enterEndDeleteAreaString).fontSize(20)
348      Text(this.exitEndDeleteAreaString).fontSize(20)
349    }
350    .padding(10)
351    .backgroundColor(0xDCDCDC)
352    .width('100%')
353    .height('100%')
354  }
355}
356```
357![deleteListItem](figures/deleteListItem.gif)
358
359### Example 3: Applying a Card-style Effect
360This example illustrates the card-style effect of the **ListItem** component.
361
362```ts
363// xxx.ets
364@Entry
365@Component
366struct ListItemExample3 {
367  build() {
368    Column() {
369      List({ space: "4vp", initialIndex: 0 }) {
370        ListItemGroup({ style: ListItemGroupStyle.CARD }) {
371          ForEach([ListItemStyle.CARD, ListItemStyle.CARD, ListItemStyle.NONE], (itemStyle: number, index?: number) => {
372            ListItem({ style: itemStyle }) {
373              Text("" + index)
374                .width("100%")
375                .textAlign(TextAlign.Center)
376            }
377          })
378        }
379        ForEach([ListItemStyle.CARD, ListItemStyle.CARD, ListItemStyle.NONE], (itemStyle: number, index?: number) => {
380          ListItem({ style: itemStyle }) {
381            Text("" + index)
382              .width("100%")
383              .textAlign(TextAlign.Center)
384          }
385        })
386      }
387      .width('100%')
388      .multiSelectable(true)
389      .backgroundColor(0xDCDCDC)
390    }
391    .width('100%')
392    .padding({ top: 5 })
393  }
394}
395```
396![ListItemStyle](figures/listItem3.jpeg)
397
398### Example 4: Setting the Swipe Action Item Using ComponentContent
399
400This example demonstrates how to set swipe action items for list items using **ComponentContent**.
401
402```ts
403// xxx.ets
404import { ComponentContent } from '@kit.ArkUI';
405
406class BuilderParams {
407  text: string | Resource;
408  scroller: ListScroller
409  constructor(text: string | Resource, scroller: ListScroller) {
410    this.text = text;
411    this.scroller = scroller;
412  }
413}
414
415@Builder
416function itemBuilder(params: BuilderParams) {
417  Row() {
418    Button(params.text).margin("4vp")
419    Button("Set").margin("4vp").onClick(() => {
420      params.scroller.closeAllSwipeActions()
421    })
422  }.padding("4vp").justifyContent(FlexAlign.SpaceEvenly)
423}
424@Component
425struct MyListItem {
426  scroller: ListScroller = new ListScroller()
427  @State arr: number[] = [0, 1, 2, 3, 4]
428  @State project ?: number = 0
429  startBuilder ?: ComponentContent<BuilderParams> = undefined
430  endBuilder ?: ComponentContent<BuilderParams> = undefined
431
432  builderParam = new BuilderParams("delete", this.scroller)
433
434  aboutToAppear(): void {
435    this.startBuilder = new ComponentContent(this.getUIContext(), wrapBuilder(itemBuilder), this.builderParam)
436    this.endBuilder = new ComponentContent(this.getUIContext(), wrapBuilder(itemBuilder), this.builderParam)
437  }
438  GetStartBuilder() {
439    this.startBuilder?.update(new BuilderParams("StartDelete", this.scroller));
440    return this.startBuilder;
441  }
442  GetEndBuilder() {
443    this.endBuilder?.update(new BuilderParams("EndDelete", this.scroller));
444    return this.endBuilder;
445  }
446  build() {
447    ListItem() {
448      Text("item" + this.project)
449        .width('100%')
450        .height(100)
451        .fontSize(16)
452        .textAlign(TextAlign.Center)
453        .borderRadius(10)
454        .backgroundColor(0xFFFFFF)
455    }
456    .transition({ type: TransitionType.Delete, opacity: 0 })
457    .swipeAction({
458      end: {
459        builderComponent: this.GetEndBuilder(),
460        onAction: () => {
461          animateTo({ duration: 1000 }, () => {
462            let index = this.arr.indexOf(this.project)
463            this.arr.splice(index, 1)
464          })
465        },
466        actionAreaDistance: 56
467      },
468      start: {
469        builderComponent: this.GetStartBuilder(),
470        onAction: () => {
471          animateTo({ duration: 1000 }, () => {
472            let index = this.arr.indexOf(this.project)
473            this.arr.splice(index, 1)
474          })
475        },
476        actionAreaDistance: 56
477      }
478    })
479    .padding(5)
480  }
481}
482
483@Entry
484@Component
485struct ListItemExample {
486  @State arr: number[] = [0, 1, 2, 3, 4]
487  private scroller: ListScroller = new ListScroller()
488
489  build() {
490    Column() {
491      List({ space: 10, scroller: this.scroller }) {
492        ListItemGroup() {
493          ForEach(this.arr, (project: number) => {
494            MyListItem({ scroller: this.scroller, project: project, arr:this.arr })
495          }, (item: string) => item)
496        }
497      }
498    }
499    .padding(10)
500    .backgroundColor(0xDCDCDC)
501    .width('100%')
502    .height('100%')
503  }
504}
505```
506![ListItemStyle](figures/deleteListItem_example04.gif)
507