鸿蒙原生开发中ListItem侧边滑,如何通过状态控制禁止和生效?

鸿蒙原生开发中ListItem侧边滑,如何通过状态控制禁止和生效?
场景描述:
1、List多选的情况下,需要禁止侧边滑动
2、List非多选情况下,需要支持侧边滑动

本文参与了 【 HarmonyOS NEXT 技术问答冲榜,等你来战!】欢迎正在阅读的你也加入。

阅读 596
1 个回答

你可以参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-refere...

@Entry 
@Component 
struct Index { 
  @State itemIndexArr: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 
 
  @Builder 
  itemEnd(index: number) { 
    Row() { 
      Text("置顶") 
        .padding(5) 
        .fontSize(16) 
        .backgroundColor('#808080') 
        .fontColor(Color.White) 
        .textAlign(TextAlign.Center) 
        .borderRadius(16) 
        .width(100) 
        .height('100%') 
        .onClick(() => { 
          this.itemIndexArr.splice(0, 0, this.itemIndexArr[index]) 
          this.itemIndexArr.splice(index + 1, 1) 
        }) 
      Text("删除") 
        .padding(5) 
        .fontSize(16) 
        .backgroundColor('#FF0000') 
        .fontColor(Color.White) 
        .borderRadius(16) 
        .textAlign(TextAlign.Center) 
        .width(100) 
        .height('100%') 
        .onClick(() => { 
          this.itemIndexArr.splice(index, 1) 
        }) 
    }.padding(4).justifyContent(FlexAlign.SpaceEvenly) 
  } 
 
 
  build() { 
    Column() { 
      List({ space: 10 }) { 
        ForEach(this.itemIndexArr, (item: number, index: number) => { 
          ListItem() { 
            Text('' + item) 
              .width('100%') 
              .height(100) 
              .fontSize(16) 
              .margin({ top: 10 }) 
              .borderRadius(16) 
              .textAlign(TextAlign.Center) 
              .backgroundColor(Color.White) 
          }.swipeAction({ 
            end: this.itemEnd.bind(this, index), 
            edgeEffect: SwipeEdgeEffect.Spring, 
          }) 
 
          .onGestureJudgeBegin((gestureInfo: GestureInfo, event: BaseGestureEvent) => { 
            //禁用双数的item 
            if ((gestureInfo.isSystemGesture = true)&&(item%2==0)) { 
              return GestureJudgeResult.REJECT 
            } else { 
              return GestureJudgeResult.CONTINUE 
            } 
          }) 
        }, (item: string) => item) 
      } 
    } 
    .padding(10) 
    .backgroundColor(0xDCDCDC) 
    .width('100%') 
    .height('100%') 
  } 
}

本文参与了 【 HarmonyOS NEXT 技术问答冲榜,等你来战!】欢迎正在阅读的你也加入。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题