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)