Skip to content

Instantly share code, notes, and snippets.

@Kashif-E
Created March 28, 2021 13:08
Show Gist options
  • Save Kashif-E/3c9f1f9e02dab9fad3bdc96f324d40e5 to your computer and use it in GitHub Desktop.
Save Kashif-E/3c9f1f9e02dab9fad3bdc96f324d40e5 to your computer and use it in GitHub Desktop.
override fun onMove(recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder): Boolean {
val adapter = recyclerView.adapter as MoviesAdapter
val from = viewHolder.adapterPosition
val to = target.adapterPosition
adapter.moveItemInRecyclerViewList(from, to)
adapter.notifyItemMoved(from, to)
return true
}
fun moveItemInRecyclerViewList(from: Int, to: Int) {
val list = differ.currentList.toMutableList()
val fromLocation = list[from]
list.removeAt(from)
if (to < from) {
//+1 because it start from 0 on the upside. otherwise it will not change the locations accordingly
list.add(to + 1 , fromLocation)
} else {
//-1 because it start from length + 1 on the down side. otherwise it will not change the locations accordingly
list.add(to - 1, fromLocation)
}
differ.submitList(list)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment