Giter VIP home page Giter VIP logo

android-dragreordersample's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

android-dragreordersample's Issues

moveItem doesn't reorder items properly

fun moveItem(from: Int, to: Int) {
val fromEmoji = emojis[from]
emojis.removeAt(from)
if (to < from) {
emojis.add(to, fromEmoji)
} else {
emojis.add(to - 1, fromEmoji)
}
}

Imagine you move items from 0 position to 1

  • First your remove items at 0 position
  • Then you add item again to 0 position such as to > from and you add to to-1 position

The proper solution may be like this:

    fun moveItem(from: Int, to: Int) {
        val fromEmoji = emojis[from]
        emojis.removeAt(from)
        emojis.add(to, fromEmoji)    
    }

Test

@Test
    public void testMoveItem() {
        List<Integer> list = Arrays.asList(0, 1, 2, 3, 4, 5);
        assertEquals(
                Arrays.asList(1, 0, 2, 3, 4, 5),
                moveItem(list, 0, 1));
        assertEquals(
                Arrays.asList(1, 2, 0, 3, 4, 5),
                moveItem(list, 0, 2));
        assertEquals(
                Arrays.asList(1, 2, 3, 4, 5, 0),
                moveItem(list, 0, 5));
        assertEquals(
                Arrays.asList(0, 1, 2, 3, 5, 4),
                moveItem(list, 5, 4));
        assertEquals(
                Arrays.asList(5, 0, 1, 2, 3, 4),
                moveItem(list, 5, 0));
        assertEquals(
                Arrays.asList(0, 1, 2, 4, 3, 5),
                moveItem(list, 3, 4));
        assertEquals(
                Arrays.asList(0, 1, 2, 4, 3, 5),
                moveItem(list, 4, 3));
    }

    public <T> List<T> moveItem(List<T> data, int from, int to) {
        data = new ArrayList<>(data);
        T item = data.get(from);
        data.remove(from);
        data.add(to, item);
        return data;
    }

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.