news 2026/8/13 20:08:00

Jetpack Compose拖放排序终极指南:Reorderable库的简单实现

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Jetpack Compose拖放排序终极指南:Reorderable库的简单实现

Jetpack Compose拖放排序终极指南:Reorderable库的简单实现

【免费下载链接】ReorderableA simple library that allows you to reorder items in `LazyColumn` and `LazyRow` as well as `Column` and `Row` in Jetpack Compose with drag and drop项目地址: https://gitcode.com/gh_mirrors/re/Reorderable

Reorderable是一个简单而强大的库,专为Jetpack Compose和Compose Multiplatform设计,让开发者能够轻松实现LazyColumnLazyRowColumnRow中的项目拖放排序功能。无论是构建列表、网格还是复杂布局,Reorderable都能提供流畅的拖放体验,帮助你打造更具交互性的应用界面。

🚀 为什么选择Reorderable库?

在移动应用开发中,用户经常需要自定义内容顺序。传统实现拖放排序需要处理复杂的手势检测、状态管理和动画效果,而Reorderable库将这一切简化,让你只需几行代码就能实现专业级的拖放功能。

核心优势一览

  • 多平台支持:完美兼容Android、iOS、Desktop/JVM、Wasm和JS平台
  • 灵活适配:支持不同大小的项目和非可排序项目的混合使用
  • 丰富交互:支持立即拖动或长按开始拖动两种模式
  • 智能滚动:拖动到屏幕边缘时自动滚动,滚动速度基于距离边缘的距离
  • 流畅动画:利用Compose的Modifier.animateItemAPI实现项目移动动画
  • 拖动手柄:支持将项目的子组件指定为拖动手柄

📱 直观的拖放体验展示

Reorderable库提供了两种主要的拖放排序场景:列表排序和网格排序。以下是实际应用效果展示:

列表拖放排序

图1:在LazyColumn中通过拖动手柄调整项目顺序,支持粘性标题和分段内容

网格拖放排序

图2:在LazyGrid中实现多列项目的自由拖放排序

⚡ 快速开始:3步集成Reorderable

1️⃣ 添加依赖

使用Version Catalog(推荐)

libs.versions.toml文件中添加:

[versions] reorderable = "3.0.0" [libraries] reorderable = { module = "sh.calvin.reorderable:reorderable", version.ref = "reorderable" }

然后在build.gradle.kts中添加依赖:

dependencies { implementation(libs.reorderable) }

直接使用Gradle

dependencies { implementation("sh.calvin.reorderable:reorderable:3.0.0") }

2️⃣ 实现基本的LazyColumn拖放

以下是一个完整的LazyColumn拖放排序示例,包含触觉反馈:

val hapticFeedback = LocalHapticFeedback.current var list by remember { mutableStateOf(List(100) { "Item $it" }) } val lazyListState = rememberLazyListState() val reorderableLazyListState = rememberReorderableLazyListState(lazyListState) { from, to -> list = list.toMutableList().apply { add(to.index, removeAt(from.index)) } hapticFeedback.performHapticFeedback(HapticFeedbackType.SegmentFrequentTick) } LazyColumn( modifier = Modifier.fillMaxSize(), state = lazyListState, contentPadding = PaddingValues(8.dp), verticalArrangement = Arrangement.spacedBy(8.dp), ) { items(list, key = { it }) { ReorderableItem(reorderableLazyListState, key = it) { isDragging -> val elevation by animateDpAsState(if (isDragging) 4.dp else 0.dp) Surface(shadowElevation = elevation) { Row { Text(it, Modifier.padding(horizontal = 8.dp)) IconButton( modifier = Modifier.draggableHandle( onDragStarted = { hapticFeedback.performHapticFeedback(HapticFeedbackType.GestureThresholdActivate) }, onDragStopped = { hapticFeedback.performHapticFeedback(HapticFeedbackType.GestureEnd) }, ), onClick = {}, ) { Icon(Icons.Rounded.DragHandle, contentDescription = "Reorder") } } } } } }

3️⃣ 探索更多组件

Reorderable库支持多种Compose布局组件,每种组件都有简单直观的API:

  • LazyRow:水平列表拖放排序
  • LazyVerticalGrid/LazyHorizontalGrid:网格布局拖放排序
  • LazyVerticalStaggeredGrid/LazyHorizontalStaggeredGrid:瀑布流布局拖放排序
  • Column/Row:简单列表拖放排序

完整的示例代码可以在demoApp/composeApp/src/commonMain/kotlin/sh/calvin/reorderable/demo/ui/目录中找到。

