| Name | Date | Size | #Lines | LOC | ||
|---|---|---|---|---|---|---|
| .. | - | - | ||||
| AppScope/ | 22-Oct-2025 | - | 35 | 32 | ||
| casesfeature/dragandexchange/ | 22-Oct-2025 | - | 891 | 814 | ||
| entry/ | 22-Oct-2025 | - | 605 | 544 | ||
| hvigor/ | 22-Oct-2025 | - | 38 | 36 | ||
| .gitignore | D | 22-Oct-2025 | 156 | 13 | 13 | |
| README.md | D | 22-Oct-2025 | 4 KiB | 105 | 78 | |
| build-profile.json5 | D | 22-Oct-2025 | 1.4 KiB | 61 | 60 | |
| code-linter.json5 | D | 22-Oct-2025 | 958 | 35 | 34 | |
| hvigorfile.ts | D | 22-Oct-2025 | 843 | 22 | 5 | |
| oh-package.json5 | D | 22-Oct-2025 | 809 | 26 | 24 | |
| ohosTest.md | D | 22-Oct-2025 | 807 | 9 | 6 |
README.md
1# Grid和List内拖拽交换子组件位置 2 3### 介绍 4 5本示例分别通过onItemDrop()和onDrop()回调,实现子组件在Grid和List中的子组件位置交换。 6 7 8### 效果图预览 9 10<img src="casesfeature/dragandexchange/drag_and_exchange.gif" width="300"> 11 12**使用说明:** 13 141. 拖拽Grid中子组件,到目标Grid子组件位置,进行两者位置互换。 152. 拖拽List中子组件,到目标List子组件位置,进行两者位置互换。 16 17### 实现思路 18 191. 在Grid组件中,通过editMode()打开编辑模式、通过onItemDragStart()指定拖拽时样式、通过onItemDrop()指定拖拽释放时的行为。源码参考[GridSceneView.ets](casesfeature/dragandexchange/src/main/ets/view/GridSceneView.ets)。 20 21 ```ts 22 Grid() { ... } 23 .editMode(true) // 设置Grid进入编辑模式 24 .onItemDragStart((event: ItemDragInfo, itemIndex: number) => { // 设置拖拽过程中显示的图形 25 this.movedItem = this.appInfoList[itemIndex]; // 记录原位置子组件信息 26 return this.itemWhileDrag(); 27 }) 28 .onItemDrop((event: ItemDragInfo, itemIndex: number, insertIndex: number, isSuccess: boolean) => { // 拖拽释放时,触发回调 29 // isSuccess=false时,说明drop的位置在grid外部;insertIndex > length时,说明有新增元素的事件发生 30 if (isSuccess && insertIndex < this.appInfoList.length) { 31 this.changeIndex(itemIndex, insertIndex); // 互换子组件index值 32 } 33 }) 34 ``` 35 362. 在List组件中,通过ListItem的onDragStart()方法指定拖拽开始时的行为,通过List的onTouch()指定拖拽释放时的行为。源码参考[ListSceneView.ets](casesfeature/dragandexchange/src/main/ets/view/ListSceneView.ets)。 37 38 ```ts 39 List({ space: LIST_SPACE }) { 40 ForEach(this.appInfoList, (item: AppInfo, index) => { 41 ListItem() { ... } 42 .onDragStart(() => { 43 item.visible = false; // 拖拽时,设置子组件原位置图标不可见 44 }) 45 .onTouch((event: TouchEvent) => { // 拖拽释放时,记录目标位置子组件index值 46 if (event.type === TouchType.Down) { 47 this.dragIndex = index; 48 } 49 }) 50 }) 51 } 52 .onDrop((event: DragEvent, extraParams: string) => { 53 let jsonString: JsonObjType = JSON.parse(extraParams) as JsonObjType; // 通过参数extraParams获取原位置子组件index值 54 this.changeIndex(this.dragIndex, jsonString.insertIndex); // 互换子组件index值 55 this.appInfoList[jsonString.insertIndex].visible = true; // 完成互换后,设置子组件原位置图标不可见 56 }) 57 ``` 58 59### 高性能知识点 60 61**不涉及** 62 63### 工程结构&模块类型 64 65``` 66dragandexchange // har类型 67|---pages 68|---|---Launcher.ets // 页面层-方案主页面 69|---view 70|---|---GridSceneView.ets // 视图层-Grid拖拽页面 71|---|---ListSceneView.ets // 视图层-List拖拽页面 72|---model 73|---|---AppInfo.ets // 模型层-App信息模型 74``` 75 76 77### 模块依赖 78 79不涉及 80 81### 参考资料 82 831. [创建网格(Grid/GridItem)](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/reference/apis-arkui/arkui-ts/ts-container-grid.md) 842. [List](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/reference/apis-arkui/arkui-ts/ts-container-list.md) 85 86### 相关权限 87不涉及 88 89### 约束与限制 901.本示例仅支持在标准系统上运行,支持设备:RK3568。 91 922.本示例为Stage模型,支持API12版本SDK,SDK版本号(API Version 12 Release)。 93 943.本示例需要使用DevEco Studio 5.0.0 Release 才可编译运行。 95 96### 下载 97如需单独下载本工程,执行如下命令: 98 99``` 100git init 101git config core.sparsecheckout true 102echo code/UI/DragAndExchange/ > .git/info/sparse-checkout 103git remote add origin https://gitee.com/openharmony/applications_app_samples.git 104git pull origin master 105```