HarmonyOS list组件对子组件进行拖拽排序时出现cppCrash

HarmonyOS
2024-12-18 15:30:42
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Excelsior_abit

可以参考以下demo,拖动到最下方list跟随滑动代码:

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
  @State slide:boolean = false
  private listScroller: ListScroller = new ListScroller()
  // list组件规格
  private listArea: Area = {
    width: 0,
    height: 0,
    position: {},
    globalPosition: {}
  }
  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, scroller: this.listScroller }) {
        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;
                  }
                  //防止offsetY触发越界逻辑
                  if (index === 0) {
                    this.offsetY = Math.max(0, this.offsetY)
                  } else if (index === this.arr.length - 1) {
                    this.offsetY = Math.min(0, this.offsetY)
                  }
                  //获取手指信息
                  let fingerInfo = event.fingerList[0]
                  let clickPercentY = (fingerInfo.globalY - Number(this.listArea.globalPosi
  • 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.
分享
微博
QQ
微信
回复
2024-12-18 17:05:18
相关问题
HarmonyOS Grid组件拖拽排序
1177浏览 • 1回复 待解决
Grid组件中如何实现拖拽排序
289浏览 • 0回复 待解决
HarmonyOS 关于Grid组件拖拽排序的问题
1419浏览 • 1回复 待解决
HarmonyOS List组件如何实现拖动重排序
1145浏览 • 1回复 待解决
HarmonyOS GridRow 实现拖拽排序
703浏览 • 1回复 待解决
HarmonyOS 如何实现Listitem的拖拽排序
1003浏览 • 1回复 待解决
native使用fork函数,出现cppcrash
2326浏览 • 1回复 待解决
使用onBackPress对子窗口进行销毁
1807浏览 • 1回复 待解决
HarmonyOS List拖动排序示例
1057浏览 • 1回复 待解决
HarmonyOS image组件拖拽问题
1215浏览 • 1回复 待解决
HarmonyOS 组件拖拽如何实现
919浏览 • 1回复 待解决
HarmonyOS如何实现list listitem拖拽
1828浏览 • 1回复 待解决
HarmonyOS Image组件关闭可拖拽功能
531浏览 • 1回复 待解决
HarmonyOS List组件和WaterFlow组件增强
1600浏览 • 1回复 待解决