我有一个回收器视图,它在片段中使用分页3.当它的一个项目被点击时,我需要显示一个底部的工作表与完全相同的回收器视图在一个更精细的方式. 我想try 并重用相同的回收器视图适配器,这样就不会不必要地调用更多的API调用(我已经在父片段中拥有了来自回收器视图的所有必需数据)
这就是我实现父级碎片回收器视图的方式
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")
}
}
视图模型:
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
}
在下面这样的底纸上:
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
}
我如何才能在不进行不必要的API调用的情况下做到这一点?
我想过获取分页适配器.snapShot()并在底页内创建一个新的适配器,但接下来如何将分页集成到底页内的回收器视图中.