aclassen / ComposeReorderable

Enables reordering by drag and drop in Jetpack Compose (Desktop) LazyList & LazyGrid.
Apache License 2.0
836 stars 89 forks source link

Animation is not working. Any idea? #229

Closed CiprianGabor closed 1 year ago

CiprianGabor commented 1 year ago

Animation is not working. Any idea? isDraggigng is always false

LazyColumn(
        state = state.listState,
        modifier = Modifier
            .reorderable(state)
            .detectReorderAfterLongPress(state)
    ) {
        items(data.value.size, { data.value[it] }) { item ->
            ReorderableItem(state, key = item) { isDragging ->
                val elevation = animateDpAsState(if (isDragging) 46.dp else 0.dp)
                Column(
                    modifier = Modifier
                        .shadow(elevation.value)
                        .background(MaterialTheme.colorScheme.surface)
                ) {
                    ShowCategoryDivider(data.value[item])
                }
            }
        }
    }
ShayDeeJay commented 1 year ago

You have not added animateplacement modifier to the column, do something like this.

modifier = Modifier .detectReorderAfterLongPress(state) .animateItemPlacement( animationSpec = tween( durationMillis = 100, easing = FastOutSlowInEasing ) )

CiprianGabor commented 1 year ago

It does not work either

CiprianGabor commented 1 year ago

Hello @warting @aclassen. Any idea why animations and haptic does not work?

this is the function:

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun VerticalReorderList(data: MutableState<List<String>>) {
    val haptic = LocalHapticFeedback.current

    val state = rememberReorderableLazyListState(onMove = { from, to ->
        data.value = data.value.toMutableList().apply {
            add(to.index, removeAt(from.index))
        }
    })
    LazyColumn(
        state = state.listState,
        modifier = Modifier
            .reorderable(state)
    ) {
        items(data.value.size, { data.value[it] }) { item ->
            ReorderableItem(state, key = item) { isDragging ->
                if (isDragging) haptic.performHapticFeedback(
                    HapticFeedbackType.LongPress
                )
                val elevation = animateDpAsState(if (isDragging) 46.dp else 0.dp)
                Column(
                    modifier = Modifier
                        .shadow(elevation.value)
                        .background(MaterialTheme.colorScheme.surface)
                        .detectReorderAfterLongPress(state)
                        .animateItemPlacement( animationSpec = tween( durationMillis = 100, easing = FastOutSlowInEasing ) )
                ) {
                    ShowCategoryDivider(data.value[item])
                }
            }
        }
    }
}

and I call it like this inside in an AlertDialog:

var categoryList = remember { mutableStateOf(listOf<String>()) }

VerticalReorderList(categoryList)
CiprianGabor commented 1 year ago

Can someone help me please?

lynkas commented 1 year ago

Can someone help me please?

I'm going to tell you how I solve this issue.

1) I clone this repo to see if it works. works. 2) I copy the simplest part of the dog list in the example. works. 3) I copied the dog list, putting my own part one by one to see what happened. finally, I found the itemsIndexed will cause no animation issue.

CiprianGabor commented 1 year ago

The problem was with the items(size) function, you have to use items(list) in order to work.

eschaumloeffel commented 2 weeks ago

For anyone potentially reading this with the same problem: For me, it only worked when using itemsIndexed(list) and providing the index to ReorderableItem(index = index). I passed the key before, but for some reason, only when passing the index, the item was considered dragging.