From 731dc8987d01ea3d849bebc0d4315f23ea85c17f Mon Sep 17 00:00:00 2001 From: SpiritCroc Date: Sat, 8 Oct 2022 19:07:34 +0200 Subject: [PATCH] Bottom space bar: always try to center the selected space Change-Id: Ia9820869d27b409da50900ab9819cec9e5e4691b --- .../app/features/home/HomeDetailFragment.kt | 2 +- .../list/home/spacebar/SpaceBarController.kt | 19 +++++++++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/home/HomeDetailFragment.kt b/vector/src/main/java/im/vector/app/features/home/HomeDetailFragment.kt index 41fd61f8df..cde3f58f30 100644 --- a/vector/src/main/java/im/vector/app/features/home/HomeDetailFragment.kt +++ b/vector/src/main/java/im/vector/app/features/home/HomeDetailFragment.kt @@ -171,10 +171,10 @@ class HomeDetailFragment : checkNotificationTabStatus() + views.spaceBarRecyclerView.layoutManager = LinearLayoutManager(context) val spaceBarAdapter = spaceBarController.also { controller -> controller.spaceRoomListener = spaceBarListener }.adapter - views.spaceBarRecyclerView.layoutManager = LinearLayoutManager(context) views.spaceBarRecyclerView.adapter = spaceBarAdapter // Reduce sensitivity of viewpager to avoid scrolling horizontally by accident too easily diff --git a/vector/src/main/java/im/vector/app/features/home/room/list/home/spacebar/SpaceBarController.kt b/vector/src/main/java/im/vector/app/features/home/room/list/home/spacebar/SpaceBarController.kt index 698b19c4bd..b472b01d33 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/list/home/spacebar/SpaceBarController.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/list/home/spacebar/SpaceBarController.kt @@ -69,7 +69,9 @@ class SpaceBarController @Inject constructor( ) onBind { _, view, _ -> host.carousel = view - host.scrollToSpace(selectedSpace?.roomId) + view.post { + host.scrollToSpace(selectedSpace?.roomId) + } } onUnbind { _, _ -> @@ -137,12 +139,17 @@ class SpaceBarController @Inject constructor( var effectivePosition = position val lm = safeCarousel.layoutManager as? LinearLayoutManager if (lm != null) { - // Look-ahead of 1 - if (lm.findFirstCompletelyVisibleItemPosition() >= position) { - effectivePosition-- - } else if (lm.findLastVisibleItemPosition() <= position) { - effectivePosition++ + // Scroll to an element such that the new selection is roughly in the middle + val firstVisible = lm.findFirstCompletelyVisibleItemPosition() + val visibleRange = lm.findLastCompletelyVisibleItemPosition() - firstVisible + 1 + val overshoot = visibleRange/2 + val currentMiddle = firstVisible + overshoot + if (currentMiddle < position) { + effectivePosition = position + overshoot + } else if (currentMiddle > position) { + effectivePosition = position - overshoot } + // List limits effectivePosition = max(0, min(effectivePosition, lm.itemCount-1)) } safeCarousel.smoothScrollToPosition(effectivePosition)