HarmonyOS 同一个父组件中的其中一个grid拖拽其中item 怎么避免影响其他的grid?

HarmonyOS 同一个父组件中的其中一个grid拖拽其中item 怎么避免影响其他的grid?

HarmonyOS
2024-10-10 10:44:29
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

请排查使用的数据源,以及定义的onItemDragStart() onItemDrop()方法是否会同时影响两个grid,正常拖拽情况下不会相互干扰,参考demo:

@Entry  
@Component  
struct GridExample {  
  @State numbers: string[] = []  
  @State numbers2: string[] = []  
  scroller: Scroller = new Scroller()  
  scroller2: Scroller = new Scroller()  
  @State text: string = 'drag'  
  @Builder  
  pixelMapBuilder() { //拖拽过程样式  
    Column() {  
      Text(this.text)  
        .fontSize(16)  
        .fontColor(Color.White)  
        .backgroundColor(Color.Black)  
        .width(80)  
        .height(80)  
        .textAlign(TextAlign.Center)  
    }  
  }  
  aboutToAppear() {  
    for (let i = 1; i <= 9; i++) {  
      this.numbers.push(i + '')  
    }  
    for (let i = 1; i <= 9; i++) {  
      this.numbers2.push('second' + i)  
    }  
  }  
  changeIndex(arr: string[], index1: number, index2: number) { //交换数组位置  
    let temp: string;  
    temp = arr[index1];  
    arr[index1] = arr[index2];  
    arr[index2] = temp;  
  }  
  build() {  
    Column({ space: 5 }) {  
      Grid(this.scroller) {  
        ForEach(this.numbers, (day: string) => {  
          GridItem() {  
            Text(day)  
              .fontSize(16)  
              .backgroundColor(0xF9CF93)  
              .width(80)  
              .height(80)  
              .textAlign(TextAlign.Center)  
          }  
        })  
      }  
      .columnsTemplate('1fr 1fr 1fr')  
      .columnsGap(10)  
      .rowsGap(10)  
      .width('90%')  
      .backgroundColor(0xFAEEE0)  
      .height(300)  
      .editMode(true) //设置Grid是否进入编辑模式,进入编辑模式可以拖拽Grid组件内部GridItem  
      .margin({ bottom: 20 })  
      .onItemDragStart((event: ItemDragInfo, itemIndex: number) => { //第一次拖拽此事件绑定的组件时,触发回调。  
        this.text = this.numbers[itemIndex]  
        return this.pixelMapBuilder() //设置拖拽过程中显示的图片。  
      })  
      .onItemDrop((event: ItemDragInfo, itemIndex: number, insertIndex: number,  
        isSuccess: boolean) => { //绑定此事件的组件可作为拖拽释放目标,当在本组件范围内停止拖拽行为时,触发回调。  
        // isSuccess=false时,说明drop的位置在grid外部;insertIndex > length时,说明有新增元素的事件发生  
        if (!isSuccess || insertIndex >= this.numbers.length) {  
          return  
        }  
        console.info('beixiang' + itemIndex + '', insertIndex + '') //itemIndex拖拽起始位置,insertIndex拖拽插入位置  
        this.changeIndex(this.numbers, itemIndex, insertIndex)  
      })  
  Grid(this.scroller2) {  
    ForEach(this.numbers2, (day: string) => {  
      GridItem() {  
        Text(day)  
          .fontSize(16)  
          .backgroundColor(Color.Pink)  
          .width(80)  
          .height(80)  
          .textAlign(TextAlign.Center)  
      }  
    })  
  }  
  .columnsTemplate('1fr 1fr 1fr')  
  .columnsGap(10)  
  .rowsGap(10)  
  .width('90%')  
  .backgroundColor(0xFAEEE0)  
  .height(300)  
  .editMode(true)  
  .onItemDragStart((event: ItemDragInfo, itemIndex: number) => {  
    this.text = this.numbers2[itemIndex]  
    return this.pixelMapBuilder() //设置拖拽过程中显示的图片。  
  })  
  .onItemDrop((event: ItemDragInfo, itemIndex: number, insertIndex: number,  
    isSuccess: boolean) => {  
    if (!isSuccess || insertIndex >= this.numbers2.length) {  
      return  
    }  
    console.info('beixiang' + itemIndex + '', insertIndex + '')  
    this.changeIndex(this.numbers2, itemIndex, insertIndex)  
  })  
}.width('100%').margin({ 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.
分享
微博
QQ
微信
回复
2024-10-10 16:46:36
相关问题
同一个HSP,router.pushUrlurl问题
1281浏览 • 1回复 待解决
HarmonyOS 多module同时依赖同一个har
1812浏览 • 1回复 待解决