HarmonyOS list的item支持托动效果吗?

HarmonyOS list的item支持托动效果吗?

HarmonyOS
2024-10-22 11:09:52
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

完整代码:

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 })  
  }  
}

下面是封装代码,方便移植:

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]  
  }  
}

封装代码使用样例:

@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 })  
  }  
}
分享
微博
QQ
微信
回复
2024-10-22 17:14:37
相关问题
HarmonyOS listitem交互效果处理
267浏览 • 1回复 待解决
HarmonyOS 项目支持gif
182浏览 • 1回复 待解决
Listitem点击变色有什么好方法
2053浏览 • 1回复 待解决
HarmonyOS List item 刷新问题
638浏览 • 1回复 待解决
HarmonyOS listitem如何保存状态
215浏览 • 2回复 待解决
HarmonyOSList是如何加载item
39浏览 • 0回复 待解决
list 支持循环滚动?
2126浏览 • 1回复 待解决
HarmonyOS list编辑移动效果
155浏览 • 1回复 待解决
怎么获取List里面每个itemposition?
196浏览 • 1回复 待解决
listadd跟remove item入场、出场动画
1087浏览 • 1回复 待解决
现在是否支持Webp图?
2215浏览 • 1回复 待解决
如何实现list折叠动画效果
399浏览 • 1回复 待解决
navigation 导航栏支持毛玻璃效果
688浏览 • 1回复 待解决
如何将List回弹效果改为阴影效果
363浏览 • 1回复 待解决
list-item 根据boolean属性 动态设置class
4749浏览 • 1回复 待解决
ListContainer中Item能取消拖拽
4978浏览 • 1回复 待解决