HarmonyOS 想实现列表滑动排序效果,请问有相关的组件和api提供吗?

HarmonyOS 想实现列表滑动排序效果,请问有相关的组件和api提供吗?

HarmonyOS
2024-10-12 09:37:42
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

请参考代码:

import curves from '@ohos.curves';  
// xxx.ets  
class ListItemModify implements AttributeModifier<ListItemAttribute> {  
  public hasShadow: boolean = false  
  public scale: number = 1  
  public offsetY: number = 0  
  applyNormalAttribute(instance: ListItemAttribute): void {  
    if (this.hasShadow) {  
      instance.shadow({ radius: 70, color: '#15000000', 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].scale = 1.05  
    })  
  }  
  onDrop(item: T):void {  
    let index = this.arr.indexOf(item)  
    this.dragRefOffset = 0  
    this.offsetY = 0  
    animateTo({ curve: curves.interpolatingSpring(0, 1, 400, 38) }, () => {  
      this.state = DragSortState.DROPPING  
      this.modify[index].hasShadow = false  
      this.modify[index].offsetY = 0  
      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), delay: 150}, ()=>{  
      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]  
  }  
}  
// xxx.ets  
@Entry  
@Component  
struct ListItemExample {  
  @State private arr: Array<number> = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]  
  @State dragSortCtrl: DragSortCtrl<number> = new DragSortCtrl<number>(this.arr, 120)  
  build() {  
    Stack() {  
      List({ space: 20, initialIndex: 0 }) {  
        ForEach(this.arr, (item: number) => {  
          ListItem() {  
            Text('' + item)  
              .width('100%')  
              .height(100)  
              .fontSize(16)  
              .textAlign(TextAlign.Center)  
              .backgroundColor(0xFFFFFF)  
          }  
          .clip(true)  
          .attributeModifier(this.dragSortCtrl.getModify(item))  
          .borderRadius(10)  
          .margin({ left: 12, right: 12 })  
          .gesture(  
            // 以下组合手势为顺序识别,当长按手势事件未正常触发时则不会触发拖动手势事件  
            GestureGroup(GestureMode.Sequence,  
              LongPressGesture({ repeat: false })  
                .onAction((event?: GestureEvent) => {  
                  this.dragSortCtrl.onLongPress(item)  
                }),  
              PanGesture({ fingers: 1, direction: null, distance: 0 })  
                .onActionUpdate((event: GestureEvent) => {  
                  this.dragSortCtrl.onMove(item, event.offsetY)  
                })  
                .onActionEnd((event: GestureEvent) => {  
                  this.dragSortCtrl.onDrop(item)  
                })  
            )  
              .onCancel(() => {  
                this.dragSortCtrl.onDrop(item)  
              })  
          )  
        }, (item: number) => item.toString())  
      }  
    }.width('100%').height('100%').backgroundColor(0xDCDCDC).padding({ top: 5 })  
  }  
}
  • 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.
分享
微博
QQ
微信
回复
2024-10-12 17:08:43
相关问题
拖动实现列表重新排序
1974浏览 • 1回复 待解决
HarmonyOS 请问提供md5算法库
1076浏览 • 1回复 待解决
滑动组件如何实现单边spring效果
1702浏览 • 1回复 待解决
请问流式布局组件
1009浏览 • 1回复 待解决
HarmonyOS 首页滑动置顶组件
807浏览 • 1回复 待解决
mysql 查询排序问题了解
2940浏览 • 1回复 待解决