一个页面同时设置多个Grid组件时,多个Grid之间会互相响应拖拽事件导致拖拽异常,即使另一个Grid设置不支持编辑,也会响应另一个Grid拖拽事件。
@Entry @Component struct GridTestPage { @State numbers: string[] = [] @State numbers1: string[] = [] scroller: Scroller = new Scroller() scroller1: Scroller = new Scroller() @State text: string = 'drag' @Builder pixelMapBuilder() { // 拖拽过程样式 Column() { Text(this.text) .fontSize(16) .backgroundColor(0xF9CF93) .width(80) .height(80) .textAlign(TextAlign.Center) } } aboutToAppear() { for (let i = 1; i <= 15; i++) { this.numbers.push(i + '') this.numbers1.push(i + 'a') } } changeIndex(index1: number, index2: number) { // 交换数组位置 let temp: string; temp = this.numbers[index1]; this.numbers[index1] = this.numbers[index2]; this.numbers[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 .supportAnimation(true) .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 } this.changeIndex(itemIndex, insertIndex) }) Grid(this.scroller1) { ForEach(this.numbers1, (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(false) // 设置Grid是否进入编辑模式,进入编辑模式可以拖拽Grid组件内部GridItem .supportAnimation(true) .margin({ top: 30 }) .onItemDragStart((event: ItemDragInfo, itemIndex: number) => { // 第一次拖拽此事件绑定的组件时,触发回调。 }) .onItemDrop((event: ItemDragInfo, itemIndex: number, insertIndex: number, isSuccess: boolean) => { // 绑定此事件的组件可作为拖拽释放目标,当在本组件范围内停止拖拽行为时,触发回调。 }) }.width('100%').margin({ top: 5 }) } }
问题效果预览:

拖拽第一个Grid组件的第5个GridItem到第二Grid组件区域,数据没有交换成功的同时第一个组件的5消失不见。
Grid组件是一种网格容器,由“行”和“列”分割的单元格所组成,通过指定“项目”所在的单元格做出各种各样的布局。在实现以上功能之前需要先了解基本的Grid组件事件以及官方基础参考示例: 示例5(Grid拖拽场景)。
@State whoIsStart: number = -1; // 两个Grid的onItemDragStart触发时分别赋值为0和1
方式一:构造两个交换数组分别对指定的Grid组件进行数据交换。
changeIndex(index1: number, index2: number) { let temp: string = 'changeIndex'; if (this.whoIsStart === 0 && index2 !== -1) { temp = this.numbers[index1]; } console.info(`temp:${temp}`); this.whoIsStart = -1; } // 交换数组位置。 changeIndex1(index1: number, index2: number) { let temp: string = 'changeIndex1'; if (this.whoIsStart === 1 && index2 !== -1) { temp = this.numbers1[index1]; } console.info(`temp:${temp}`); this.whoIsStart = -1; }
方式二:设置一个交换函数内部判定是否属于同一个Grid的数据来源。
changeIndex(index1: number, index2: number) { let numberArr: Array<string> = this.whoIsStart === 0 ? this.numbers : this.numbers1; let temp: string; temp = numberArr[index1] + ""; if (this.whoIsStart === 0 && index2 !== -1) { this.numbers[index1] = numberArr[index2]; this.numbers[index2] = temp; } else if (this.whoIsStart === 1 && index2 !== -1) { this.numbers1[index1] = numberArr[index2]; this.numbers1[index2] = temp; } this.changeData = []; this.text = ""; this.whoIsStart = -1; }
// 交换数组位置。 @Entry @Component struct ChangeIndexOne { @State whoIsStart: number = -1; @State numbers: string[] = []; @State numbers1: string[] = []; changeIndex(index1: number, index2: number) { let temp: string = 'changeIndex'; if (this.whoIsStart === 0 && index2 !== -1) { temp = this.numbers[index1]; } console.info(`temp:${temp}`); this.whoIsStart = -1; } // 交换数组位置。 changeIndex1(index1: number, index2: number) { let temp: string = 'changeIndex1'; if (this.whoIsStart === 1 && index2 !== -1) { temp = this.numbers1[index1]; } console.info(`temp:${temp}`); this.whoIsStart = -1; } build() { Column() { Grid() { } .onItemDrop((event: ItemDragInfo, itemIndex: number, insertIndex: number, isSuccess: boolean) => { if (!isSuccess || insertIndex >= this.numbers.length) { return; } // 判定是否是同一个Grid在交换数据,不是同一个执行返回。 if (this.whoIsStart !== 0) { return; } this.changeIndex(itemIndex, insertIndex); console.info(`event:${event},itemIndex:${itemIndex},isSuccess:${isSuccess}`); }); Grid() { } .onItemDrop((event: ItemDragInfo, itemIndex: number, insertIndex: number, isSuccess: boolean) => { if (!isSuccess || insertIndex >= this.numbers.length) { return; } // 判定是否是同一个Grid在交换数据,不是同一个执行返回。 if (this.whoIsStart !== 0) { return; } this.changeIndex1(itemIndex, insertIndex); console.info(`event:${event},itemIndex:${itemIndex},isSuccess:${isSuccess}`); }); }; } }
@ObservedV2 class GridInfo { @Trace numbers: string[] = []; @Trace scroller: Scroller; constructor(numbers: string[]) { this.numbers = numbers; this.scroller = new Scroller(); } }
由于封装为类之后,涉及深层次的嵌套。此处可以采用ObservedV2或Observed监控对象,当对象属性发生改变时可以刷新UI。
@ObservedV2 class GridInfo { @Trace numbers: string[] = []; @Trace scroller: Scroller; constructor(numbers: string[]) { this.numbers = numbers; this.scroller = new Scroller(); } } @Entry @Component struct GridTestPageOne { @State whoIsStart: number = 0; @State numbers: string[] = []; @State numbersArr: Array<GridInfo> = []; @State text: string = 'drag'; // 拖拽过程样式。 @Builder pixelMapBuilder() { Column() { Text(this.text) .fontSize(16) .backgroundColor(0xF9CF93) .width(30) .height(30) .textAlign(TextAlign.Center); }; } aboutToAppear() { // 第一个Grid数据。 for (let i = 1; i <= 9; i++) { this.numbers.push(i + ''); } this.numbersArr.push(new GridInfo(this.numbers)); this.numbers = []; // 置空。 // 第二个Grid数据。 for (let i = 1; i <= 9; i++) { this.numbers.push(i + 'a'); } this.numbersArr.push(new GridInfo(this.numbers)); this.numbers = []; // 置空。 // 第三个Grid数据。 for (let i = 1; i <= 9; i++) { this.numbers.push(i + 'b'); } this.numbersArr.push(new GridInfo(this.numbers)); } changeIndex(index1: number, index2: number) { // 由于执行函数前已经判定过this.whoIsStart,所以不需要再次判定this.whoIsStart超出数组索引范围。 if (index2 !== -1) { let temp: string = this.numbersArr[this.whoIsStart].numbers[index1]; this.numbersArr[this.whoIsStart].numbers[index1] = this.numbersArr[this.whoIsStart].numbers[index2]; this.numbersArr[this.whoIsStart].numbers[index2] = temp; } this.text = ''; this.whoIsStart = -1; // 交换完成后重新赋值为-1等待下次数据交换。 } build() { Column({ space: 5 }) { ForEach(this.numbersArr, (item: GridInfo, indexGrid: number) => { Grid(item.scroller) { ForEach(item.numbers, (day: string) => { GridItem() { Text(day) .fontSize(16) .backgroundColor(0xF9CF93) .width(60) .height(60) .textAlign(TextAlign.Center); }; }); } .columnsTemplate('1fr 1fr 1fr') .columnsGap(10) .rowsGap(10) .width(250) .backgroundColor(0xFAEEE0) .height(250) .editMode(true) // 设置Grid是否进入编辑模式,进入编辑模式可以拖拽Grid组件内部GridItem。 .onItemDragStart((event: ItemDragInfo, itemIndex: number) => { console.info(`event:${event}`); this.text = item.numbers[itemIndex]; this.whoIsStart = indexGrid; // 拖动时将第几个Grid组件在进行拖动,赋值给判定变量。 return this.pixelMapBuilder(); // 设置拖拽过程中显示的图片。 }) .onItemDragMove(() => { }) .onItemDrop((event: ItemDragInfo, itemIndex: number, insertIndex: number, isSuccess: boolean) => { if (!isSuccess || insertIndex >= item.numbers.length) { return; } // 判定是不是同一个Grid不可进行数据交换。 if (this.whoIsStart !== indexGrid) { return; } this.changeIndex(itemIndex, insertIndex); // 同一个Grid执行数据交换。 console.info(`event:${event},itemIndex:${itemIndex}`); }); }); }.width('100%').margin({ top: 5 }); } }
效果预览:

@State changeData: Array<number> = []; // 存储需要交换的数据。 @State whoIsStart: number = -1; // 两个Grid的onItemDragStart触发时分别赋值为0和1
@ObservedV2 class GridInfo1 { @Trace numbers: string[] = []; @Trace scroller: Scroller; constructor(numbers: string[]) { this.numbers = numbers; this.scroller = new Scroller(); } } @Entry @Component struct GridTestPageTwo { @State changeData: Array<number> = []; // 存储需要交换的数据。 @State whoIsStart: number = -1; // 两个Grid的onItemDragStart触发时分别赋值为0和1 @State whoIsEnd: number = -1; @State numbers: string[] = []; @State numbersArr: Array<GridInfo1> = []; @State text: string = 'drag'; // 拖拽过程样式 @Builder pixelMapBuilder() { Column() { Text(this.text) .fontSize(16) .backgroundColor(0xF9CF93) .width(30) .height(30) .textAlign(TextAlign.Center); }; } aboutToAppear() { // 第一个Grid数据。 for (let i = 1; i <= 9; i++) { this.numbers.push(i + ''); } this.numbersArr.push(new GridInfo1(this.numbers)); this.numbers = []; // 置空。 // 第二个Grid数据。 for (let i = 1; i <= 9; i++) { this.numbers.push(i + 'a'); } this.numbersArr.push(new GridInfo1(this.numbers)); this.numbers = []; // 置空。 // 第三个Grid数据。 for (let i = 1; i <= 9; i++) { this.numbers.push(i + 'b'); } this.numbersArr.push(new GridInfo1(this.numbers)); } changeIndex(index1: number, index2: number) { // 由于执行函数前已经判定过this.whoIsStart,无需再次判定this.whoIsStart超出数组索引范围。 if (index2 !== -1 && this.whoIsStart !== -1 && this.whoIsEnd !== -1) { let temp: string = this.numbersArr[this.whoIsStart].numbers[index1]; this.numbersArr[this.whoIsStart].numbers[index1] = this.numbersArr[this.whoIsEnd].numbers[index2]; this.numbersArr[this.whoIsEnd].numbers[index2] = temp; } this.text = ''; this.changeData = []; // 存储置空。 this.whoIsStart = -1; // 交换完成后重新赋值为-1等待下次数据交换。 this.whoIsEnd = -1; // 交换完成后重新赋值为-1等待下次数据交换。 } build() { Column({ space: 5 }) { ForEach(this.numbersArr, (item: GridInfo1, indexGrid: number) => { Grid(item.scroller) { ForEach(item.numbers, (day: string) => { GridItem() { Text(day) .fontSize(16) .backgroundColor(0xF9CF93) .width(60) .height(60) .textAlign(TextAlign.Center); }; }); } .columnsTemplate('1fr 1fr 1fr') .columnsGap(10) .rowsGap(10) .width(250) .backgroundColor(0xFAEEE0) .height(250) .editMode(true) // 设置Grid是否进入编辑模式,进入编辑模式可以拖拽Grid组件内部GridItem。 .onItemDragStart((event: ItemDragInfo, itemIndex: number) => { console.info(`event:${event}`); this.text = item.numbers[itemIndex]; this.whoIsStart = indexGrid; // 拖动时将第几个Grid组件在进行拖动,赋值给判定变量。 this.changeData[0] = itemIndex; return this.pixelMapBuilder(); }) .onItemDragMove(() => { }) .onItemDrop((event: ItemDragInfo, itemIndex: number, insertIndex: number, isSuccess: boolean) => { if (!isSuccess || insertIndex >= item.numbers.length) { return; } if (insertIndex === -1) { return; } this.whoIsEnd = indexGrid; // 插入时将第几个Grid组件在进行插入,赋值给判定变量。 this.changeData[1] = insertIndex; this.changeIndex(this.changeData[0], this.changeData[1]); // 不同Grid执行数据交换。 console.info(`event:${event},itemIndex:${itemIndex}`); }); }); }.width('100%').margin({ top: 5 }); } }
效果预览:
