I have a recycler view that uses paging 3 inside a fragment. When an item on it is clicked, I need to show a bottom sheet with the exact same recycler view in a more elaborate way. I want to try and reuse the same recycler view adapter so that more API calls are not invoked unnecessarily (I already have all the required data from the recycler view in the parent fragment)
This is how I implement my parent fragment recycler view
cardsRcView = binding.myCardsRcv.apply {
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
setHasFixedSize(true)
// use a linear layout manager
layoutManager = CenterZoomLinearLayoutManager(context)
// specify an viewAdapter (see also next example)
adapter = cardsAdapter
}
cardsAdapter.apply {
onClickCard = {
val cardSheet = DetailsBottomSheet.newInstance(cardsAdapter,cardPos)
cardSheet.show(childFragmentManager, "Card Details")
}
}
ViewModel:
fun fetchCards(
orgId: String
): Flow<PagingData<UIModel>> {
val pageSize = 5
return Pager(
config = PagingConfig(
pageSize = pageSize,
enablePlaceholders = false,
initialLoadSize = pageSize
),
pagingSourceFactory = {
CardsPagingDataSource(
orgId = orgId,
getCardsUseCase = getCardsUseCase
)
}
).flow
}
And in the bottomsheet like this:
companion object {
fun newInstance(
cardsAdapter: CardsAdapter,
cardPos: Int
): CardDetailsBottomSheet {
val cardBottomSheet = CardDetailsBottomSheet()
cardBottomSheet.cardsAdapter = cardsAdapter
cardBottomSheet.cardPos = cardPos
return cardBottomSheet
}
}
private var cardsAdapter: CardsAdapter? = null
....
cardsRcView.apply {
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
setHasFixedSize(true)
// use a linear layout manager
layoutManager = CenterZoomLinearLayoutManager(context)
// specify an viewAdapter (see also next example)
adapter = cardsAdapter
}
// This will affect my parent fragment adapter. I don't want it to.
cardsAdapter?.apply {
onClickCard = null
}
How can I do this without making unnecessary API calls?
I have thought of getting the paging adapters.snapShot() and creating a new adapater inside the bottom sheet, but then how can I integrate the Paging to the recycler view inside the bottomsheet.