Giter Site home page Giter Site logo

akashk25121 / composereorderable Goto Github PK

View Code? Open in Web Editor NEW

This project forked from aclassen/composereorderable

0.0 0.0 0.0 2.42 MB

Enables reordering by drag and drop in Jetpack Compose (Desktop) LazyList & LazyGrid.

License: Apache License 2.0

Kotlin 99.46% CSS 0.13% HTML 0.41%

composereorderable's Introduction

Compose LazyList/Grid reorder

Latest release

A Jetpack Compose (Android + Desktop) modifier enabling reordering by drag and drop in a LazyList and LazyGrid.

Sample

Download

dependencies {
    implementation("org.burnoutcrew.composereorderable:reorderable:<latest_version>")
}

How to use

  • Create a reorderable state by rememberReorderableLazyListState for LazyList or rememberReorderableLazyGridState for LazyGrid
  • Add the reorderable(state) modifier to your list/grid
  • Inside the list/grid itemscope create a ReorderableItem(state, key = ) for a keyed lists or ReorderableItem(state, index = ) for a indexed only list. (Animated items only work with keyed lists)
  • Apply the detectReorderAfterLongPress(state) or detectReorder(state) modifier to the list. If only a drag handle is needed apply the detect modifier to any child composable inside the item layout.

ReorderableItem provides the item dragging state, use this to apply elevation , scale etc.

@Composable
fun VerticalReorderList() {
    val data = remember { mutableStateOf(List(100) { "Item $it" }) }
    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)
        .detectReorderAfterLongPress(state)
    ) {
        items(data.value, { it }) { item ->
            ReorderableItem(state, key = item) { isDragging ->
                val elevation = animateDpAsState(if (isDragging) 16.dp else 0.dp)
                Column(
                    modifier = Modifier
                        .shadow(elevation.value)
                        .background(MaterialTheme.colors.surface)
                ) {
                    Text(item)
                }
            }
        }
    }
}

The item placement and drag cancelled animation can be changed or disabled by dragCancelledAnimation and defaultDraggingModifier

@Composable
fun VerticalReorderGrid() {
    val data = remember { mutableStateOf(List(100) { "Item $it" }) }
    val state = rememberReorderableLazyGridState(dragCancelledAnimation = NoDragCancelledAnimation(),
        onMove = { from, to ->
            data.value = data.value.toMutableList().apply {
                add(to.index, removeAt(from.index))
            }
        })
    LazyVerticalGrid(
        columns = GridCells.Fixed(4),
        state = state.gridState,
        modifier = Modifier.reorderable(state)
    ) {
        items(data.value, { it }) { item ->
            ReorderableItem(state, key = item, defaultDraggingModifier = Modifier) { isDragging ->
                Box(
                    modifier = Modifier
                        .aspectRatio(1f)
                        .background(MaterialTheme.colors.surface)
                ) {
                    Text(text = item,
                         modifier = Modifier.detectReorderAfterLongPress(state)
                    )
                }
            }
        }
    }
}

Check out the sample app for different implementation samples.

Notes

It's a known issue that the first visible item does not animate.

License

Copyright 2022 André Claßen

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

    https://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.

composereorderable's People

Contributors

aclassen avatar dependabot[bot] avatar warting avatar bierdav avatar veselyjan92 avatar juby210 avatar ofalvai avatar wakaztahir avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.