💡 高级使用技巧

处理分组列表和头部

当列表包含头部或分组时,需要调整索引计算:

val reorderableLazyColumnState = rememberReorderableLazyListState(lazyListState) { from, to -> list = list.toMutableList().apply { // 调整索引以跳过头部 add(to.index - 1, removeAt(from.index - 1)) } } LazyColumn(state = lazyListState) { item { Text("Header") } // 头部不参与排序 items(list, key = { item -> item.id }) { item -> ReorderableItem(reorderableLazyColumnState, item.id) { // 项目内容 } } }

自定义拖动手柄

可以将拖动手柄传递给子组件:

@Composable fun ListItem(scope: ReorderableCollectionItemScope, item: String) { Row { Text(item) IconButton( modifier = with(scope) { Modifier.draggableHandle() }, onClick = {} ) { Icon(Icons.Rounded.DragHandle, contentDescription = "Reorder") } } }

与Material3组件集成

与Material3的Card组件结合使用时,可通过MutableInteractionSource确保交互状态正确:

val interactionSource = remember { MutableInteractionSource() } Card( onClick = {}, interactionSource = interactionSource, ) { IconButton( modifier = Modifier.draggableHandle(interactionSource = interactionSource), onClick = {} ) { Icon(Icons.Rounded.DragHandle, contentDescription = "Reorder") } }

🏆 被行业领先应用采用

Reorderable库已经被众多知名应用采用,包括:

  • Lawnchair Launcher
  • Home Assistant
  • ProtonVPN
  • Pocket Casts
  • Mihon

📥 获取项目

要开始使用Reorderable库,请克隆项目仓库:

git clone https://gitcode.com/gh_mirrors/re/Reorderable

探索demoApp目录中的示例,了解如何在实际项目中实现各种拖放排序功能。

Reorderable库为Jetpack Compose开发者提供了简单而强大的拖放排序解决方案,无论是简单列表还是复杂网格,都能轻松实现流畅的拖放体验。立即集成Reorderable,为你的应用添加专业级的交互功能吧!

【免费下载链接】ReorderableA simple library that allows you to reorder items in `LazyColumn` and `LazyRow` as well as `Column` and `Row` in Jetpack Compose with drag and drop项目地址: https://gitcode.com/gh_mirrors/re/Reorderable

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/14 15:51:18

STEP3-VL-10B实战教程:WebUI中上传表格图片→提取数据→生成分析

STEP3-VL-10B实战教程:WebUI中上传表格图片→提取数据→生成分析 你是不是也遇到过这样的烦恼?拿到一份密密麻麻的表格图片,想要分析里面的数据,却只能手动一个个数字敲进Excel,费时费力还容易出错。 今天&#xff0…

作者头像 李华
网站建设 2026/7/14 15:51:19

DAMO-YOLO手机检测入门:OpenCV imread读取路径编码问题与中文支持修复

DAMO-YOLO手机检测入门:OpenCV imread读取路径编码问题与中文支持修复 1. 引言 你有没有遇到过这样的情况:写了一个看起来很完美的目标检测程序,模型也加载成功了,但一运行到读取图片那一步,程序就报错了&#xff0c…

作者头像 李华
网站建设 2026/7/14 15:51:22

GitHub 5万星项目拆解:opencode架构设计与部署优化技巧

GitHub 5万星项目拆解:opencode架构设计与部署优化技巧 1. 项目概述:终端原生的AI编程助手 OpenCode是2024年开源的一款AI编程助手框架,用Go语言编写,主打"终端优先、多模型、隐私安全"的设计理念。这个项目在GitHub上…

作者头像 李华
网站建设 2026/7/14 15:51:20

react-d3-tree高级交互功能:拖拽、缩放与动画效果实现

react-d3-tree高级交互功能:拖拽、缩放与动画效果实现 【免费下载链接】react-d3-tree :deciduous_tree: React component to create interactive D3 tree graphs 项目地址: https://gitcode.com/gh_mirrors/re/react-d3-tree react-d3-tree是一个基于React和…

作者头像 李华
网站建设 2026/7/14 15:51:24

从零开始使用LiDAR_IMU_Init:3步完成LiDAR与IMU的精准标定

从零开始使用LiDAR_IMU_Init:3步完成LiDAR与IMU的精准标定 【免费下载链接】LiDAR_IMU_Init [IROS2022] Robust Real-time LiDAR-inertial Initialization Method. 项目地址: https://gitcode.com/gh_mirrors/li/LiDAR_IMU_Init LiDAR_IMU_Init是一款强大的实…

作者头像 李华