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

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

Z
zengyawen 已提交
6 7 8

用来展示列表具体item,宽度默认充满List组件,必须配合List来使用。

Z
zengyawen 已提交
9

Z
zengyawen 已提交
10

Z
zengyawen 已提交
11 12

## 子组件
Z
zengyawen 已提交
13 14 15

可以包含单个子组件。

Z
zengyawen 已提交
16 17 18 19 20 21 22 23 24 25

## 接口

ListItem()


## 属性

| 名称 | 参数类型 | 默认值 | 描述 |
| -------- | -------- | -------- | -------- |
S
sienna1128 已提交
26 27
| sticky | [Sticky](#Sticky)| Sticky.None | 设置ListItem吸顶效果。 |
| editable  | boolean \| [EditMode](#EditMode) | false       | 当前ListItem元素是否可编辑,进入编辑模式后可删除或移动。    |
H
geshi  
HelloCrease 已提交
28
| selectable<sup>8+</sup> | boolean | true | 当前ListItem元素是否可以被鼠标框选。<br/>>&nbsp;&nbsp;**说明:**<br/>>&nbsp;外层List容器的鼠标框选开启时,ListItem的框选才生效。 |
S
sienna1128 已提交
29
| swipeAction<sup>9+</sup> | {<br/>start?:&nbsp;CustomBuilder,<br/>end?:CustomBuilder,<br/>edgeEffect?:&nbsp;[SwipeEdgeEffect](#SwipeEdgeEffect<sup>9+</sup>),<br/>} | - | 用于设置ListItem的划出组件。<br/>start:&nbsp;ListItem向右划动时item左边的组件(List垂直布局时)或ListItem向下划动时item上方的组件(List水平布局时)。<br/>end:&nbsp;ListItem向左划动时item右边的组件(List垂直布局时)或ListItem向上划动时item下方的组件(List水平布局时)。<br/>edgeEffect:&nbsp;滑动效果。<br/> |
Z
zengyawen 已提交
30

S
sienna1128 已提交
31
## Sticky
Z
zengyawen 已提交
32 33 34 35
  | 名称 | 描述 | 
  | -------- | -------- |
  | None | 无吸顶效果。 | 
  | Normal | 当前item吸顶。 | 
K
kangchongtao 已提交
36
  | Opacity | 当前item吸顶显示透明度变化效果。 |
Z
zengyawen 已提交
37

S
sienna1128 已提交
38
## EditMode
K
kangchongtao 已提交
39 40 41 42 43 44 45

| 名称     | 描述        |
| ------ | --------- |
| None   | 编辑操作不限制。    |
| Deletable | 可删除。 |
| Movable | 可移动。 |

S
sienna1128 已提交
46
## SwipeEdgeEffect<sup>9+</sup>
47 48 49 50
  | 名称 | 描述 |
  | -------- | -------- |
  | Spring | ListItem划动距离超过划出组件大小后可以继续划动,松手后按照弹簧阻尼曲线回弹。 |
  | None | ListItem划动距离不能超过划出组件大小。 |
Z
zengyawen 已提交
51 52 53 54 55

## 事件

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


## 示例
Z
zengyawen 已提交
60

H
geshi  
HelloCrease 已提交
61 62
```ts
// xxx.ets
Z
zengyawen 已提交
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 90 91 92 93 94 95 96 97 98 99 100
@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 已提交
101
![zh-cn_image_0000001219864159](figures/zh-cn_image_0000001219864159.gif)
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152

```ts
// xxx.ets
@Entry
@Component
struct ListItemExample2 {
  @State message: string = 'Hello World'

  @Builder itemEnd() {
    Row () {
      Button("Del").margin("4vp")
      Button("Set").margin("4vp")
    }.padding("4vp").justifyContent(FlexAlign.SpaceEvenly)
  }

  build() {
    Column() {
      List({space:10}) {
        ListItem() {
          Text(this.message) {
          }
          .width('100%')
          .height(100)
          .fontSize(16)
          .textAlign(TextAlign.Center)
          .borderRadius(10)
          .backgroundColor(0xFFFFFF)
        }
        .swipeAction({ end:this.itemEnd})

        ListItem() {
          Text(this.message) {
          }
          .width('100%')
          .height(100)
          .fontSize(16)
          .textAlign(TextAlign.Center)
          .borderRadius(10)
          .backgroundColor(0xFFFFFF)
        }
        .swipeAction({ start:this.itemEnd})
      }
    }
    .padding(10)
    .backgroundColor(0xDCDCDC)
    .width('100%')
    .height('100%')
  }
}
```
![zh-cn_image_1501929990650](figures/zh-cn_image_1501929990650.jpg)