RecyclerView: introduce view pool

This commit is contained in:
ganfra 2019-12-30 19:54:39 +01:00
parent 03fd474aa8
commit 8156b754c1
5 changed files with 19 additions and 5 deletions

View File

@ -63,7 +63,8 @@ import im.vector.riotx.features.ui.UiStateRepository
ViewModelModule::class,
FragmentModule::class,
HomeModule::class,
RoomListModule::class
RoomListModule::class,
ScreenModule::class
]
)
@ScreenScope

View File

@ -17,6 +17,7 @@
package im.vector.riotx.core.di
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.RecyclerView
import dagger.Module
import dagger.Provides
import im.vector.riotx.core.glide.GlideApp
@ -27,4 +28,9 @@ object ScreenModule {
@Provides
@JvmStatic
fun providesGlideRequests(context: AppCompatActivity) = GlideApp.with(context)
@Provides
@JvmStatic
@ScreenScope
fun providesSharedViewPool() = RecyclerView.RecycledViewPool()
}

View File

@ -26,9 +26,13 @@ import com.airbnb.epoxy.EpoxyController
*/
fun RecyclerView.configureWith(epoxyController: EpoxyController,
itemAnimator: RecyclerView.ItemAnimator? = null,
viewPool: RecyclerView.RecycledViewPool? = null,
showDivider: Boolean = false,
hasFixedSize: Boolean = true) {
layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false).apply {
recycleChildrenOnDetach = viewPool != null
}
setRecycledViewPool(viewPool)
itemAnimator?.let { this.itemAnimator = it }
if (showDivider) {
addItemDecoration(DividerItemDecoration(context, DividerItemDecoration.VERTICAL))

View File

@ -59,7 +59,8 @@ data class RoomListParams(
class RoomListFragment @Inject constructor(
private val roomController: RoomSummaryController,
val roomListViewModelFactory: RoomListViewModel.Factory,
private val notificationDrawerManager: NotificationDrawerManager
private val notificationDrawerManager: NotificationDrawerManager,
private val sharedViewPool: RecyclerView.RecycledViewPool
) : VectorBaseFragment(), RoomSummaryController.Listener, OnBackPressed, FabMenuView.Listener {
@ -95,7 +96,6 @@ class RoomListFragment @Inject constructor(
setupCreateRoomButton()
setupRecyclerView()
sharedActionViewModel = activityViewModelProvider.get(RoomListQuickActionsSharedActionViewModel::class.java)
roomListViewModel.subscribe { renderState(it) }
roomListViewModel.viewEvents
.observe()
@ -193,6 +193,8 @@ class RoomListFragment @Inject constructor(
val stateRestorer = LayoutManagerStateRestorer(layoutManager).register()
roomListView.layoutManager = layoutManager
roomListView.itemAnimator = RoomListAnimator()
roomListView.setRecycledViewPool(sharedViewPool)
layoutManager.recycleChildrenOnDetach = true
roomController.listener = this
modelBuildListener = OnModelBuildFinishedListener { it.dispatchTo(stateRestorer) }
roomController.addModelBuildListener(modelBuildListener)

View File

@ -46,6 +46,7 @@ data class RoomListActionsArgs(
class RoomListQuickActionsBottomSheet : VectorBaseBottomSheetDialogFragment(), RoomListQuickActionsEpoxyController.Listener {
private lateinit var sharedActionViewModel: RoomListQuickActionsSharedActionViewModel
@Inject lateinit var sharedViewPool: RecyclerView.RecycledViewPool
@Inject lateinit var roomListActionsViewModelFactory: RoomListQuickActionsViewModel.Factory
@Inject lateinit var roomListActionsEpoxyController: RoomListQuickActionsEpoxyController
@Inject lateinit var navigator: Navigator
@ -70,7 +71,7 @@ class RoomListQuickActionsBottomSheet : VectorBaseBottomSheetDialogFragment(), R
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
sharedActionViewModel = activityViewModelProvider.get(RoomListQuickActionsSharedActionViewModel::class.java)
recyclerView.configureWith(roomListActionsEpoxyController, hasFixedSize = false)
recyclerView.configureWith(roomListActionsEpoxyController, viewPool = sharedViewPool, hasFixedSize = false)
// Disable item animation
recyclerView.itemAnimator = null
roomListActionsEpoxyController.listener = this