145 Star 1.4K Fork 1.7K

OpenHarmony/applications_app_samples

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

Grid和List内拖拽交换子组件位置

介绍

本示例分别通过onItemDrop()和onDrop()回调,实现子组件在Grid和List中的子组件位置交换。

效果图预览

使用说明:

  1. 拖拽Grid中子组件,到目标Grid子组件位置,进行两者位置互换。
  2. 拖拽List中子组件,到目标List子组件位置,进行两者位置互换。

实现思路

  1. 在Grid组件中,通过editMode()打开编辑模式、通过onItemDragStart()指定拖拽时样式、通过onItemDrop()指定拖拽释放时的行为。源码参考GridSceneView.ets

    Grid() { ... }
    .editMode(true) // 设置Grid进入编辑模式
    .onItemDragStart((event: ItemDragInfo, itemIndex: number) => { // 设置拖拽过程中显示的图形
      this.movedItem = this.appInfoList[itemIndex]; // 记录原位置子组件信息
      return this.itemWhileDrag(); 
    })
    .onItemDrop((event: ItemDragInfo, itemIndex: number, insertIndex: number, isSuccess: boolean) => { // 拖拽释放时,触发回调
      // isSuccess=false时,说明drop的位置在grid外部;insertIndex > length时,说明有新增元素的事件发生
      if (isSuccess && insertIndex < this.appInfoList.length) {
        this.changeIndex(itemIndex, insertIndex); // 互换子组件index值
      }
    })
    
  2. 在List组件中,通过ListItem的onDragStart()方法指定拖拽开始时的行为,通过List的onTouch()指定拖拽释放时的行为。源码参考ListSceneView.ets

    List({ space: LIST_SPACE }) {
      ForEach(this.appInfoList, (item: AppInfo, index) => {
        ListItem() { ... }
        .onDragStart(() => {
          item.visible = false; // 拖拽时,设置子组件原位置图标不可见
        })
        .onTouch((event: TouchEvent) => { // 拖拽释放时,记录目标位置子组件index值
          if (event.type === TouchType.Down) {
            this.dragIndex = index;
          }
        })
      })
    }
    .onDrop((event: DragEvent, extraParams: string) => {
      let jsonString: JsonObjType = JSON.parse(extraParams) as JsonObjType; // 通过参数extraParams获取原位置子组件index值
      this.changeIndex(this.dragIndex, jsonString.insertIndex); // 互换子组件index值
      this.appInfoList[jsonString.insertIndex].visible = true; // 完成互换后,设置子组件原位置图标不可见
    })
    

高性能知识点

不涉及

工程结构&模块类型

dragandexchange                  // har类型               
|---pages                                       
|---|---Launcher.ets             // 页面层-方案主页面
|---view                                        
|---|---GridSceneView.ets        // 视图层-Grid拖拽页面             
|---|---ListSceneView.ets        // 视图层-List拖拽页面  
|---model                                     
|---|---AppInfo.ets              // 模型层-App信息模型  

模块依赖

不涉及

参考资料

  1. 创建网格(Grid/GridItem)
  2. List

相关权限

不涉及

约束与限制

1.本示例仅支持在标准系统上运行,支持设备:RK3568。

2.本示例为Stage模型,支持API12版本SDK,SDK版本号(API Version 12 Release)。

3.本示例需要使用DevEco Studio 5.0.0 Release 才可编译运行。

下载

如需单独下载本工程,执行如下命令:

git init
git config core.sparsecheckout true
echo code/UI/DragAndExchange/ > .git/info/sparse-checkout
git remote add origin https://gitee.com/openharmony/applications_app_samples.git
git pull origin master
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/openharmony/applications_app_samples.git
git@gitee.com:openharmony/applications_app_samples.git
openharmony
applications_app_samples
applications_app_samples
master

搜索帮助