HarmonyOS 元服务项目如何将listitem的swipeAction滑动效果恢复至未滑动

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

需要在点击一个button的时候取消listitem的滑动效果,也就是关闭swipeAction已经滑出的效果。

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

HarmonyOS
2024-09-25 12:08:23
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu

由于ListScroller是API 11新增的接口,所以暂不支持元服务开发。

可以自定义动画效果,实现功能

参考demo:

// ListDemo.ets  
@Entry  
@Component  
struct ListDemo {  
  @State arr: number[] = [0, 1, 2, 3, 4]  
  
  @State offsetX: number = 0;  
  @State currentIndex: number = -1;  
  @State curOffsetX: number = 0;  
  
  build() {  
    Column() {  
      List({ space: 20, initialIndex: 0 }) {  
        ForEach(this.arr, (item: number, index) => {  
          ListItem() {  
            AddressItem({  
              item: item + '',  
              onDeleteItem: () => {  
                this.offsetX = 0  
              }  
            })  
          }  
          .translate({x: this.currentIndex == index ? this.offsetX : 0})  
          .animation({  
            duration: 400,  
            curve: Curve.Smooth,  
          })  
          .gesture(  
            PanGesture({ direction: PanDirection.Left | PanDirection.Right })  
              .onActionUpdate((event) => {  
                this.currentIndex = index  
                let x = event.offsetX;  
                // 左滑  
                if (x < 0) {  
                  this.offsetX = (x + this.curOffsetX) < -60 ? -80 : x;  
                }  
                // 右滑  
                if (x > 0) {  
                  this.offsetX = 0;  
                }  
              })  
              .onActionEnd((event) => {  
                let x = event.offsetX;  
                // 左滑  
                if (x < 0) {  
                  this.offsetX = -60;  
                  this.curOffsetX = -60  
                }  
                // 右滑  
                if (x > 0) {  
                  this.offsetX = 0;  
                  this.curOffsetX = 0;  
                }  
              })  
          )  
        })  
      }  
      .listDirection(Axis.Vertical)  
      .margin({ top: 20 })  
    }  
    .backgroundColor(0xDCDCDC)  
    .width('100%')  
    .height('100%')  
  }  
}  
// AddressItem.ets  
import { AlertDialog } from '@ohos.arkui.advanced.Dialog'  
  
@Component  
struct AddressItem {  
  item: string = ''  
  onDeleteItem: () => void = () => {}  
  
  dialogControllerConfirm: CustomDialogController = new CustomDialogController({  
    builder: AlertDialog({  
      content: '确定删除?',  
      primaryButton: {  
        value: '取消',  
        action: () => {  
          this.onDeleteItem()  
        },  
      },  
      secondaryButton: {  
        value: '确认',  
        fontColor: Color.Green,  
        action: () => {  
        }  
      },  
    }),  
    autoCancel: false,  
    customStyle: true,  
    alignment: DialogAlignment.Center  
  })  
  
  build() {  
    Row() {  
      Column() {  
        Text(this.item)  
        Text(this.item)  
      }.width('100%').backgroundColor(Color.White)  
  
      Text('删除')  
        .width(42)  
        .height(42)  
        .fontColor(Color.White)  
        .borderRadius(6)  
        .backgroundColor('#007dff')  
        .textAlign(TextAlign.Center)  
        .margin({ left: 12 })  
        .onClick(() => {  
          this.dialogControllerConfirm.open()  
        })  
    }  
    .width('100%')  
    .constraintSize({ maxWidth: '100%' })  
    .margin({left: 20, right: 10})  
  }  
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 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.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
分享
微博
QQ
微信
回复
2024-09-25 16:28:07
相关问题
HarmonyOS ListItem滑动编辑后如何复原?
758浏览 • 1回复 待解决
HarmonyOS ListItemswipeAction(end:)问题
586浏览 • 1回复 待解决
如何将List回弹效果改为阴影效果
1203浏览 • 1回复 待解决
实现层叠广告滑动效果
1642浏览 • 1回复 待解决
滑动组件如何实现单边spring效果
1700浏览 • 1回复 待解决
HarmonyOS 如何将svg图片导入到项目
1334浏览 • 1回复 待解决