ts-container-list.md 9.5 KB
Newer Older
Z
zengyawen 已提交
1
# List
Z
zengyawen 已提交
2

Z
zengyawen 已提交
3

E
ester.zhou 已提交
4 5
> **NOTE**
>
Z
zengyawen 已提交
6
> This component is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
7 8


E
ester.zhou 已提交
9
The **\<List>** component provides a list container that presents a series of list items arranged in columns with the same width. It supports presentation of the same type of data, for example, images or text, in a multiple and coherent row style.
Z
zengyawen 已提交
10

Z
zengyawen 已提交
11

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

Z
zengyawen 已提交
14
None
Z
zengyawen 已提交
15 16


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

E
ester.zhou 已提交
19
This component contains the child component [\<ListItem>](ts-container-listitem.md).
Z
zengyawen 已提交
20 21


Z
zengyawen 已提交
22
## APIs
Z
zengyawen 已提交
23

E
ester.zhou 已提交
24
List(value:{space?: number | string, initialIndex?: number, scroller?: Scroller})
Z
zengyawen 已提交
25

Z
zengyawen 已提交
26
- Parameters
27
  | Name | Type | Mandatory | Default Value | Description |
Z
zengyawen 已提交
28
  | -------- | -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
29 30 31
  | space | number&nbsp;\|&nbsp;string | No | 0 | Spacing between list items. |
  | initialIndex | number | No | 0 | Item displayed at the beginning of the viewport when the current list is loaded for the first time, that is, the first item to be displayed. If the value set is greater than the sequence number of the last item, the setting does not take effect. |
  | scroller | [Scroller](ts-container-scroll.md#scroller) | No | - | Controller bound to the list to control the scrolling. |
Z
zengyawen 已提交
32 33


Z
zengyawen 已提交
34
## Attributes
Z
zengyawen 已提交
35

E
esterzhou 已提交
36
| Name | Type | Default Value | Description |
Z
zengyawen 已提交
37
| -------- | -------- | -------- | -------- |
E
esterzhou 已提交
38
| listDirection | [Axis](ts-appendix-enums.md#axis-enums) | Vertical | Direction in which the list items are arranged. For details, see **Axis** enums. |
E
ester.zhou 已提交
39
| divider | {<br>strokeWidth: Length,<br>color?:Color,<br>startMargin?: Length,<br>endMargin?: Length<br>} | - | Style of the divider for the list items. By default, there is no divider.<br>**strokeWidth**: stroke width of the divider.<br>**color**: color of the divider.<br>**startMargin**: distance between the divider and the start of the list.<br>**endMargin**: distance between the divider and the end of the list. |
E
ester.zhou 已提交
40 41
| editMode | boolean | false | Whether the **\<List>** component is in editable mode. |
| edgeEffect | EdgeEffect | EdgeEffect.Spring | Sliding effect. For details, see **EdgeEffect enums**. |
E
ester.zhou 已提交
42 43 44 45 46
| chainAnimation | boolean | false | Whether to display chained animations on this list when it slides or its top and bottom are dragged. The list items are separated with even space, and one item animation starts after the previous animation during basic sliding interactions. The chained animation effect is similar with spring physics.<br>- **false**: No chained animations are displayed.<br>- **true**: Chained animations are displayed. |
| multiSelectable<sup>8+</sup> | boolean | false | Whether to enable mouse frame selection.<br>- **false**: The mouse frame selection is disabled.<br>- **true**: The mouse frame selection is enabled. |
| restoreId<sup>8+</sup> | number | - | Migration ID of the component. During application migration, the status of the component is migrated to the component with the same migration ID on the peer end.<br>For a **<\List>** component, the status includes the item serial number displayed at the start position. |
| lanes<sup>9+</sup> | number \|<br>{<br>minLength: Length,<br>maxLength: Length<br>} | 1 | In the following description, **listDirection** is set to **Axis.Vertical**:<br>Number of columns in which the list items are arranged along the cross axis.<br>- If the value type is number, the column width is determined based on the specified number and the cross-axis width of the **\<List>** component.<br>- If the value type is {minLength, maxLength}, the number of columns is adjusted adaptively based on the width of the **\<List>** component, ensuring that the width respects the {minLength, maxLength} constraints during adaptation. The **minLength** constraint is prioritized. For example, if **lanes** is set to **{minLength: 40vp, maxLength: 60vp}** and the width of the **\<List>** component is 70 vp, the list items are arranged in one column with their alignment compliant with the **alignListItem** settings. If the width of the **\<List>** component is changed to 80 vp, which is twice the value of **minLength**, the list items are arranged in two columns.|
| alignListItem<sup>9+</sup> | ListItemAlign | ListItemAlign.Center | Alignment mode of list items along the cross axis when: Cross-axis width of the **\<List>** component > Cross-axis width of list items x Value of **lanes**.|
Z
zengyawen 已提交
47

Z
zengyawen 已提交
48
- EdgeEffect enums
49
  | Name | Description |
Z
zengyawen 已提交
50
  | -------- | -------- |
E
ester.zhou 已提交
51
  | Spring | Similar to the physical dynamic effect of a spring. After scrolling to the edge, the user can continue to scroll for a distance based on the initial speed or by touching the knob of the scrollbar. After the user releases their hand, the knob is rebounded. |
E
ester.zhou 已提交
52
  | None | No effect when the list is scrolled to the edge. |
E
ester.zhou 已提交
53 54 55 56 57 58 59 60

- ListItemAlign enums

  | Name  | Description                                  |
  | ------ | -------------------------------------- |
  | Start  | The list items are packed toward the start edge of the **\<List>** component along the cross axis.|
  | Center | The list items are centered in the **\<List>** component along the cross axis.|
  | End    | The list items are packed toward the end edge of the **\<List>** component in the cross axis.|
Z
zengyawen 已提交
61 62


Z
zengyawen 已提交
63
## Events
Z
zengyawen 已提交
64

E
esterzhou 已提交
65
| Name | Description |
Z
zengyawen 已提交
66
| -------- | -------- |
E
esterzhou 已提交
67
| onItemDelete(index: number) =&gt; boolean | Triggered when a list item is deleted. |
E
ester.zhou 已提交
68 69
| onScrollBegin<sup>9+</sup>(dx: number, dy: number)&nbsp;=&gt;&nbsp;{ dxRemain: number, dyRemain: number } | Triggered when scrolling starts.<br/>Parameters:<br/>- **dx**: amount by which the list will scroll in the horizontal direction.<br/>- **dy**: amount by which the list will scroll in the vertical direction.<br/>Return value:<br/>- **dxRemain**: remaining amount by which the list can scroll in the horizontal direction.<br/>- **dyRemain**: remaining amount by which the list can scroll in the vertical direction. |
| onScrollIndex(firstIndex:&nbsp;number,&nbsp;lastIndex:&nbsp;number)&nbsp;=&gt;&nbsp;void | Triggered when the start position and end position of the current list are changed. |
Z
zengyawen 已提交
70

E
ester.zhou 已提交
71 72
> **NOTE**
>
Z
zengyawen 已提交
73
> To enable the editable mode for a list, the following conditions must be met:
E
ester.zhou 已提交
74
>
Z
zengyawen 已提交
75
> - **editMode** is set to **true**.
E
ester.zhou 已提交
76 77 78
>
> - The list is bound to the **onItemDelete** event, and the event returns **true** through a callback.
>
Z
zengyawen 已提交
79
> - The **editable** attribute of **ListItem** is set to **true**.
E
ester.zhou 已提交
80
>
Z
zengyawen 已提交
81
> To enable dragging for a list item, the following conditions must be met:
E
ester.zhou 已提交
82
>
Z
zengyawen 已提交
83
> - **editMode** is set to **true**.
E
ester.zhou 已提交
84 85
>
> - The list item is bound to the **onDragStart** event, and the event returns a floating UI through a callback.
Z
zengyawen 已提交
86

Z
zengyawen 已提交
87

Z
zengyawen 已提交
88 89
## Example

Z
zengyawen 已提交
90

E
ester.zhou 已提交
91 92
```ts
// xxx.ets
Z
zengyawen 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
@Entry
@Component
struct ListExample {
  private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  @State editFlag: boolean = false

  build() {
    Stack({ alignContent: Alignment.TopStart }) {
      Column() {
        List({ space: 20, initialIndex: 0 }) {
          ForEach(this.arr, (item) => {
            ListItem() {
              Text('' + item)
                .width('100%').height(100).fontSize(16)
                .textAlign(TextAlign.Center).borderRadius(10).backgroundColor(0xFFFFFF)
            }.editable(true)
          }, item => item)
        }
E
ester.zhou 已提交
111 112 113
        .listDirection(Axis.Vertical) // Direction in which the list items are arranged.
        .divider({ strokeWidth: 2, color: 0xFFFFFF, startMargin: 20, endMargin: 20 }) // Style of the divider for the list items.
        .edgeEffect(EdgeEffect.None) // No effect when the list is scrolled to the edge.
Z
zengyawen 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
        .chainAnimation(false) // Chained animations are disabled.
        .onScrollIndex((firstIndex: number, lastIndex: number) => {
          console.info('first' + firstIndex)
          console.info('last' + lastIndex)
        })
        .editMode(this.editFlag)
        .onItemDelete((index: number) => {
          console.info(this.arr[index] + 'Delete')
          this.arr.splice(index, 1)
          console.info(JSON.stringify(this.arr))
          this.editFlag = false
          return true
        }).width('90%')
      }.width('100%')

      Button('edit list')
        .onClick(() => {
          this.editFlag = !this.editFlag
        }).margin({ top: 5, left: 20 })
    }.width('100%').height('100%').backgroundColor(0xDCDCDC).padding({ top: 5 })
  }
}
```

Z
zengyawen 已提交
138
![en-us_image_0000001256978357](figures/en-us_image_0000001256978357.gif)
E
ester.zhou 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

```ts
// xxx.ets
@Entry
@Component
struct ListLanesExample {
  @State arr: string[] = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19"]
  @State alignListItem: ListItemAlign = ListItemAlign.Start

  build() {
    Column() {
      List({ space: 20, initialIndex: 0 }) {
        ForEach(this.arr, (item) => {
          ListItem() {
            Text('' + item)
              .width('100%')
              .height(100)
              .fontSize(16)
              .textAlign(TextAlign.Center)
              .borderRadius(10)
              .backgroundColor(0xFFFFFF)
          }
          .border({ width: 2, color: Color.Green })
        }, item => item)
      }
      .height(300)
      .width("90%")
      .editMode(true)
      .border({ width: 3, color: Color.Red })
      .lanes({ minLength: 40, maxLength: 60 })
      .alignListItem(this.alignListItem)

E
ester.zhou 已提交
171
      Button("Change alignListItem:" + this.alignListItem).onClick(() => {
E
ester.zhou 已提交
172 173 174 175 176 177 178 179 180 181 182 183
        if (this.alignListItem == ListItemAlign.Start) {
          this.alignListItem = ListItemAlign.Center
        } else if (this.alignListItem == ListItemAlign.Center) {
          this.alignListItem = ListItemAlign.End
        } else {
          this.alignListItem = ListItemAlign.Start
        }
      })
    }.width('100%').height('100%').backgroundColor(0xDCDCDC).padding({ top: 5 })
  }
}
```
E
ester.zhou 已提交
184