Skip to content

Instantly share code, notes, and snippets.

@ananth10
Created March 16, 2023 12:21
Show Gist options
  • Save ananth10/2a2173220f4cb5a14475d695ad9ef356 to your computer and use it in GitHub Desktop.
Save ananth10/2a2173220f4cb5a14475d695ad9ef356 to your computer and use it in GitHub Desktop.
Swipe Delete In RecyclerView
private val swipeCallBack = object : ItemTouchHelper.SimpleCallback(0,ItemTouchHelper.RIGHT or ItemTouchHelper.LEFT){
override fun onMove(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder
): Boolean {
return true
}
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
val layoutPosition = viewHolder.layoutPosition
val selectedArt = artRecyclerviewAdapter.arts[layoutPosition]
viewModel.deleteArt(selectedArt)
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.recyclerView.adapter = artRecyclerviewAdapter
binding.recyclerView.layoutManager = LinearLayoutManager(requireContext())
ItemTouchHelper(swipeCallBack).attachToRecyclerView(binding.recyclerView)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment