HarmonyOS 鸿蒙Next中如何实现支持拖拽排序的List组件并允许用户自定义列表顺序 List和Grid组件在不同设备上表现是否一致

发布于 1周前 作者 wuwangju 来自 鸿蒙OS

HarmonyOS 鸿蒙Next中如何实现支持拖拽排序的List组件并允许用户自定义列表顺序 List和Grid组件在不同设备上表现是否一致

请教一个鸿蒙的问题在HarmonyOS NEXT中,如何实现一个支持拖拽排序的List组件,以允许用户自定义列表顺序?List和Grid组件在不同设备(如手机、平板、电视)上的表现是否一致?

2 回复

有两个方案来实现list的拖拽:

1、通过拖拽onDragStart、onItemDrop结合来实现,具体参考: https://gitee.com/harmonyos-cases/cases/tree/master/CommonAppDevelopment/feature/dragandexchange#grid%E5%92%8Clist%E5%86%85%E6%8B%96%E6%8B%BD%E4%BA%A4%E6%8D%A2%E5%AD%90%E7%BB%84%E4%BB%B6%E4%BD%8D%E7%BD%AE

2、通过手势来实现,具体参考: https://gitee.com/harmonyos-cases/cases/tree/master/CommonAppDevelopment/feature/listexchange#%E5%88%97%E8%A1%A8%E9%A1%B9%E4%BA%A4%E6%8D%A2%E6%A1%88%E4%BE%8B

以下示例代码是使用手势实现的简单效果:

import curves from '@ohos.curves'
import Curves from '@ohos.curves'

// xxx.ets
@Entry
@Component
struct ListItemExample {
  @State private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  @State dragItem: number = -1
  @State scaleItem: number = -1
  @State neighborItem: number = -1
  @State neighborScale: number = -1
  private dragRefOffset: number = 0
  @State offsetX: number = 0
  @State offsetY: number = 0
  private ITEM_INTV: number = 120

  scaleSelect(item: number): number {
    if (this.scaleItem == item) {
      return 1.05
    } else if (this.neighborItem == item) {
      return this.neighborScale
    } else {
      return 1
    }
  }

  itemMove(index: number, newIndex: number): void {
    let tmp = this.arr.splice(index, 1)
    this.arr.splice(newIndex, 0, tmp[0])
  }

