From caa71faab1a733b2a4705c80b7bbbf3c78aa95a3 Mon Sep 17 00:00:00 2001 From: Vonter <25414711+Vonter@users.noreply.github.com> Date: Tue, 1 Jun 2021 19:55:49 +0530 Subject: [PATCH 1/2] added scroll position save and restore in favorites tab --- .../instagrabber/fragments/FavoritesFragment.kt | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/awais/instagrabber/fragments/FavoritesFragment.kt b/app/src/main/java/awais/instagrabber/fragments/FavoritesFragment.kt index 04e97e26..c0c7fd26 100644 --- a/app/src/main/java/awais/instagrabber/fragments/FavoritesFragment.kt +++ b/app/src/main/java/awais/instagrabber/fragments/FavoritesFragment.kt @@ -2,6 +2,7 @@ package awais.instagrabber.fragments import android.content.DialogInterface import android.os.Bundle +import android.os.Handler import android.util.Log import android.view.LayoutInflater import android.view.View @@ -23,6 +24,8 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder class FavoritesFragment : Fragment() { private var shouldRefresh = true + private var scrollPosition = 0 + private lateinit var binding: FragmentFavoritesBinding private lateinit var root: RecyclerView private lateinit var adapter: FavoritesAdapter @@ -46,11 +49,19 @@ class FavoritesFragment : Fragment() { shouldRefresh = false } + override fun onPause() { + super.onPause() + scrollPosition = (root.getLayoutManager() as LinearLayoutManager).findLastVisibleItemPosition() + } + override fun onResume() { super.onResume() if (!this::adapter.isInitialized) return // refresh list every time in onViewStateRestored since it is cheaper than implementing pull down to refresh - favoritesViewModel.list.observe(viewLifecycleOwner, { list: List? -> adapter.submitList(list) }) + favoritesViewModel.list.observe(viewLifecycleOwner, { + list: List? -> adapter.submitList(list, Runnable { + root.scrollToPosition(scrollPosition)}) + }) } private fun init() { From 257d707c145289c46b950097ba508db1c50e8c84 Mon Sep 17 00:00:00 2001 From: Vonter <25414711+Vonter@users.noreply.github.com> Date: Mon, 7 Jun 2021 22:16:17 +0530 Subject: [PATCH 2/2] Change favorites tab scroll position restore to use StateRestorationPolicy --- .../awais/instagrabber/fragments/FavoritesFragment.kt | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/fragments/FavoritesFragment.kt b/app/src/main/java/awais/instagrabber/fragments/FavoritesFragment.kt index c0c7fd26..25b611d3 100644 --- a/app/src/main/java/awais/instagrabber/fragments/FavoritesFragment.kt +++ b/app/src/main/java/awais/instagrabber/fragments/FavoritesFragment.kt @@ -2,7 +2,6 @@ package awais.instagrabber.fragments import android.content.DialogInterface import android.os.Bundle -import android.os.Handler import android.util.Log import android.view.LayoutInflater import android.view.View @@ -24,8 +23,6 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder class FavoritesFragment : Fragment() { private var shouldRefresh = true - private var scrollPosition = 0 - private lateinit var binding: FragmentFavoritesBinding private lateinit var root: RecyclerView private lateinit var adapter: FavoritesAdapter @@ -51,7 +48,7 @@ class FavoritesFragment : Fragment() { override fun onPause() { super.onPause() - scrollPosition = (root.getLayoutManager() as LinearLayoutManager).findLastVisibleItemPosition() + adapter.setStateRestorationPolicy(RecyclerView.Adapter.StateRestorationPolicy.PREVENT) } override fun onResume() { @@ -60,8 +57,9 @@ class FavoritesFragment : Fragment() { // refresh list every time in onViewStateRestored since it is cheaper than implementing pull down to refresh favoritesViewModel.list.observe(viewLifecycleOwner, { list: List? -> adapter.submitList(list, Runnable { - root.scrollToPosition(scrollPosition)}) + adapter.setStateRestorationPolicy(RecyclerView.Adapter.StateRestorationPolicy.ALLOW) }) + }) } private fun init() {