Skip to content

Instantly share code, notes, and snippets.

@Kashif-E
Created March 28, 2021 12:50
Show Gist options
  • Save Kashif-E/a59b732d13b03b6074bb3548ee43c681 to your computer and use it in GitHub Desktop.
Save Kashif-E/a59b732d13b03b6074bb3548ee43c681 to your computer and use it in GitHub Desktop.
private val itemTouchHelper by lazy {
val simpleItemTouchCallback = object : ItemTouchHelper.SimpleCallback(UP or DOWN or START or END, 0) {
override fun onMove(recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder): Boolean {
//getting the adapter
val adapter = recyclerView.adapter as MoviesAdapter
//the position from where item has been moved
val from = viewHolder.adapterPosition
//the position where the item is moved
val to = target.adapterPosition
//telling the adapter to move the item
adapter.notifyItemMoved(from, to)
return true
}
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
}
override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder?, actionState: Int) {
super.onSelectedChanged(viewHolder, actionState)
}
override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) {
super.clearView(recyclerView, viewHolder)
}
}
ItemTouchHelper(simpleItemTouchCallback)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment