Closed LilMoke closed 4 years ago
In the onSwiped
method of SwipeToDeleteCallback
you have access to a RecyclerView.ViewHolder
(parameter viewHolder
). You can get the adapter position of the item that was swiped calling the [getAdapterPosition](https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder#getAdapterPosition()) method.
With the adapter position you can call the method getSectionForPosition from SectionedRecyclerViewAdapter
(variable sectionedAdapter
in your code):
sectionedAdapter.getSectionForPosition(viewHolder.getAdapterPosition())
Thank you for this answer. One related question...
I have this code to add the swipe to delete in my fragment:
val swipeHandler = object : SwipeToDeleteCallback(context!!) {
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
val position = sectionedAdapter?.getPositionInSection(viewHolder.adapterPosition) ?: -1
val section = sectionedAdapter?.getSectionForPosition(viewHolder.getAdapterPosition()) as ProfileReferencesSection
doDelete(section, position)
}
}
val itemTouchHelper = ItemTouchHelper(swipeHandler)
itemTouchHelper.attachToRecyclerView(recyclerView)
The swiping works fine and your suggestion helped a lot.
One side effect of hat I am doing is that I am also able to swipe the section headers.
How can I prevent this?
Hi, on the SwipeToDeleteCallback function you can add this override:
override fun getMovementFlags(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder
): Int {
if (viewHolder is HeaderViewHolder || viewHolder is FooterViewHolder) {
return 0 // This prevent the swipe gesture
}
return super.getMovementFlags(recyclerView, viewHolder)
}
Hope this help you
Hello,
I am trying to implement Swipe to delete on a sectioned recycler. I have created a fragment as follows:
Then I created a section as follows:
The problem I am having is in my section in the Callback, how do I get the section so I know which section to remove the item from?
Any help is greatly appreciated. The Sample provided was great except it does not show a swipe to delete.