Set preferred anchor position from force scroll

Change-Id: I36c071c9caf8aa0ac913aea6211fd278690dd330
This commit is contained in:
SpiritCroc 2021-10-27 17:33:51 +02:00
parent 395b146500
commit 1fdb6923f5
1 changed files with 10 additions and 2 deletions

View File

@ -17,7 +17,7 @@
package im.vector.app.features.home.room.detail package im.vector.app.features.home.room.detail
import android.view.View 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.core.platform.DefaultListUpdateCallback
import im.vector.app.features.home.room.detail.timeline.TimelineEventController import im.vector.app.features.home.room.detail.timeline.TimelineEventController
import im.vector.app.features.home.room.detail.timeline.item.ItemWithEvents 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 java.util.concurrent.CopyOnWriteArrayList
import kotlin.math.max import kotlin.math.max
class ScrollOnNewMessageCallback(private val layoutManager: LinearLayoutManager, class ScrollOnNewMessageCallback(private val layoutManager: BetterLinearLayoutManager,
private val timelineEventController: TimelineEventController, private val timelineEventController: TimelineEventController,
private val parentView: View) : DefaultListUpdateCallback { private val parentView: View) : DefaultListUpdateCallback {
private val newTimelineEventIds = CopyOnWriteArrayList<String>() private val newTimelineEventIds = CopyOnWriteArrayList<String>()
private var forceScroll = false private var forceScroll = false
var initialForceScroll = false var initialForceScroll = false
set(value) {
field = value
if (!value) {
layoutManager.setPreferredAnchorPosition(-1)
}
}
var initialForceScrollEventId: String? = null var initialForceScrollEventId: String? = null
fun addNewTimelineEventIds(eventIds: List<String>) { fun addNewTimelineEventIds(eventIds: List<String>) {
@ -64,6 +70,7 @@ class ScrollOnNewMessageCallback(private val layoutManager: LinearLayoutManager,
} }
if (scrollToEvent == null) { if (scrollToEvent == null) {
layoutManager.scrollToPositionWithOffset(0, 0) layoutManager.scrollToPositionWithOffset(0, 0)
layoutManager.setPreferredAnchorPosition(0)
} else { } else {
timelineEventController.searchPositionOfEvent(scrollToEvent)?.let { timelineEventController.searchPositionOfEvent(scrollToEvent)?.let {
// Scroll such that the scrolled-to event is moved down (1-TARGET_SCROLL_OUT_FACTOR) of the screen. // 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). // from the bottom of the view, not the top).
val scrollToPosition = max(it + scrollOffset + 1, 0) val scrollToPosition = max(it + scrollOffset + 1, 0)
layoutManager.scrollToPositionWithOffset(scrollToPosition, (parentView.measuredHeight * RoomDetailFragment.TARGET_SCROLL_OUT_FACTOR).toInt()) layoutManager.scrollToPositionWithOffset(scrollToPosition, (parentView.measuredHeight * RoomDetailFragment.TARGET_SCROLL_OUT_FACTOR).toInt())
layoutManager.setPreferredAnchorPosition(scrollToPosition)
} }
} }
return return