Search code examples
swap

How to swap View with long index gap in Kotlin?


I can swap a view that I click, with other view that I can see. But when I want to swap a view that I click, with other view that I can't see, it refuses to swap. How can I fix this?


Solution

  • You should first do a swap for your list of items that you are dealing with, and then use notifyItemMoved for those indices. following code may help you:

    val tmp = items[currentPos]
    items[currentPos] = items[idx]
    items[idx] = tmp
            
    notifyItemMoved(currentPos, idx)