Created
March 28, 2021 13:08
-
-
Save Kashif-E/3c9f1f9e02dab9fad3bdc96f324d40e5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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