From 1fdb6923f51b76ac3cc87628032540b824979933 Mon Sep 17 00:00:00 2001 From: SpiritCroc Date: Wed, 27 Oct 2021 17:33:51 +0200 Subject: [PATCH] Set preferred anchor position from force scroll Change-Id: I36c071c9caf8aa0ac913aea6211fd278690dd330 --- .../home/room/detail/ScrollOnNewMessageCallback.kt | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/ScrollOnNewMessageCallback.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/ScrollOnNewMessageCallback.kt index a1b28003a3..884ef1812e 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/ScrollOnNewMessageCallback.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/ScrollOnNewMessageCallback.kt @@ -17,7 +17,7 @@ package im.vector.app.features.home.room.detail import android.view.View -import androidx.recyclerview.widget.LinearLayoutManager +import de.spiritcroc.recyclerview.widget.BetterLinearLayoutManager import im.vector.app.core.platform.DefaultListUpdateCallback import im.vector.app.features.home.room.detail.timeline.TimelineEventController import im.vector.app.features.home.room.detail.timeline.item.ItemWithEvents @@ -25,13 +25,19 @@ import org.matrix.android.sdk.api.extensions.tryOrNull import java.util.concurrent.CopyOnWriteArrayList import kotlin.math.max -class ScrollOnNewMessageCallback(private val layoutManager: LinearLayoutManager, +class ScrollOnNewMessageCallback(private val layoutManager: BetterLinearLayoutManager, private val timelineEventController: TimelineEventController, private val parentView: View) : DefaultListUpdateCallback { private val newTimelineEventIds = CopyOnWriteArrayList() private var forceScroll = false var initialForceScroll = false + set(value) { + field = value + if (!value) { + layoutManager.setPreferredAnchorPosition(-1) + } + } var initialForceScrollEventId: String? = null fun addNewTimelineEventIds(eventIds: List) { @@ -64,6 +70,7 @@ class ScrollOnNewMessageCallback(private val layoutManager: LinearLayoutManager, } if (scrollToEvent == null) { layoutManager.scrollToPositionWithOffset(0, 0) + layoutManager.setPreferredAnchorPosition(0) } else { timelineEventController.searchPositionOfEvent(scrollToEvent)?.let { // Scroll such that the scrolled-to event is moved down (1-TARGET_SCROLL_OUT_FACTOR) of the screen. @@ -71,6 +78,7 @@ class ScrollOnNewMessageCallback(private val layoutManager: LinearLayoutManager, // from the bottom of the view, not the top). val scrollToPosition = max(it + scrollOffset + 1, 0) layoutManager.scrollToPositionWithOffset(scrollToPosition, (parentView.measuredHeight * RoomDetailFragment.TARGET_SCROLL_OUT_FACTOR).toInt()) + layoutManager.setPreferredAnchorPosition(scrollToPosition) } } return