Created
March 16, 2023 12:21
-
-
Save ananth10/2a2173220f4cb5a14475d695ad9ef356 to your computer and use it in GitHub Desktop.
Swipe Delete In RecyclerView
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
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