Bottom space bar: always try to center the selected space

Change-Id: Ia9820869d27b409da50900ab9819cec9e5e4691b
This commit is contained in:
SpiritCroc 2022-10-08 19:07:34 +02:00
parent a53c183847
commit 731dc8987d
2 changed files with 14 additions and 7 deletions

View File

@ -171,10 +171,10 @@ class HomeDetailFragment :
checkNotificationTabStatus() checkNotificationTabStatus()
views.spaceBarRecyclerView.layoutManager = LinearLayoutManager(context)
val spaceBarAdapter = spaceBarController.also { controller -> val spaceBarAdapter = spaceBarController.also { controller ->
controller.spaceRoomListener = spaceBarListener controller.spaceRoomListener = spaceBarListener
}.adapter }.adapter
views.spaceBarRecyclerView.layoutManager = LinearLayoutManager(context)
views.spaceBarRecyclerView.adapter = spaceBarAdapter views.spaceBarRecyclerView.adapter = spaceBarAdapter
// Reduce sensitivity of viewpager to avoid scrolling horizontally by accident too easily // Reduce sensitivity of viewpager to avoid scrolling horizontally by accident too easily

View File

@ -69,7 +69,9 @@ class SpaceBarController @Inject constructor(
) )
onBind { _, view, _ -> onBind { _, view, _ ->
host.carousel = view host.carousel = view
host.scrollToSpace(selectedSpace?.roomId) view.post {
host.scrollToSpace(selectedSpace?.roomId)
}
} }
onUnbind { _, _ -> onUnbind { _, _ ->
@ -137,12 +139,17 @@ class SpaceBarController @Inject constructor(
var effectivePosition = position var effectivePosition = position
val lm = safeCarousel.layoutManager as? LinearLayoutManager val lm = safeCarousel.layoutManager as? LinearLayoutManager
if (lm != null) { if (lm != null) {
// Look-ahead of 1 // Scroll to an element such that the new selection is roughly in the middle
if (lm.findFirstCompletelyVisibleItemPosition() >= position) { val firstVisible = lm.findFirstCompletelyVisibleItemPosition()
effectivePosition-- val visibleRange = lm.findLastCompletelyVisibleItemPosition() - firstVisible + 1
} else if (lm.findLastVisibleItemPosition() <= position) { val overshoot = visibleRange/2
effectivePosition++ 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)) effectivePosition = max(0, min(effectivePosition, lm.itemCount-1))
} }
safeCarousel.smoothScrollToPosition(effectivePosition) safeCarousel.smoothScrollToPosition(effectivePosition)