• 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
9
10## Child Components
11
12This component can contain a single child component.
13
14## APIs
15
16Since API version 9, this API is supported in ArkTS widgets.
17
18### ListItem<sup>10+</sup>
19
20ListItem(value?: ListItemOptions)
21
22**Parameters**
23
24| Name| Type                                     | Mandatory| Description                                                    |
25| ------ | --------------------------------------------- | ---- | ------------------------------------------------------------ |
26| value  | [ListItemOptions](#listitemoptions10) | No  | Value of the list item, containing the **style** parameter of the **ListItemStyle**enum type.|
27
28### ListItem<sup>(deprecated)</sup>
29
30ListItem(value?: string)
31
32This API is deprecated since API version 10. You are advised to use [ListItem<sup>10+</sup>](#listitem10) instead.
33
34**Parameters**
35
36| Name| Type                     | Mandatory| Description|
37| ------ | ----------------------------- | ---- | -------- |
38| value  | string | No  | N/A      |
39
40## Attributes
41
42In addition to the [universal attributes](ts-universal-attributes-size.md), the following attributes are supported.
43
44| Name| Type| Description|
45| -------- | -------- | -------- |
46| sticky<sup>(deprecated)</sup> | [Sticky](#stickydeprecated) | Sticky effect of the list item.<br>Default value: **Sticky.None**<br>This API is deprecated since API version 9. You are advised to use **sticky** of the [\<List>](ts-container-list.md#attributes) component.|
47| editable<sup>(deprecated)</sup>  | boolean \| [EditMode](#editmodedeprecated) | Whether to enter editing mode, where the list item can be deleted or moved.<br>This API is deprecated since API version 9.<br>Default value: **false**|
48| selectable<sup>8+</sup> | boolean | Whether the current list item is selectable by mouse drag.<br>**NOTE**<br>This attribute takes effect only when mouse frame selection is enabled for the parent **\<List>** container.<br>Default value: **true**|
49| selected<sup>10+</sup> | boolean | Whether the list item is selected. This attribute supports [$$](../../quick-start/arkts-two-way-sync.md) for two-way binding of variables.<br>**NOTE**<br>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.<br>Default value: **false**|
50| swipeAction<sup>9+</sup> | {<br>start?: CustomBuilder \| [SwipeActionItem](#swipeactionitem10),<br>end?:CustomBuilder \| [SwipeActionItem](#swipeactionitem10),<br>edgeEffect?: [SwipeEdgeEffect](#swipeedgeeffect9),<br>} | Swipe action displayed when the list item is swiped out from the screen edge.<br>- **start**: swipe action 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>- **end**: swipe action 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>- **edgeEffect**: scroll effect.<br>**NOTE**<br>- The 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.<br> - The 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.|
51
52## Sticky<sup>(deprecated)</sup>
53This API is deprecated since API version 9. You are advised to use [stickyStyle](ts-container-list.md#stickystyle9) of the **\<List>** component.
54| Name| Description|
55| -------- | -------- |
56| None | The list item is not sticky.|
57| Normal | The list item is sticky with no special effects.|
58| Opacity | The list item is sticky with opacity changes.|
59
60## EditMode<sup>(deprecated)</sup>
61This API is deprecated since API version 9.
62| Name    | Description       |
63| ------ | --------- |
64| None   | The editing operation is not restricted.   |
65| Deletable | The list item can be deleted.|
66| Movable | The list item can be moved.|
67
68## SwipeEdgeEffect<sup>9+</sup>
69| Name| Description|
70| -------- | -------- |
71| Spring | When the list item scrolls to the edge of the list, it can continue to scroll for a distance. If the delete area is set, the list item can continue to scroll after the scroll distance reaches the delete threshold and, after being released, rebound following the spring curve.|
72| None | The list item cannot scroll beyond the edge of the list. If the delete area is set, the list item cannot continue to scroll after the scroll distance reaches the delete threshold. If the delete callback is set, it is triggered when the delete threshold is reached and the list item is released.|
73
74## SwipeActionItem<sup>10+</sup>
75Describes the swipe action item.
76
77For a list in vertical layout, it refers to the delete item displayed on the left (or right) of the list item when the list item is swiped right (or left).
78
79For a list in horizontal layout, it refers to the delete item displayed below (or above) the list item when the list item is swiped up (or down).
80
81| Name                | Type                                                    | Mandatory| Description                                                        |
82| -------------------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
83| 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.|
84| 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.|
85| onEnterActionArea | () => void | No| Callback invoked each time the list item enters the delete area.|
86| onExitActionArea | () => void | No|Callback invoked each time the list item exits the delete area.|
87| builder |  CustomBuilder | 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).|
88## ListItemOptions<sup>10+</sup>
89
90| Name | Type                                 | Mandatory| Description                                                        |
91| ----- | ----------------------------------------- | ---- | ------------------------------------------------------------ |
92| 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>If this parameter is set to **ListItemStyle.CARD**, the default card style is applied, but only when **ListItemGroupStyle.CARD** is set for [\<ListItemGroup>](ts-container-listitemgroup.md).<br>In the default card style, the list item has a 48 vp height and 100% width. It can be in focus, hover, press, selected, or disable style depending on the state.<br>**NOTE**<br>In the default card style, the list has its **listDirection** attribute fixed at **Axis.Vertical** and **alignListItem** attribute at **ListItemAlign.Center**.|
93
94## ListItemStyle<sup>10+</sup>
95
96| Name| Description              |
97| ---- | ------------------ |
98| NONE | No style.          |
99| CARD | Default card style.|
100
101## Events
102
103| Name| Description|
104| -------- | -------- |
105| onSelect(event: (isSelected: boolean) =&gt; void)<sup>8+</sup> | Triggered when the selected state of the **\<ListItem>** changes.<br>**isSelected**: Returns **true** if the **\<ListItem>** is selected by mouse drag; returns **false** otherwise.|
106
107## Example
108
109### Example 1
110
111```ts
112// xxx.ets
113@Entry
114@Component
115struct ListItemExample {
116  private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
117
118  build() {
119    Column() {
120      List({ space: 20, initialIndex: 0 }) {
121        ForEach(this.arr, (item: number) => {
122          ListItem() {
123            Text('' + item)
124              .width('100%')
125              .height(100)
126              .fontSize(16)
127              .textAlign(TextAlign.Center)
128              .borderRadius(10)
129              .backgroundColor(0xFFFFFF)
130          }
131        }, (item: string) => item)
132      }.width('90%')
133      .scrollBar(BarState.Off)
134    }.width('100%').height('100%').backgroundColor(0xDCDCDC).padding({ top: 5 })
135  }
136}
137```
138
139![en-us_image_0000001219864159](figures/en-us_image_0000001219864159.gif)
140
141### Example 2
142
143
144```ts
145// xxx.ets
146// xxx.ets
147@Entry
148@Component
149struct ListItemExample2 {
150  @State message: string = 'Hello World'
151  @State arr: number[] = [0, 1, 2, 3, 4]
152  @State enterEndDeleteAreaString: string = "not enterEndDeleteArea"
153  @State exitEndDeleteAreaString: string = "not exitEndDeleteArea"
154
155  @Builder itemEnd() {
156    Row() {
157      Button("Delete").margin("4vp")
158      Button("Set").margin("4vp")
159    }.padding("4vp").justifyContent(FlexAlign.SpaceEvenly)
160  }
161
162  build() {
163    Column() {
164      List({ space: 10 }) {
165        ForEach(this.arr, (item: number) => {
166          ListItem() {
167            Text("item" + item)
168              .width('100%')
169              .height(100)
170              .fontSize(16)
171              .textAlign(TextAlign.Center)
172              .borderRadius(10)
173              .backgroundColor(0xFFFFFF)
174          }
175          .transition({ type: TransitionType.Delete, opacity: 0 })
176          .swipeAction({
177            end: {
178              builder: this.itemEnd,
179              onAction: () => {
180                animateTo({ duration: 1000 }, () => {
181                  let index = this.arr.indexOf(item)
182                  this.arr.splice(index, 1)
183                })
184              },
185              actionAreaDistance: 56,
186              onEnterActionArea: () => {
187                this.enterEndDeleteAreaString = "enterEndDeleteArea"
188                this.exitEndDeleteAreaString = "not exitEndDeleteArea"
189              },
190              onExitActionArea: () => {
191                this.enterEndDeleteAreaString = "not enterEndDeleteArea"
192                this.exitEndDeleteAreaString = "exitEndDeleteArea"
193              }
194            }
195          })
196        }, (item: string) => item)
197      }
198      Text(this.enterEndDeleteAreaString).fontSize(20)
199      Text(this.exitEndDeleteAreaString).fontSize(20)
200    }
201    .padding(10)
202    .backgroundColor(0xDCDCDC)
203    .width('100%')
204    .height('100%')
205  }
206}
207```
208![deleteListItem](figures/deleteListItem.gif)
209
210### Example 3
211
212```ts
213// xxx.ets
214@Entry
215@Component
216struct ListItemExample3 {
217  build() {
218    Column() {
219      List({ space: "4vp", initialIndex: 0 }) {
220        ListItemGroup({ style: ListItemGroupStyle.CARD }) {
221          ForEach([ListItemStyle.CARD, ListItemStyle.CARD, ListItemStyle.NONE], (itemStyle: number, index?: number) => {
222            ListItem({ style: itemStyle }) {
223              Text("" + index)
224                .width("100%")
225                .textAlign(TextAlign.Center)
226            }
227          })
228        }
229        ForEach([ListItemStyle.CARD, ListItemStyle.CARD, ListItemStyle.NONE], (itemStyle: number, index?: number) => {
230          ListItem({ style: itemStyle }) {
231            Text("" + index)
232              .width("100%")
233              .textAlign(TextAlign.Center)
234          }
235        })
236      }
237      .width('100%')
238      .multiSelectable(true)
239      .backgroundColor(0xDCDCDC) // List in light blue
240    }
241    .width('100%')
242    .padding({ top: 5 })
243  }
244}
245```
246![ListItemStyle](figures/listItem3.jpeg)
247