diff --git a/vector/src/main/java/im/vector/riotx/core/di/ScreenComponent.kt b/vector/src/main/java/im/vector/riotx/core/di/ScreenComponent.kt index e0b14af9d0..aec3372098 100644 --- a/vector/src/main/java/im/vector/riotx/core/di/ScreenComponent.kt +++ b/vector/src/main/java/im/vector/riotx/core/di/ScreenComponent.kt @@ -63,7 +63,8 @@ import im.vector.riotx.features.ui.UiStateRepository ViewModelModule::class, FragmentModule::class, HomeModule::class, - RoomListModule::class + RoomListModule::class, + ScreenModule::class ] ) @ScreenScope diff --git a/vector/src/main/java/im/vector/riotx/core/di/ScreenModule.kt b/vector/src/main/java/im/vector/riotx/core/di/ScreenModule.kt index 1073a59f7c..56fac34f1e 100644 --- a/vector/src/main/java/im/vector/riotx/core/di/ScreenModule.kt +++ b/vector/src/main/java/im/vector/riotx/core/di/ScreenModule.kt @@ -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() } diff --git a/vector/src/main/java/im/vector/riotx/core/extensions/RecyclerView.kt b/vector/src/main/java/im/vector/riotx/core/extensions/RecyclerView.kt index 003045af51..3d247e149c 100644 --- a/vector/src/main/java/im/vector/riotx/core/extensions/RecyclerView.kt +++ b/vector/src/main/java/im/vector/riotx/core/extensions/RecyclerView.kt @@ -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)) diff --git a/vector/src/main/java/im/vector/riotx/features/home/room/list/RoomListFragment.kt b/vector/src/main/java/im/vector/riotx/features/home/room/list/RoomListFragment.kt index e272c1423f..122b95aa52 100644 --- a/vector/src/main/java/im/vector/riotx/features/home/room/list/RoomListFragment.kt +++ b/vector/src/main/java/im/vector/riotx/features/home/room/list/RoomListFragment.kt @@ -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) diff --git a/vector/src/main/java/im/vector/riotx/features/home/room/list/actions/RoomListQuickActionsBottomSheet.kt b/vector/src/main/java/im/vector/riotx/features/home/room/list/actions/RoomListQuickActionsBottomSheet.kt index 60a26c8151..5fc33ffbe9 100644 --- a/vector/src/main/java/im/vector/riotx/features/home/room/list/actions/RoomListQuickActionsBottomSheet.kt +++ b/vector/src/main/java/im/vector/riotx/features/home/room/list/actions/RoomListQuickActionsBottomSheet.kt @@ -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