From a5e9542a6b267ad3ed34452ff9938bb8909fb611 Mon Sep 17 00:00:00 2001 From: Kainalu Hagiwara Date: Mon, 3 Aug 2020 13:55:37 -0700 Subject: [PATCH] For #13016 - Only scroll to current page in tab history after the initial layout. --- .../fenix/tabhistory/TabHistoryView.kt | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/org/mozilla/fenix/tabhistory/TabHistoryView.kt b/app/src/main/java/org/mozilla/fenix/tabhistory/TabHistoryView.kt index c7e2e3004..f74da7e4d 100644 --- a/app/src/main/java/org/mozilla/fenix/tabhistory/TabHistoryView.kt +++ b/app/src/main/java/org/mozilla/fenix/tabhistory/TabHistoryView.kt @@ -34,18 +34,25 @@ class TabHistoryView( private val adapter = TabHistoryAdapter(interactor) private val layoutManager = object : LinearLayoutManager(containerView.context) { + + private var shouldScrollToSelected = true + override fun onLayoutCompleted(state: RecyclerView.State?) { super.onLayoutCompleted(state) currentIndex?.let { index -> - // Force expansion of the dialog, otherwise scrolling to the current history item - // won't work when its position is near the bottom of the recyclerview. - expandDialog.invoke() - // Also, attempt to center the current history item. - val itemView = tabHistoryRecyclerView.findViewHolderForLayoutPosition( - findFirstCompletelyVisibleItemPosition() - )?.itemView - val offset = tabHistoryRecyclerView.height / 2 - (itemView?.height ?: 0) / 2 - scrollToPositionWithOffset(index, offset) + // Attempt to center the current history item after the first layout is completed, + // but not after subsequent layouts + if (shouldScrollToSelected) { + // Force expansion of the dialog, otherwise scrolling to the current history item + // won't work when its position is near the bottom of the recyclerview. + expandDialog.invoke() + val itemView = tabHistoryRecyclerView.findViewHolderForLayoutPosition( + findFirstCompletelyVisibleItemPosition() + )?.itemView + val offset = tabHistoryRecyclerView.height / 2 - (itemView?.height ?: 0) / 2 + scrollToPositionWithOffset(index, offset) + shouldScrollToSelected = false + } } } }.apply {