【HarmonyOS NEXT】如何将listitem的swipeAction滑动效果恢复至未滑动

 【关键字】

listitem / swipeAction / 滑动效果 / 恢复

【问题描述】

如何将listitem的swipeAction滑动效果恢复至未滑动?

【解决方案】

可以使用ListScroller提供的closeAllSwipeActions()方法将滑动效果进行恢复。

参考文档如下:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-container-list-V5#closeallswipeactions11

示例代码如下:

@CustomDialog
struct CustomDialogExample {
cancel?: () => void
confirm?: () => void
controller: CustomDialogController

build() {
Column() {
Text('我是内容').fontSize(20).margin({ top: 10, bottom: 10 })
Flex({ justifyContent: FlexAlign.SpaceAround }) {
Button('cancel').onClick(() => {
this.controller.close()
if (this.cancel) this.cancel()
}).backgroundColor(0xffffff).fontColor(Color.Black)
Button('confirm').onClick(() => {
this.controller.close()
if (this.confirm) this.confirm()
}).backgroundColor(0xffffff).fontColor(Color.Red)
}.margin({ bottom: 10 })
}
}
}


@Entry
@Component
struct ListSwipePage {
@State arr: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
private scrollerForList: ListScroller = new ListScroller()
dialogController: CustomDialogController = new CustomDialogController({
builder: CustomDialogExample({
cancel: this.onCancel,
confirm: this.onAccept,
})
})

onCancel() {
}

onAccept() {
}

@Builder
itemEnd() {
Row() {
Button("Delete").margin("4vp")
Button("Set").margin("4vp")
.onClick(() => {
this.dialogController.open()
this.scrollerForList.closeAllSwipeActions() // 重点是这行代码
})
}.padding("4vp").justifyContent(FlexAlign.SpaceEvenly)
}

build() {
Column() {
List({ space: 5, scroller: this.scrollerForList }) {
ForEach(this.arr, (item: number) => {
ListItem() {
Text("item" + item)
.width('100%')
.height(100)
.textAlign(TextAlign.Center)
.borderRadius(10)
.backgroundColor(0xFFFFFF)
}
.transition({ type: TransitionType.Delete, opacity: 0 })
.swipeAction({
end: {
builder: () => {
this.itemEnd()
},
onAction: () => {
animateTo({ duration: 1000 }, () => {
let index = this.arr.indexOf(item)
this.arr.splice(index, 1)
})
}, actionAreaDistance: 56
}
})
}, (item: string) => item)
}
}
.padding(20).backgroundColor(0xDCDCDC).width('100%').height('100%')
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值