HarmonyOS 鸿蒙Next排序使用什么组件

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

HarmonyOS 鸿蒙Next排序使用什么组件 见图片,点击可以上移/下移改变顺序,怎么达到排序的效果

图片


更多关于HarmonyOS 鸿蒙Next排序使用什么组件的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

官方没有现成的组件,可以参照下面代码自定义组件去完成此功能

import curves from '@ohos.curves';
import animator, { AnimatorResult } from '@ohos.animator';

class ListItemModify implements AttributeModifier<ListItemAttribute> {
  public hasShadow: boolean = false
  public scale: number = 1
  public offsetY: number = 0
  private scroll: ListScroller = new ListScroller()
  public color:string = '#00000000'
  applyNormalAttribute(instance: ListItemAttribute): void {
    if (this.hasShadow) {
      instance.shadow({ radius: 70, color: this.color, offsetX: 0, offsetY: 0 })
      instance.zIndex(1)
    }
    instance.scale({ x: this.scale, y: this.scale })
    instance.translate({ y: this.offsetY })
  }
}

enum DragSortState {
  IDLE,
  PRESSING,
  MOVING,
  DROPPING,
}

@Observed
class DragSortCtrl<T> {
  private arr: Array<T>
  private modify: Array<ListItemModify>
  private dragRefOffset: number = 0
  offsetY: number = 0
  state:DragSortState = DragSortState.IDLE
  private ITEM_INTV: number = 120
  constructor(arr:Array<T>, intv:number) {
    this.arr = arr;
    this.modify = new Array<ListItemModify>()
    this.ITEM_INTV = intv
    arr.forEach(()=>{
      this.modify.push(new ListItemModify())
    })
  }
  itemMove(index: number, newIndex: number): void {
    let tmp = this.arr.splice(index, 1)
    this.arr.splice(newIndex, 0, tmp[0])
    let tmp2 = this.modify.splice(index, 1)
    this.modify.splice(newIndex, 0, tmp2[0])
  }
  onLongPress(item: T): void {
    let index = this.arr.indexOf(item)
    this.dragRefOffset = 0
    animateTo({ curve: Curve.Friction, duration: 300 }, () => {
      this.state = DragSortState.PRESSING
      this.modify[index].hasShadow = true
      this.modify[index].color ='#15000000'
      this.modify[index].scale = 1.05
    })
  }
  onDrop(item: T):void {
    let index = this.arr.indexOf(item)
    animateTo({ duration: 300, curve: Curve.Sharp }, () => {
      this.modify[index].color = '#00000000'
      this.modify[index].hasShadow = false
      if (index < this.modify.length - 1) {
        this.modify[index + 1].scale = 1;
      }
      if (index > 0) {
        this.modify[index - 1].scale = 1;
      }
    })
    animateTo({curve: curves.interpolatingSpring(14, 1, 170, 17)}, () => {
      this.state = DragSortState.IDLE
      this.modify[index].scale = 1
    })
  }
  onMove(item: T, offset: number) {
    this.state = DragSortState.MOVING
    this.offsetY = offset - this.dragRefOffset
    let index = this.arr.indexOf(item)
    this.modify[index].offsetY = this.offsetY
    let curveValue = curves.initCurve(Curve.Sharp)
    if (this.offsetY < 0) {
      let value = curveValue.interpolate(-this.offsetY / this.ITEM_INTV)
      if (index < this.modify.length - 1) {
        this.modify[index + 1].scale = 1;
      }
      if (index > 0) {
        this.modify[index - 1].scale = 1 - value / 20;
      }
    } else if (this.offsetY > 0) {
      let value = curveValue.interpolate(this.offsetY / this.ITEM_INTV)
      if (index < this.modify.length - 1) {
        this.modify[index + 1].scale = 1 - value / 20;
      }
      if (index > 0) {
        this.modify[index - 1].scale = 1;
      }
    }
    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.modify[index].offsetY = this.offsetY
        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.modify[index].offsetY = this.offsetY
        this.itemMove(index, index - 1)
      })
    }
  }
  getModify(item:T):ListItemModify {
    let index = this.arr.indexOf(item)
    return this.modify[index]
  }
}

@Entry
@Component
struct ListAutoSortExample {
  @State private arr: Array<number> = [0, 1, 2, 3, 4, 5]
  @State dragSortCtrl: DragSortCtrl<number> = new DragSortCtrl<number>(this.arr, 120)
  private listScroll: ListScroller = new ListScroller()
  private backAnimator: AnimatorResult | null= null
  private dropFlag: Boolean = true
  @Builder
  itemEnd(item: number) {
    Row () {
      Button("To TOP").margin("4vp").onClick(() => {
        this.listScroll.closeAllSwipeActions({
          onFinish: () => {
            setTimeout(() => {
              let _this = this
              this.dropFlag = true
              this.dragSortCtrl.onLongPress(item)
              let length = 120 * this.arr.indexOf(item)
              this.backAnimator = animator.create({
                duration: 0,
                easing: "interpolating-spring(0, 1, 150, 24)",
                delay: 0,
                fill: "none",
                direction: "normal",
                iterations: 1,
                begin: 0,
                end: - length
              })
              this.backAnimator.onframe = (value) => {
                this.dragSortCtrl.onMove(item, value)
                if (Math.abs(value + length) < 50 && this.dropFlag){
                  this.dropFlag = false
                  this.dragSortCtrl.onDrop(item)
                }
              }
              this.backAnimator.onfinish = () => {
                this.dropFlag = true
              }
              this.backAnimator.play()
            })
          }
        })
      })
    }.padding("4vp").justifyContent(FlexAlign.SpaceEvenly)
  }
  
  build() {
    Row() {
      Column(){
        List({ space: 20, scroller: this.listScroll }) {
          ForEach(this.arr, (item: number) => {
            ListItem() {
              Text('' + item)
                .width('100%')
                .height(100)
                .fontSize(16)
                .borderRadius(10)
                .textAlign(TextAlign.Center)
                .backgroundColor(0xFFFFFF)
            }
            .swipeAction({
              end:this.itemEnd(item)
            })
            .clip(true)
            .attributeModifier(this.dragSortCtrl.getModify(item))
            .borderRadius(10)
            .margin({ left: 20, right: 20 })
          }, (item: number) => item.toString())
        }
        .padding({top:20})
        .height("100%")
      }
    }.backgroundColor(0xDCDCDC)
  }
}

更多关于HarmonyOS 鸿蒙Next排序使用什么组件的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


HarmonyOS(鸿蒙)系统中进行Next排序时,通常使用的是其内置的数据结构和算法组件。这些组件是鸿蒙系统为了高效处理数据排序等任务而专门设计的。在鸿蒙系统中,并不依赖于特定的编程语言(如Java或C语言)中的标准库函数来实现排序功能,而是采用更为底层和系统级的方法来处理。

具体到Next排序(这里假设是指一种特定的排序算法或排序场景,尽管“Next排序”并非一个标准的排序算法名称),鸿蒙可能会提供一个专门的排序组件或接口。这个组件可能封装了多种排序算法,如快速排序、归并排序等,并根据数据的特点和排序需求自动选择合适的算法。

开发者在使用鸿蒙系统进行开发时,可以通过调用这些系统级的排序组件来实现数据的排序功能。这些组件通常具有高效、稳定、易于集成等特点,能够满足各种复杂场景下的排序需求。

需要注意的是,由于鸿蒙系统的不断更新和演进,具体的排序组件和接口可能会有所变化。因此,开发者在使用时应参考最新的鸿蒙系统文档或API指南。

如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html

回到顶部