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

Y
yamila 已提交
3
用来展示列表具体item,必须配合List来使用。
Z
zengyawen 已提交
4

G
gmy 已提交
5 6 7
>  **说明:**
>
>  该组件从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
Z
zengyawen 已提交
8

Z
zengyawen 已提交
9 10

## 子组件
Z
zengyawen 已提交
11 12 13

可以包含单个子组件。

Z
zengyawen 已提交
14 15
## 接口

16 17
从API version 9开始,该接口支持在ArkTS卡片中使用。

S
sunjiakun 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
**方法1:** ListItem(value?: ListItemOptions)<sup>10+</sup>

**参数:**

| 参数名 | 参数类型                                      | 必填 | 参数描述                                                     |
| ------ | --------------------------------------------- | ---- | ------------------------------------------------------------ |
| value  | [ListItemOptions](#listitemoptions10对象说明) | 否   | 为ListItem提供可选参数, 该对象内含有ListItemStyle枚举类型的style参数。 |

**方法2:** ListItem(value?: string)<sup>(deprecated)</sup>

从API version 10开始, 该接口不再维护,推荐使用方法1。

**参数:**

| 参数名 | 参数类型                      | 必填 | 参数描述 |
| ------ | ----------------------------- | ---- | -------- |
| value  | string<sup>(deprecated)</sup> | 否   | 无       |

Z
zengyawen 已提交
36 37
## 属性

G
gmy 已提交
38 39 40 41
除支持[通用属性](ts-universal-attributes-size.md)外,还支持以下属性:

| 名称 | 参数类型 | 描述 |
| -------- | -------- | -------- |
Y
yeyinglong 已提交
42
| sticky<sup>(deprecated)</sup> | [Sticky](#stickydeprecated枚举说明) | 设置ListItem吸顶效果。<br/>默认值:Sticky.None<br/>从API version9开始废弃,推荐使用[List组件sticky属性](ts-container-list.md#属性)。 |
Y
yeyinglong 已提交
43
| editable<sup>(deprecated)</sup>  | boolean&nbsp;\|&nbsp;[EditMode](#editmodedeprecated枚举说明) | 当前ListItem元素是否可编辑,进入编辑模式后可删除或移动列表项。<br/>从API version9开始废弃。<br/>默认值:false |
S
sunbees 已提交
44
| selectable<sup>8+</sup> | boolean | 当前ListItem元素是否可以被鼠标框选。<br/>**说明:**<br/>外层List容器的鼠标框选开启时,ListItem的框选才生效。<br/>默认值:true |
Y
yeyinglong 已提交
45
| selected<sup>10+</sup> | boolean | 设置当前ListItem选中状态。该属性支持[$$](../../quick-start/arkts-two-way-sync.md)双向绑定变量。<br/>**说明:**<br/>该属性需要在设置[选中态样式](./ts-universal-attributes-polymorphic-style.md)前使用才能生效选中态样式。<br/>默认值:false|
46
| swipeAction<sup>9+</sup> | {<br/>start?:&nbsp;CustomBuilder&nbsp;\|&nbsp;[SwipeActionItem](#swipeactionitem10对象说明),<br/>end?:CustomBuilder&nbsp;\|&nbsp;[SwipeActionItem](#swipeactionitem10对象说明),<br/>edgeEffect?:&nbsp;[SwipeEdgeEffect](#swipeedgeeffect9枚举说明),<br/>} | 用于设置ListItem的划出组件。<br/>- start:&nbsp;ListItem向右划动时item左边的组件(List垂直布局时)或ListItem向下划动时item上方的组件(List水平布局时)。<br/>- end:&nbsp;ListItem向左划动时item右边的组件(List垂直布局时)或ListItem向上划动时item下方的组件(List水平布局时)。<br/>- edgeEffect:&nbsp;滑动效果。<br/>**说明:** <br/>- start和end对应的@builder函数中顶层必须是单个组件,不能是if/else、ForEach、LazyForEach语句。<br/> - 滑动手势只在listItem区域上,如果子组件划出ListItem区域外,在ListItem以外部分不会响应划动手势。所以在多列模式下,建议不要将划出组件设置太宽。 |
Z
zengyawen 已提交
47

Y
yeyinglong 已提交
48 49
## Sticky<sup>(deprecated)</sup>枚举说明
从API version9开始废弃,推荐使用[List组件stickyStyle枚举](ts-container-list.md#stickystyle9枚举说明)
G
gmy 已提交
50 51 52 53 54
| 名称 | 描述 |
| -------- | -------- |
| None | 无吸顶效果。 |
| Normal | 当前item吸顶。 |
| Opacity | 当前item吸顶显示透明度变化效果。 |
Z
zengyawen 已提交
55

Y
yeyinglong 已提交
56 57
## EditMode<sup>(deprecated)</sup>枚举说明
从API version9开始废弃。
K
kangchongtao 已提交
58 59 60 61 62 63 64
| 名称     | 描述        |
| ------ | --------- |
| None   | 编辑操作不限制。    |
| Deletable | 可删除。 |
| Movable | 可移动。 |

## SwipeEdgeEffect<sup>9+</sup>枚举说明
G
gmy 已提交
65 66
| 名称 | 描述 |
| -------- | -------- |
67 68
| Spring | ListItem划动距离超过划出组件大小后可以继续划动。如果设置了删除区域,ListItem划动距离超过删除阈值后可以继续划动,松手后按照弹簧阻尼曲线回弹。 |
| None | ListItem划动距离不能超过划出组件大小。如果设置了删除区域,ListItem划动距离不能超过删除阈值,并且在设置删除回调的情况下,达到删除阈值后松手触发删除回调。 |
Z
zengyawen 已提交
69

L
limeng 已提交
70
## SwipeActionItem<sup>10+</sup>对象说明
71 72
List垂直布局,ListItem向右滑动,item左边的长距离滑动删除选项或向左滑动时,item右边的长距离滑动删除选项。
</br>List水平布局,ListItem向上滑动,item下边的长距离滑动删除选项或向下滑动时,item上边的长距离滑动删除选项。
L
limeng 已提交
73 74 75

| 名称                 | 参数类型                                                     | 必填 | 描述                                                         |
| -------------------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
76 77 78 79
| actionAreaDistance | [Length](ts-types.md#length) | 否 | 设置组件长距离滑动删除距离阈值。<br/>默认值:56vp <br/>**说明:** <br/>不支持设置百分比。<br/>删除距离阈值大于item宽度减去划出组件宽度,或删除距离阈值小于等于0就不会设置删除区域。|
| onAction | () => void | 否 | 组件进入长距删除区后删除ListItem时调用,进入长距删除区后抬手时触发。<br/>**说明:** <br/> 滑动后松手的位置超过或等于设置的距离阈值,并且设置的距离阈值有效时才会触发。|
| onEnterActionArea | () => void | 否 | 在滑动条目进入删除区域时调用,只触发一次,当再次进入时仍触发。 |
| onExitActionArea | () => void | 否 |当滑动条目退出删除区域时调用,只触发一次,当再次退出时仍触发。 |
80
| builder |  CustomBuilder | 否 |当列表项向右或向右滑动(当列表方向为“垂直”时),向下或向下滑动(当列方向为“水平”时)时显示的操作项。 |
S
sunjiakun 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93
## ListItemOptions<sup>10+</sup>对象说明

| 名称  | 参数类型                                  | 必填 | 描述                                                         |
| ----- | ----------------------------------------- | ---- | ------------------------------------------------------------ |
| 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,居中对齐显示。 |

## ListItemStyle<sup>10+</sup>枚举说明

| 名称 | 描述               |
| ---- | ------------------ |
| NONE | 无样式。           |
| CARD | 显示默认卡片样式。 |

Z
zengyawen 已提交
94 95 96 97
## 事件

| 名称 | 功能描述 |
| -------- | -------- |
S
sunbees 已提交
98
| onSelect(event:&nbsp;(isSelected:&nbsp;boolean)&nbsp;=&gt;&nbsp;void)<sup>8+</sup> | ListItem元素被鼠标框选的状态改变时触发回调。<br/>isSelected:进入鼠标框选范围即被选中返回true,&nbsp;移出鼠标框选范围即未被选中返回false。 |
Z
zengyawen 已提交
99 100

## 示例
Z
zengyawen 已提交
101

L
l00613276 已提交
102 103
### 示例1 

H
geshi  
HelloCrease 已提交
104 105
```ts
// xxx.ets
Z
zengyawen 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118
@Entry
@Component
struct ListItemExample {
  private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

  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)
Y
yeyinglong 已提交
119
          }
Z
zengyawen 已提交
120
        }, item => item)
Y
yeyinglong 已提交
121
      }.width('90%')
Y
yeyinglong 已提交
122
      .scrollBar(BarState.Off)
Z
zengyawen 已提交
123 124 125 126 127
    }.width('100%').height('100%').backgroundColor(0xDCDCDC).padding({ top: 5 })
  }
}
```

Z
zengyawen 已提交
128
![zh-cn_image_0000001219864159](figures/zh-cn_image_0000001219864159.gif)
129

L
l00613276 已提交
130 131
### 示例2

L
limeng 已提交
132 133 134 135 136

```ts
// xxx.ets
@Entry
@Component
137 138
struct ListItemExample2 {
  @State message: string = 'Hello World'
L
limeng 已提交
139 140 141 142
  @State arr: number[] = [0, 1, 2, 3, 4]
  @State enterEndDeleteAreaString: string = "not enterEndDeleteArea"
  @State exitEndDeleteAreaString: string = "not exitEndDeleteArea"

143
  @Builder itemEnd() {
144 145
    Row() {
      Button("Delete").margin("4vp")
L
limeng 已提交
146
      Button("Set").margin("4vp")
147
    }.padding("4vp").justifyContent(FlexAlign.SpaceEvenly)
L
limeng 已提交
148
  }
149

L
limeng 已提交
150 151
  build() {
    Column() {
152
      List({ space: 10 }) {
153
        ForEach(this.arr, (item) => {
L
limeng 已提交
154 155 156 157 158 159 160 161 162
          ListItem() {
            Text("item" + item)
              .width('100%')
              .height(100)
              .fontSize(16)
              .textAlign(TextAlign.Center)
              .borderRadius(10)
              .backgroundColor(0xFFFFFF)
          }
163
          .transition({ type: TransitionType.Delete, opacity: 0 })
164 165 166
          .swipeAction({
            end: {
              builder: this.itemEnd.bind(this, item),
167
              onAction: () => {
168 169 170 171 172
                animateTo({ duration: 1000 }, () => {
                  let index = this.arr.indexOf(item)
                  this.arr.splice(index, 1)
                })
              },
173
              actionAreaDistance: 56,
174
              onEnterActionArea: () => {
175 176 177
                this.enterEndDeleteAreaString = "enterEndDeleteArea"
                this.exitEndDeleteAreaString = "not exitEndDeleteArea"
              },
178
              onExitActionArea: () => {
179 180 181
                this.enterEndDeleteAreaString = "not enterEndDeleteArea"
                this.exitEndDeleteAreaString = "exitEndDeleteArea"
              }
L
limeng 已提交
182 183 184 185 186 187 188 189 190 191 192 193 194
            }
          })
        }, item => item)
      }
      Text(this.enterEndDeleteAreaString).fontSize(20)
      Text(this.exitEndDeleteAreaString).fontSize(20)
    }
    .padding(10)
    .backgroundColor(0xDCDCDC)
    .width('100%')
    .height('100%')
  }
}
195
```
L
limeng 已提交
196
![deleteListItem](figures/deleteListItem.gif)
197

L
l00613276 已提交
198 199
### 示例3

200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
```ts
// xxx.ets
@Entry
@Component
struct ListItemExample3 {
 private arr: any = [ListItemStyle.CARD, ListItemStyle.CARD,ListItemStyle.NONE]
 build() {
  Column() {
   List({ space: "4vp", initialIndex: 0 }) {
    ListItemGroup({style:ListItemGroupStyle.CARD}){
     ForEach(this.arr, (itemStyle,index) => {
      ListItem({style:itemStyle}) {
       Text(""+index)
        .width("100%")
        .textAlign(TextAlign.Center)
      }
     })
    }
    ForEach(this.arr, (itemStyle,index) => {
     ListItem({style:itemStyle}) {
      Text(""+index)
       .width("100%")
       .textAlign(TextAlign.Center)
     }
    })
   }
   .width('100%')
   .multiSelectable(true)
   .backgroundColor(0xDCDCDC) // 浅蓝色的List
  }
  .width('100%')
  .padding({ top: 5 })
 }
}

```
![ListItemStyle](figures/listItem3.jpeg)