ts-container-listitem.md 2.8 KB
Newer Older
Z
zengyawen 已提交
1
# ListItem
Z
zengyawen 已提交
2

Z
zengyawen 已提交
3

E
esterzhou 已提交
4
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
Z
zengyawen 已提交
5
> This component is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
6

Z
zengyawen 已提交
7 8 9 10 11

The **<ListItem>** component displays specific items in the list. Its width occupies the **<List>** component by default and must be used together with **<List>**.


## Required Permissions
Z
zengyawen 已提交
12 13 14

None

Z
zengyawen 已提交
15 16

## Child Components
Z
zengyawen 已提交
17 18 19

This component can contain a single child component.

Z
zengyawen 已提交
20 21 22 23 24 25 26 27

## APIs

ListItem()


## Attributes

E
esterzhou 已提交
28
| Name | Type | Default Value | Description |
Z
zengyawen 已提交
29
| -------- | -------- | -------- | -------- |
E
esterzhou 已提交
30 31 32
| sticky | Sticky | Sticky.None | Sticky effect of the list item. For details, see Sticky enums. |
| editable | boolean | false | Whether the list item is editable. A list item can be deleted in editing mode. |
| selectable<sup>8+</sup> | boolean | true | Whether the current **&lt;ListItem&gt;** is selectable by the mouse.<br/>> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br/>> This attribute takes effect only when mouse frame selection is enabled for the parent **&lt;List&gt;** container. |
Z
zengyawen 已提交
33 34 35 36

- Sticky enums
    | Name | Description | 
  | -------- | -------- |
E
esterzhou 已提交
37 38
  | None | No sticky. | 
  | Normal | The list item is sticky. | 
Z
zengyawen 已提交
39 40 41 42 43 44


## Events

  | Name | Description | 
| -------- | -------- |
E
esterzhou 已提交
45
| onSelect(callback: (isSelected: boolean) =&gt; any)<sup>8+</sup> | Triggered when the selected state of the **&lt;ListItem&gt;** changes.<br/>**isSelected**: Returns **true** if the **&lt;ListItem&gt;** is selected by the mouse; returns **false** otherwise. | 
Z
zengyawen 已提交
46 47 48 49


## Example

Z
zengyawen 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

```
@Entry
@Component
struct ListItemExample {
  private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  @State editFlag: boolean = false

  build() {
    Column() {
      List({ space: 20, initialIndex: 0 }) {
        ListItem() {
          Text('sticky:Normal , click me edit list')
            .width('100%').height(40).fontSize(12).fontColor(0xFFFFFF)
            .textAlign(TextAlign.Center).backgroundColor(0x696969)
            .onClick(() => {
              this.editFlag = !this.editFlag
            })
        }.sticky(Sticky.Normal)

        ForEach(this.arr, (item) => {
          ListItem() {
            Text('' + item)
              .width('100%').height(100).fontSize(16)
              .textAlign(TextAlign.Center).borderRadius(10).backgroundColor(0xFFFFFF)
          }.editable(this.editFlag)
        }, item => item)
      }
      .editMode(true)
      .onItemDelete((index: number) => {
        console.info(this.arr[index - 1] + 'Delete')
        this.arr.splice(index - 1,1)
        this.editFlag = false
        return true
      }).width('90%')
    }.width('100%').height('100%').backgroundColor(0xDCDCDC).padding({ top: 5 })
  }
}
```

Z
zengyawen 已提交
90
![en-us_image_0000001257138339](figures/en-us_image_0000001257138339.gif)