{"id":20846936,"url":"https://github.com/dsappteam/recyclerviewdrag","last_synced_at":"2025-05-09T03:16:40.007Z","repository":{"id":52890518,"uuid":"521139835","full_name":"DSAppTeam/RecyclerViewDrag","owner":"DSAppTeam","description":":white_check_mark: 基于RecyclerView实现的可拖拽/合并交互，比如顺序改变/合并成文件夹等","archived":false,"fork":false,"pushed_at":"2023-05-04T02:21:21.000Z","size":10589,"stargazers_count":46,"open_issues_count":1,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-09T03:16:34.496Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/DSAppTeam.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-08-04T05:49:48.000Z","updated_at":"2025-05-08T06:42:41.000Z","dependencies_parsed_at":"2023-01-23T12:45:32.914Z","dependency_job_id":null,"html_url":"https://github.com/DSAppTeam/RecyclerViewDrag","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DSAppTeam%2FRecyclerViewDrag","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DSAppTeam%2FRecyclerViewDrag/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DSAppTeam%2FRecyclerViewDrag/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DSAppTeam%2FRecyclerViewDrag/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DSAppTeam","download_url":"https://codeload.github.com/DSAppTeam/RecyclerViewDrag/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253181442,"owners_count":21866995,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-18T02:18:30.854Z","updated_at":"2025-05-09T03:16:39.988Z","avatar_url":"https://github.com/DSAppTeam.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"# RecyclerView拖拽合并实现方案\n\n## 简介\n\n基于ItemTouchHelper.Callback接口实现RecyclerView拖拽合并功能，对外提供拖拽处理接口可以根据自身业务需求自定义合并处理逻辑。\n\n提供Adapter和ViewHolder接口，只需要做少量的改动就可以让已有的RecyclerView支持合并Item的效果。\n\n## 效果演示\n\n1、拖拽应用方案\n\n\u003cimg src=\"https://github.com/DSAppTeam/RecyclerViewDrag/blob/master/Screenshot/ds_demo.gif\" width = \"360\" height = \"750\"\u003e\n\n2、合并成文件夹\n\n\u003cimg src=\"https://github.com/DSAppTeam/RecyclerViewDrag/blob/master/Screenshot/folder_demo.gif\" width = \"360\" height = \"750\"\u003e\n\n3、合并执行加法\n\n\u003cimg src=\"https://github.com/DSAppTeam/RecyclerViewDrag/blob/master/Screenshot/addition_demo.gif\" width = \"360\" height = \"750\"\u003e\n\n4、合并执行乘法\n\n\u003cimg src=\"https://github.com/DSAppTeam/RecyclerViewDrag/blob/master/Screenshot/multiply_demo.gif\" width = \"360\" height = \"750\"\u003e\n\n## 配置\n\n1. 在项目根目录下的build.gradle添加 JitPack 仓库\n\n   ```groovy\n    allprojects {\n        repositories {\n            maven { url 'https://jitpack.io' }\n        }\n    }\n   ```\n2. 在 **app** 目录下的build.gradle中添加依赖\n\n   ```groovy\n   dependencies {\n        implementation 'com.github.DSAppTeam:RecyclerViewDrag:1.0.6'\n   }\n   ```\n\n## 如何使用\n\n```kotlin\nval itemTouchCallback = DragTouchCallback(mAdapter)\n// 设置自定义的IDragHandler对象\nitemTouchCallback.setDragHandler(AdditionHandlerImpl(recyclerView, mAdapter))\nItemTouchHelper(itemTouchCallback).attachToRecyclerView(recyclerView)\n```\n\n步骤1：将自己的ViewHolder实现IDragItem接口。\n\n步骤2：将自己的Adapter实现IDragAdapter接口。\n\n步骤3：基于IDragHandler接口实现自己需要的合并处理逻辑。\n\n步骤4：将IDragHandler对象设置给DragTouchCallback，并完成对RecyclerView的绑定。\n\n## 类\u0026接口说明\n\n### IDragItem\n\n描述：拖拽Item接口，由RecyclerView的具体ViewHolder实现，用于判断拖动、合并和显示UI状态等\n\n\n| 方法名                                                                      | 描述             |\n| ----------------------------------------------------------------------------- | ------------------ |\n| canDrag(): Boolean                                                          | 是否可以拖动     |\n| canMerge(): Boolean                                                         | 是否可以合并     |\n| acceptMerge(): Boolean                                                      | 是否接收合并     |\n| showMergePreview(holder: RecyclerView.ViewHolder?, show: Boolean)           | 显示合并预览效果 |\n| showDragState(holder: RecyclerView.ViewHolder?, isCurrentlyActive: Boolean) | 显示拖动状态     |\n\n### IDragAdapter\n\n描述：拖动适配器，由RecyclerView的具体适配器实现\n\n\n| 方法名                                                        | 描述                                 |\n| --------------------------------------------------------------- | -------------------------------------- |\n| getDragData(): List\\\u003cAny\\\u003e                                    | 获取适配器列表数据                   |\n| getDragItem(viewHolder: RecyclerView.ViewHolder?): IDragItem? | 根据ViewHolder获取对应的DragItem对象 |\n\n### IDragHandler\n\n描述：拖拽处理接口，拖拽条件判断、回调监听、合并处理逻辑。接入拖拽功能时需要实现这个接口，并且将这个处理器通过DragTouchCallback#setDragHandler()赋值。\n\n\n| 方法名                                                    | 描述             |\n| ----------------------------------------------------------- | ------------------ |\n| swapPosition(fromPosition: Int, toPosition: Int): Boolean | 是否可以交换位置 |\n| onBeforeSwap(fromPosition: Int, toPosition: Int)          | 交换位置前回调   |\n| onAfterSwap(fromPosition: Int, toPosition: Int)           | 交换位置后回调   |\n| onMergeData(fromPosition: Int, toPosition: Int)           | 合并逻辑         |\n| onStartDrag(viewHolder: RecyclerView.ViewHolder?)         | 开始拖拽         |\n| onStopDrag(performMerge: Boolean)                         | 结束拖拽         |\n\n### DragTouchCallback类\n\n描述：DragTouchCallback继承ItemTouchHelper.Callback()用于判断列表Item在拖拽过程中是否触发Item位置交换、Item合并操作的逻辑处理。\n\n\n| 方法名                                                                                             | 描述                                     |\n| ---------------------------------------------------------------------------------------------------- | ------------------------------------------ |\n| DragTouchCallback(dragAdapter: IDragAdapter, horizontal: Boolean = true, vertical: Boolean = true) | 构造方法支持设置垂直、水平方向的拖动控制 |\n| setDragHandler(handler: IDragHandler)                                                              | 设置拖拽处理器                           |\n\n**主要实现：**\n\n* 重写chooseDropTarget()方法，计算两个ViewHolder之间X和Y轴方向的重叠部分是否达到触发合并的条件，将满足合并条件的两个ViewHolder暂存起来，当用户松开手指时触发合并逻辑。\n* 重写omMove()方法，通过外部注入的IDragHandler处理器的swapPosition()方法判断两个ViewHolder是否可以交换位置。\n\n## 许可证\n\nApache 2.0. 有关详细信息，请参阅 [License](https://github.com/DSAppTeam/DSDrag/blob/master/LICENSE) 。\n\n## 欢迎提需要支持的功能及issue\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdsappteam%2Frecyclerviewdrag","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdsappteam%2Frecyclerviewdrag","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdsappteam%2Frecyclerviewdrag/lists"}