HarmonyOS List拖动排序示例

HarmonyOS
2024-12-24 18:20:37
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Excelsior_abit

请参考以下demo:

import curves from '@ohos.curves';

@Entry
@Component
struct Index {
  @State numbers: String[] = ["娱乐", "关注", "热榜", "航天", "动漫", "宠物", "保险", "NBA", "汽车", "财经", "体育", "女性", "搞笑", "军事", "有料", "北京", "深度", "放松"]
  scroller: Scroller = new Scroller()
  @State text: string = 'drag'
  @State isShowDelete: boolean = false
  @State isEdit: boolean = false
  @State rotateZ: number = 0;

  private stopJump() {
    animateTo({
      delay: 0,
      tempo: 5,
      duration: 0,
      curve: Curve.Smooth,
      playMode: PlayMode.Normal,
      iterations: 1

    },
      () => {
        this.rotateZ = 0;

      })
  }

  //抖动的动画
  private jumpWithSpeed(speed: number) {
    if (this.isEdit) {
      this.rotateZ = -1;
      animateTo({
        delay: 0, //延时播放
        tempo: speed, //动画的播放速度,值越大动画播放越快
        duration: 1000, //动画持续的时间
        curve: Curve.Smooth, //动画曲线
        playMode: PlayMode.Normal, //动画播放模式
        iterations: -1

      },
        () => {
          this.rotateZ = 1;

        })
    } else {
      this.stopJump()
    }


  }

  //拖拽过程中展示的样式
  @Builder
  pixelMapBuilder() {
    Column() {
      Text(this.text)
        .fontSize(16)
        .backgroundColor(0xF9CF93)
        .width(80)
        .height(40)
        .textAlign(TextAlign.Center)
        .borderRadius(20)
        .borderWidth(2)
        .borderColor(Color.Orange)
    }
  }

  //改变数组中元素位置
  changeIndex(index1: number, index2: number) {
    this.numbers.splice(index2, 0, this.numbers.splice(index1, 1)[0])

  }

  build() {
    Column({ space: 5 }) {
      Row({ space: 30 }) {
        Text("我的频道")
          .fontSize(20)
        Text(this.isEdit ? "长按拖动调整顺序" : "点击编辑按钮进入编辑")
          .fontColor(Color.Gray)
        Button() {
          Text(this.isEdit ? "完成" : "编辑")
            .fontColor(this.isEdit ? Color.Black : 0xF9CF93)
        }.width(80)
        .height(30)
        .backgroundColor(Color.White)
        .onClick(() => {
          this.isEdit = !this.isEdit
          this.jumpWithSpeed(5)
        })
      }.padding({ left: 15 })

      Grid(this.scroller) {
        ForEach(this.numbers, (day: string, index: number) => {
          GridItem() {
            Stack({ alignContent: Alignment.TopEnd }) {
              Text(day)
                .borderRadius(20)
                .borderWidth(2)
                .borderColor(Color.Orange)
                .fontSize(16)
                .backgroundColor(0xF9CF93)
                .width(80)
                .height(40)
                .textAlign(TextAlign.Center)
                .onTouch((event: TouchEvent) => {
                  if (event.type === TouchType.Down) {
                    this.text = day

                  }
                })
              if (this.isEdit) {
                Image($r('app.media.close'))
                  .width(20)
                  .height(20)
                  .onClick(() => {
                    animateTo({ duration: 300 }, () => {
                      this.numbers.splice(index, 1)
                    })
                    this.stopJump()

                    this.jumpWithSpeed(5)
                  })
              }
            }
            .rotate({ z: this.rotateZ,
              angle: 0.4,
              centerX: 0.5,
              centerY: 0.5

            })

          }
          .transition({ type: TransitionType.All, translate: { x: 100 }, scale: { x: 1, y: 1 } })
          .padding({ top: 15 })

        })
      }
      .columnsTemplate('1fr 1fr 1fr')
      .columnsGap(10)
      .rowsGap(10)
      .onScrollIndex((first: number) => {
        console.info(first.toString())
      })
      .margin({ top: 5 })
      .width('100%')
      .backgroundColor(0xFAEEE0)
      .height('100%')
      .supportAnimation(true) //是否支持动画
      //设置Grid是否进入编辑模式,进入编辑模式可以拖拽Grid组件内部GridItem
      .editMode(this.isEdit)
      //第一次拖拽此事件绑定的组件时,触发回调
      .onItemDragStart((event: ItemDragInfo, itemIndex: number) => {
        //设置拖拽过程中显示的图片
        return this.pixelMapBuilder()
      })
      //绑定此事件的组件可作为拖拽释放目标,当在本组件范围内停止拖拽行为时,触发回调
      //itemIndex为拖拽起始位置,insertIndex为拖拽插入位置
      .onItemDrop((event: ItemDragInfo, itemIndex: number, insertIndex: number, isSuccess: boolean) => {
        //不支持拖拽到已有内容以外的位置
        if (insertIndex < this.numbers.length) {
          this.changeIndex(itemIndex, insertIndex)
          this.stopJump()
          this.jumpWithSpeed(5)
        }
      })
    }.width('100%')
    .margin({ top: 5 })
    .alignItems(HorizontalAlign.End)
  }
}
  • 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.
  • 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.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
分享
微博
QQ
微信
回复
2024-12-24 20:35:47
相关问题
HarmonyOS List组件如何实现拖动排序
1089浏览 • 1回复 待解决
拖动实现列表重新排序
1951浏览 • 1回复 待解决
HarmonyOS Grid拖动排序和长按冲突
1538浏览 • 2回复 待解决
HarmonyOS list元素拖动换位置的实现
590浏览 • 1回复 待解决
根据list中某项的id如何sort排序
1361浏览 • 1回复 待解决
HarmonyOS GridRow 实现拖拽排序
682浏览 • 1回复 待解决
HarmonyOS Grid拖动+删除?
781浏览 • 0回复 待解决
HarmonyOS bindSheet拖动问题
469浏览 • 1回复 待解决
DataAbilityHelper怎么排序?
4808浏览 • 1回复 待解决
HarmonyOS Grid组件拖拽排序
1154浏览 • 1回复 待解决
HarmonyOS 数组中对象的排序
1118浏览 • 1回复 待解决
HarmonyOS 悬浮按钮拖动问题
1268浏览 • 1回复 待解决
HarmonyOS Grid组件拖动异常
968浏览 • 1回复 待解决
HarmonyOS 拖动和停靠效果
619浏览 • 1回复 待解决
HarmonyOS Array.sort排序错误
694浏览 • 1回复 待解决