Reorderable
Reorderable copied to clipboard
Reorder items in Lists and Grids in Jetpack Compose and Compose Multiplatform with drag and drop.
Reorderable
Reorderable is a simple library that allows you to reorder items in LazyColumn, LazyRow, LazyVerticalGrid, LazyHorizontalGrid, LazyVerticalStaggeredGrid, and LazyHorizontalStaggeredGrid as well as Column and Row in Jetpack Compose and Compose Multiplatform with drag and drop.
The latest demo app APK can be found in the releases section under the "Assets" section of the latest release.
| LazyColumn | LazyGrid |
|---|---|
|
|
Features
- Supports Compose Multiplatform (Android, iOS, Desktop/JVM, Wasm)
- Supports items of different sizes
- Some items can be made non-reorderable
- Supports dragging immediately or long press to start dragging
- Scrolls when dragging to the edge of the screen. (unavailable for
ColumnandRow) The scroll speed is based on the distance from the edge of the screen. - Uses the new
Modifier.animateItemPlacementAPI to animate item movement inLazyColumn,LazyRow,LazyVerticalGrid,LazyHorizontalGrid,LazyVerticalStaggeredGrid, andLazyHorizontalStaggeredGrid - Supports using a child of an item as the drag handle
Usage
Version Catalog
If you're using Version Catalog, add the following to your libs.versions.toml file:
[versions]
#...
reorderable = "2.1.1"
[libraries]
#...
reorderable = { module = "sh.calvin.reorderable:reorderable", version.ref = "reorderable" }
or
[libraries]
#...
reorderable = { module = "sh.calvin.reorderable:reorderable", version = "2.1.1" }
Gradle
If you're using Gradle instead, add the following to your build.gradle file:
Kotlin DSL
dependencies {
implementation("sh.calvin.reorderable:reorderable:2.1.1")
}
Groovy DSL
dependencies {
implementation 'sh.calvin.reorderable:reorderable:2.1.1'
}
Examples
See demo app code for more examples.
Table of Contents
LazyColumnLazyRowLazyVerticalGridLazyHorizontalGridLazyVerticalStaggeredGridLazyHorizontalStaggeredGridColumnRow- Accessibility
- FAQ
LazyColumn
Find more examples in SimpleReorderableLazyColumnScreen.kt, SimpleLongPressHandleReorderableLazyColumnScreen.kt and ComplexReorderableLazyColumnScreen.kt in the demo app.
To use this library with LazyColumn, follow this basic structure:
val lazyListState = rememberLazyListState()
val reorderableLazyListState = rememberReorderableLazyListState(lazyListState) { from, to ->
// Update the list
}
LazyColumn(state = lazyListState) {
items(list, key = { /* item key */ }) {
ReorderableItem(reorderableLazyListState, key = /* item key */) { isDragging ->
// Item content
IconButton(
modifier = Modifier.draggableHandle(),
/* ... */
)
}
}
}
Since Modifier.draggableHandle and Modifier.longPressDraggableHandle can only be used in ReorderableCollectionItemScope, you may need to pass ReorderableCollectionItemScope to a child composable. For example:
@Composable
fun List() {
// ...
LazyColumn(state = lazyListState) {
items(list, key = { /* item key */ }) {
ReorderableItem(reorderableLazyListState, key = /* item key */) { isDragging ->
// Item content
DragHandle(this)
}
}
}
}
@Composable
fun DragHandle(scope: ReorderableCollectionItemScope) {
IconButton(
modifier = with(scope) {
Modifier.draggableHandle()
},
/* ... */
)
}
If your LazyColumn displays under navigation bar or notification bar, you may want to add scrollThresholdPadding to rememberReorderableLazyListState to move the scroll trigger area out from under the navigation bar or notification bar.
val reorderableLazyListState = rememberReorderableLazyListState(
lazyListState = lazyListState,
scrollThresholdPadding = WindowInsets.systemBars.asPaddingValues(),
) { from, to ->
...
}
Here's a more complete example with (with haptic feedback):
val view = LocalView.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))
}
view.performHapticFeedback(HapticFeedbackConstants.SEGMENT_FREQUENT_TICK)
}
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 = {
view.performHapticFeedback(HapticFeedbackConstants.DRAG_START)
},
onDragStopped = {
view.performHapticFeedback(HapticFeedbackConstants.GESTURE_END)
},
),
onClick = {},
) {
Icon(Icons.Rounded.DragHandle, contentDescription = "Reorder")
}
}
}
}
}
}
If you want to use the material3's Clickable Card, you can create a MutableInteractionSource and pass it to both the Card and the Modifier.draggableHandle (or Modifier.longPressDraggableHandle), Modifier.draggableHandle will emit drag events to the MutableInteractionSource so that the Card can respond to the drag events:
val view = LocalView.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))
}
view.performHapticFeedback(HapticFeedbackConstants.SEGMENT_FREQUENT_TICK)
}
LazyColumn(
modifier = Modifier.fillMaxSize(),
state = lazyListState,
contentPadding = PaddingValues(8.dp),
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
items(list, key = { it }) { item ->
ReorderableItem(reorderableLazyListState, key = item) {
val interactionSource = remember { MutableInteractionSource() }
Card(
onClick = {},
interactionSource = interactionSource,
) {
Row {
Text(item, Modifier.padding(horizontal = 8.dp))
IconButton(
modifier = Modifier.draggableHandle(
onDragStarted = {
view.performHapticFeedback(HapticFeedbackConstants.DRAG_START)
},
onDragStopped = {
view.performHapticFeedback(HapticFeedbackConstants.GESTURE_END)
},
interactionSource = interactionSource,
),
onClick = {},
) {
Icon(Icons.Rounded.DragHandle, contentDescription = "Reorder")
}
}
}
}
}
}
LazyRow
See SimpleReorderableLazyRowScreen.kt and ComplexReorderableLazyRowScreen.kt in the demo app.
You can just replace Column with Row in the LazyColumn examples above.
LazyVerticalGrid
Find more examples in SimpleReorderableLazyVerticalGridScreen.kt in the demo app.
To use this library with LazyVerticalGrid, follow this basic structure:
val lazyGridState = rememberLazyGridState()
val reorderableLazyGridState = rememberReorderableLazyGridState(lazyGridState) { from, to ->
// Update the list
}
LazyVerticalGrid(state = lazyGridState) {
items(list, key = { /* item key */ }) {
ReorderableItem(reorderableLazyGridState, key = /* item key */) { isDragging ->
// Item content
IconButton(
modifier = Modifier.draggableHandle(),
/* ... */
)
}
}
}
Since Modifier.draggableHandle and Modifier.longPressDraggableHandle can only be used in ReorderableCollectionItemScope, you may need to pass ReorderableCollectionItemScope to a child composable. For example:
@Composable
fun Grid() {
// ...
LazyVerticalGrid(state = lazyGridState) {
items(Grid, key = { /* item key */ }) {
ReorderableItem(reorderableLazyGridState, key = /* item key */) { isDragging ->
// Item content
DragHandle(this)
}
}
}
}
@Composable
fun DragHandle(scope: ReorderableCollectionItemScope) {
IconButton(
modifier = with(scope) {
Modifier.draggableHandle()
},
/* ... */
)
}
If your LazyVerticalGrid displays under navigation bar or notification bar, you may want to add scrollThresholdPadding to rememberReorderableLazyGridState to move the scroll trigger area out from under the navigation bar or notification bar.
val reorderableLazyGridState = rememberReorderableLazyGridState(
lazyGridState = lazyGridState,
scrollThresholdPadding = WindowInsets.systemBars.asPaddingValues(),
) { from, to ->
...
}
Here's a more complete example with (with haptic feedback):
val view = LocalView.current
var list by remember { mutableStateOf(List(100) { "Item $it" }) }
val lazyGridState = rememberLazyGridState()
val reorderableLazyGridState = rememberReorderableLazyGridState(lazyGridState) { from, to ->
list = list.toMutableList().apply {
add(to.index, removeAt(from.index))
}
view.performHapticFeedback(HapticFeedbackConstants.SEGMENT_FREQUENT_TICK)
}
LazyVerticalGrid(
columns = GridCells.Adaptive(minSize = 96.dp),
modifier = Modifier.fillMaxSize(),
state = lazyGridState,
contentPadding = PaddingValues(8.dp),
verticalArrangement = Arrangement.spacedBy(8.dp),
horizontalArrangement = Arrangement.spacedBy(8.dp),
) {
items(list, key = { it }) {
ReorderableItem(reorderableLazyGridState, 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 = {
view.performHapticFeedback(HapticFeedbackConstants.DRAG_START)
},
onDragStopped = {
view.performHapticFeedback(HapticFeedbackConstants.GESTURE_END)
},
),
onClick = {},
) {
Icon(Icons.Rounded.DragHandle, contentDescription = "Reorder")
}
}
}
}
}
}
If you want to use the material3's Clickable Card, you can create a MutableInteractionSource and pass it to both the Card and the Modifier.draggableHandle (or Modifier.longPressDraggableHandle), Modifier.draggableHandle will emit drag events to the MutableInteractionSource so that the Card can respond to the drag events:
val view = LocalView.current
var list by remember { mutableStateOf(List(100) { "Item $it" }) }
val lazyGridState = rememberLazyGridState()
val reorderableLazyGridState = rememberReorderableLazyGridState(lazyGridState) { from, to ->
list = list.toMutableList().apply {
add(to.index, removeAt(from.index))
}
view.performHapticFeedback(HapticFeedbackConstants.SEGMENT_FREQUENT_TICK)
}
LazyVerticalGrid(
columns = GridCells.Adaptive(minSize = 96.dp),
modifier = Modifier.fillMaxSize(),
state = lazyGridState,
contentPadding = PaddingValues(8.dp),
verticalArrangement = Arrangement.spacedBy(8.dp),
horizontalArrangement = Arrangement.spacedBy(8.dp),
) {
items(list, key = { it }) { item ->
ReorderableItem(reorderableLazyGridState, key = item) {
val interactionSource = remember { MutableInteractionSource() }
Card(
onClick = {},
interactionSource = interactionSource,
) {
Row {
Text(item, Modifier.padding(horizontal = 8.dp))
IconButton(
modifier = Modifier.draggableHandle(
onDragStarted = {
view.performHapticFeedback(HapticFeedbackConstants.DRAG_START)
},
onDragStopped = {
view.performHapticFeedback(HapticFeedbackConstants.GESTURE_END)
},
interactionSource = interactionSource,
),
onClick = {},
) {
Icon(Icons.Rounded.DragHandle, contentDescription = "Reorder")
}
}
}
}
}
}
LazyHorizontalGrid
Find more examples in SimpleReorderableLazyHorizontalGridScreen.kt in the demo app.
You can just replace LazyVerticalStaggeredGrid with LazyHorizontalGrid in the LazyVerticalStaggeredGrid examples above.
LazyVerticalStaggeredGrid
Find more examples in SimpleReorderableLazyVerticalStaggeredGridScreen.kt in the demo app.
To use this library with LazyVerticalStaggeredGrid, follow this basic structure:
val lazyStaggeredGridState = rememberLazyStaggeredGridState()
val reorderableLazyStaggeredGridState = rememberReorderableLazyStaggeredGridState(lazyStaggeredGridState) { from, to ->
// Update the list
}
LazyVerticalStaggeredGrid(state = lazyStaggeredGridState) {
items(list, key = { /* item key */ }) {
ReorderableItem(reorderableLazyStaggeredGridState, key = /* item key */) { isDragging ->
// Item content
IconButton(
modifier = Modifier.draggableHandle(),
/* ... */
)
}
}
}
Since Modifier.draggableHandle and Modifier.longPressDraggableHandle can only be used in ReorderableCollectionItemScope, you may need to pass ReorderableCollectionItemScope to a child composable. For example:
@Composable
fun Grid() {
// ...
LazyVerticalStaggeredGrid(state = lazyStaggeredGridState) {
items(list, key = { /* item key */ }) {
ReorderableItem(reorderableLazyStaggeredGridState, key = /* item key */) { isDragging ->
// Item content
DragHandle(this)
}
}
}
}
@Composable
fun DragHandle(scope: ReorderableCollectionItemScope) {
IconButton(
modifier = with(scope) {
Modifier.draggableHandle()
},
/* ... */
)
}
If your LazyVerticalStaggeredGrid displays under navigation bar or notification bar, you may want to add scrollThresholdPadding to rememberReorderableLazyStaggeredGridState to move the scroll trigger area out from under the navigation bar or notification bar.
val reorderableLazyStaggeredGridState = rememberReorderableLazyStaggeredGridState(
lazyStaggeredGridState = lazyStaggeredGridState,
scrollThresholdPadding = WindowInsets.systemBars.asPaddingValues(),
) { from, to ->
...
}
Here's a more complete example with (with haptic feedback):
val view = LocalView.current
var list by remember { mutableStateOf(List(100) { "Item $it" }) }
val lazyStaggeredGridState = rememberLazyStaggeredGridState()
val reorderableLazyStaggeredGridState = rememberReorderableLazyStaggeredGridState(lazyStaggeredGridState) { from, to ->
list = list.toMutableList().apply {
add(to.index, removeAt(from.index))
}
view.performHapticFeedback(HapticFeedbackConstants.SEGMENT_FREQUENT_TICK)
}
LazyVerticalStaggeredGrid(
columns = StaggeredGridCells.Adaptive(minSize = 96.dp),
modifier = Modifier.fillMaxSize(),
state = lazyStaggeredGridState,
contentPadding = PaddingValues(8.dp),
verticalItemSpacing = 8.dp,
horizontalArrangement = Arrangement.spacedBy(8.dp),
) {
items(list, key = { it }) {
ReorderableItem(reorderableLazyStaggeredGridState, 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 = {
view.performHapticFeedback(HapticFeedbackConstants.DRAG_START)
},
onDragStopped = {
view.performHapticFeedback(HapticFeedbackConstants.GESTURE_END)
},
),
onClick = {},
) {
Icon(Icons.Rounded.DragHandle, contentDescription = "Reorder")
}
}
}
}
}
}
If you want to use the material3's Clickable Card, you can create a MutableInteractionSource and pass it to both the Card and the Modifier.draggableHandle (or Modifier.longPressDraggableHandle), Modifier.draggableHandle will emit drag events to the MutableInteractionSource so that the Card can respond to the drag events:
val view = LocalView.current
var list by remember { mutableStateOf(List(100) { "Item $it" }) }
val lazyStaggeredGridState = rememberLazyStaggeredGridState()
val reorderableLazyStaggeredGridState = rememberReorderableLazyStaggeredGridState(lazyStaggeredGridState) { from, to ->
list = list.toMutableList().apply {
add(to.index, removeAt(from.index))
}
view.performHapticFeedback(HapticFeedbackConstants.SEGMENT_FREQUENT_TICK)
}
LazyVerticalStaggeredGrid(
columns = StaggeredGridCells.Adaptive(minSize = 96.dp),
modifier = Modifier.fillMaxSize(),
state = lazyStaggeredGridState,
contentPadding = PaddingValues(8.dp),
verticalItemSpacing = 8.dp,
horizontalArrangement = Arrangement.spacedBy(8.dp),
) {
items(list, key = { it }) { item ->
ReorderableItem(reorderableLazyStaggeredGridState, key = item) {
val interactionSource = remember { MutableInteractionSource() }
Card(
onClick = {},
interactionSource = interactionSource,
) {
Row {
Text(item, Modifier.padding(horizontal = 8.dp))
IconButton(
modifier = Modifier.draggableHandle(
onDragStarted = {
view.performHapticFeedback(HapticFeedbackConstants.DRAG_START)
},
onDragStopped = {
view.performHapticFeedback(HapticFeedbackConstants.GESTURE_END)
},
interactionSource = interactionSource,
),
onClick = {},
) {
Icon(Icons.Rounded.DragHandle, contentDescription = "Reorder")
}
}
}
}
}
}
LazyHorizontalStaggeredGrid
Find more examples in SimpleReorderableLazyHorizontalStaggeredGridScreen.kt in the demo app.
You can just replace LazyVerticalStaggeredGrid with LazyHorizontalStaggeredGrid in the LazyVerticalStaggeredGrid examples above.
Column
Find more examples in ReorderableColumnScreen.kt and LongPressHandleReorderableColumnScreen.kt in the demo app.
To use this library with Column, follow this basic structure:
ReorderableColumn(
list = list,
onSettle = { fromIndex, toIndex ->
// Update the list
},
) { index, item, isDragging ->
key(item.id) {
// Item content
IconButton(modifier = Modifier.draggableHandle(), /* ... */)
}
}
Since Modifier.draggableHandle and Modifier.longPressDraggableHandle can only be used in ReorderableScope, you may need to pass ReorderableScope to a child composable. For example:
@Composable
fun List() {
// ...
ReorderableColumn(
list = list,
onSettle = { fromIndex, toIndex ->
// Update the list
},
) { index, item, isDragging ->
key(item.id) {
// Item content
DragHandle(this)
}
}
}
@Composable
fun DragHandle(scope: ReorderableScope) {
IconButton(modifier = with(scope) { Modifier.draggableHandle() }, /* ... */)
}
Here's a more complete example (with haptic feedback):
val view = LocalView.current
var list by remember { mutableStateOf(List(4) { "Item $it" }) }
ReorderableColumn(
modifier = Modifier
.fillMaxSize()
.padding(8.dp),
list = list,
onSettle = { fromIndex, toIndex ->
list = list.toMutableList().apply {
add(toIndex, removeAt(fromIndex))
}
},
onMove = {
view.performHapticFeedback(HapticFeedbackConstants.SEGMENT_FREQUENT_TICK)
},
verticalArrangement = Arrangement.spacedBy(8.dp),
) { _, item, isDragging ->
key(item) {
val elevation by animateDpAsState(if (isDragging) 4.dp else 0.dp)
Surface(shadowElevation = elevation) {
Row {
Text(item, Modifier.padding(horizontal = 8.dp))
IconButton(
modifier = Modifier.draggableHandle(
onDragStarted = {
view.performHapticFeedback(HapticFeedbackConstants.DRAG_START)
},
onDragStopped = {
view.performHapticFeedback(HapticFeedbackConstants.GESTURE_END)
},
),
onClick = {},
) {
Icon(Icons.Rounded.DragHandle, contentDescription = "Reorder")
}
}
}
}
}
If you want to use the material3's Clickable Card, you can create a MutableInteractionSource and pass it to both the Card and the Modifier.draggableHandle (or Modifier.longPressDraggableHandle), Modifier.draggableHandle will emit drag events to the MutableInteractionSource so that the Card can respond to the drag events:
val view = LocalView.current
var list by remember { mutableStateOf(List(4) { "Item $it" }) }
ReorderableColumn(
modifier = Modifier
.fillMaxSize()
.padding(8.dp),
list = list,
onSettle = { fromIndex, toIndex ->
list = list.toMutableList().apply {
add(toIndex, removeAt(fromIndex))
}
},
onMove = {
view.performHapticFeedback(HapticFeedbackConstants.SEGMENT_FREQUENT_TICK)
},
verticalArrangement = Arrangement.spacedBy(8.dp),
) { _, item, _ ->
key(item) {
val interactionSource = remember { MutableInteractionSource() }
Card(
onClick = {},
interactionSource = interactionSource,
) {
Row {
Text(item, Modifier.padding(horizontal = 8.dp))
IconButton(
modifier = Modifier.draggableHandle(
onDragStarted = {
view.performHapticFeedback(HapticFeedbackConstants.DRAG_START)
},
onDragStopped = {
view.performHapticFeedback(HapticFeedbackConstants.GESTURE_END)
},
interactionSource = interactionSource,
),
onClick = {},
) {
Icon(Icons.Rounded.DragHandle, contentDescription = "Reorder")
}
}
}
}
}
Row
See ReorderableRowScreen.kt in the demo app.
You can just replace Column with Row in the Column examples above.
Accessibility
See the demo app for examples of how to make the reorderable list accessible.
If the items in the list do not contain any button besides the drag handle, I recommend adding "Move Up"/"Move Down"/"Move Left"/"Move Right" actions to the TalkBack menu in each item via SemanticsPropertyReceiver.customActions and applying Modifier.clearAndSetSemantics to the drag handle button to make the drag handle button not focusable for TalkBack. For more information, see Key steps to improve Compose accessibility.
FAQ
When onMove is called to move items, the dragging item flickers/jumps/flashes.
[!NOTE]
This assumes you're using version 2.0.3 or later of this library.
The onMove function expects the list to be updated before it returns. If the list is updated after onMove returns, the dragging item will flicker. To fix this, update the list before returning from onMove.
If you can't keep the list update inside onMove, you can use a channel to communicate between onMove and the list update composition. Here's an example:
val listUpdatedChannel = remember { Channel<Unit>() }
val reorderableLazyXXXXState = rememberReorderableLazyXXXXState(listState) { from, to ->
// clear the channel
listUpdatedChannel.tryReceive()
// update the list
// wait for the list to be updated
listUpdatedChannel.receive()
}
LaunchedEffect(list) {
// notify the list is updated
listUpdatedChannel.trySend(Unit)
}
When moving the top item, the list flickers/jumps/flashes.
See issue #4. This problem will be fixed once Compose Foundation v1.7.0 is released.
API
LazyColumn / LazyRow
rememberReorderableLazyListStateReorderableItemModifier.draggableHandleModifier.longPressDraggableHandle
LazyVerticalGrid / LazyHorizontalGrid
rememberReorderableLazyGridStateReorderableItemModifier.draggableHandleModifier.longPressDraggableHandle
LazyVerticalStaggeredGrid / LazyHorizontalStaggeredGrid
rememberReorderableLazyStaggeredGridStateReorderableItemModifier.draggableHandleModifier.longPressDraggableHandle
Column / Row
ReorderableColumnReorderableRowModifier.draggableHandleModifier.longPressDraggableHandle
Running the demo app
To run the Android demo app, open the project in Android Studio and run the app.
To run the iOS demo app, open the iosApp project in Xcode and run the app or add the following Configuration to the Android Studio project, you may need to install the Kotlin Multiplatform Mobile plugin first.
To run the web demo app, run ./gradlew :composeApp:wasmJsBrowserDevelopmentRun.
To run the desktop demo app, run ./gradlew :demoApp:ComposeApp:run.
Contributing
Open this project with Android Studio Preview.
You'll want to install the Kotlin Multiplatform Mobile plugin in Android Studio before you open this project.
License
Copyright 2023 Calvin Liang
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.