  build() {
    Stack() {
      List({ space: 20, initialIndex: 0 }) {
        ForEach(this.arr, (item: number) => {
          ListItem() {
            Text('' + item)
              .width('100%')
              .height(100)
              .fontSize(16)
              .textAlign(TextAlign.Center)
              .borderRadius(10)
              .backgroundColor(0xFFFFFF)
              .shadow(this.scaleItem == item ? {
                radius: 70,
                color: '#15000000',
                offsetX: 0,
                offsetY: 0
              } :
                {
                  radius: 0,
                  color: '#15000000',
                  offsetX: 0,
                  offsetY: 0
                })
              .animation({ curve: Curve.Sharp, duration: 300 })
          }
          .margin({ left: 12, right: 12 })
          .scale({ x: this.scaleSelect(item), y: this.scaleSelect(item) })
          .zIndex(this.dragItem == item ? 1 : 0)
          .translate(this.dragItem == item ? { y: this.offsetY } : { y: 0 })
          .gesture(
            // 以下组合手势为顺序识别,当长按手势事件未正常触发时则不会触发拖动手势事件
            GestureGroup(GestureMode.Sequence,
              LongPressGesture({ repeat: true })
                .onAction((event?: GestureEvent) => {
                  animateTo({ curve: Curve.Friction, duration: 300 }, () => {
                    this.scaleItem = item
                  })
                })
                .onActionEnd(() => {
                  animateTo({ curve: Curve.Friction, duration: 300 }, () => {
                    this.scaleItem = -1
                  })
                }),
              PanGesture({ fingers: 1, direction: null, distance: 0 })
                .onActionStart(() => {
                  this.dragItem = item
                  this.dragRefOffset = 0
                })
                .onActionUpdate((event: GestureEvent) => {
                  this.offsetY = event.offsetY - this.dragRefOffset
                  // console.log('Y:' + this.offsetY.toString())
                  this.neighborItem = -1
                  let index = this.arr.indexOf(item)
                  let curveValue = Curves.initCurve(Curve.Sharp)
                  let value: number = 0
                  //根据位移计算相邻项的缩放
                  if (this.offsetY < 0) {
                    value = curveValue.interpolate(-this.offsetY / this.ITEM_INTV)
                    this.neighborItem = this.arr[index-1]
                    this.neighborScale = 1 - value / 20;
                    console.log('neighborScale:' + this.neighborScale.toString())
                  } else if (this.offsetY > 0) {
                    value = curveValue.interpolate(this.offsetY / this.ITEM_INTV)
                    this.neighborItem = this.arr[index+1]
                    this.neighborScale = 1 - value / 20;
                  }
                  //根据位移交换排序
                  if (this.offsetY > this.ITEM_INTV / 2) {
                    animateTo({ curve: curves.interpolatingSpring(0, 1, 400, 38) }, () => {
                      this.offsetY -= this.ITEM_INTV
                      this.dragRefOffset += this.ITEM_INTV
                      this.itemMove(index, index + 1)
                    })
                  } else if (this.offsetY < -this.ITEM_INTV / 2) {
                    animateTo({ curve: curves.interpolatingSpring(0, 1, 400, 38) }, () => {
                      this.offsetY += this.ITEM_INTV
                      this.dragRefOffset -= this.ITEM_INTV
                      this.itemMove(index, index - 1)
                    })
                  }
                })
                .onActionEnd((event: GestureEvent) => {
                  animateTo({ curve: curves.interpolatingSpring(0, 1, 400, 38) }, () => {
                    this.dragItem = -1
                    this.neighborItem = -1
                  })
                  animateTo({
                    curve: curves.interpolatingSpring(14, 1, 170, 17), delay: 150
                  }, () => {
                    this.scaleItem = -1
                  })
                })
            )
              .onCancel(() => {
                animateTo({ curve: curves.interpolatingSpring(0, 1, 400, 38) }, () => {
                  this.dragItem = -1
                  this.neighborItem = -1
                })
                animateTo({
                  curve: curves.interpolatingSpring(14, 1, 170, 17), delay: 150
                }, () => {
                  this.scaleItem = -1
                })
              })
          )
        }, (item: number) => item.toString())
      }
    }.width('100%').height('100%').backgroundColor(0xDCDCDC).padding({ top: 5 })
  }
}

List和Grid组件在不同设备(如手机、平板、电视)上的表现需要适配

更多关于HarmonyOS 鸿蒙Next中如何实现支持拖拽排序的List组件并允许用户自定义列表顺序 List和Grid组件在不同设备上表现是否一致的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS 鸿蒙Next中,实现支持拖拽排序的List组件并允许用户自定义列表顺序,通常需要通过自定义组件和事件监听来实现。你可以利用鸿蒙提供的组件框架,通过监听用户的拖拽动作(如触摸开始、移动、结束事件),动态调整List组件中Item的位置。这涉及到对UI布局的重排和数据模型的更新。

至于List和Grid组件在不同设备上表现是否一致,这主要取决于组件的适配性和设备的屏幕尺寸、分辨率等因素。鸿蒙系统提供了响应式布局和自适应UI设计的能力,旨在确保组件在不同设备上能够保持一致的用户体验。然而,开发者在设计组件时仍需考虑设备的多样性,进行适当的布局调整和测试,以确保List和Grid组件在不同设备上都能正确显示和交互。

总结来说,实现拖拽排序的List组件需要自定义事件处理和UI更新逻辑,而List和Grid组件在不同设备上的表现一致性则依赖于良好的适配和测试。如果问题依旧没法解决请联系官网客服,官网地址是 https://www.itying.com/category-93-b0.html

回到顶部