Skip to content
Snippets Groups Projects
Commit 30cb7c12 authored by Janne Mareike Koschinski's avatar Janne Mareike Koschinski
Browse files

Finally reliably fix the scroll issue

parent ec5f3df9
No related branches found
No related tags found
No related merge requests found
......@@ -83,6 +83,7 @@ class MessageListFragment : ServiceBoundFragment() {
messageList.itemAnimator = null
messageList.setItemViewCacheSize(20)
var isScrolling = false
messageList.addOnScrollListener(
object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
......@@ -93,7 +94,13 @@ class MessageListFragment : ServiceBoundFragment() {
scrollDown.toggle(canScrollDown && isScrollingDown)
}
override fun onScrollStateChanged(recyclerView: RecyclerView?, newState: Int) = Unit
override fun onScrollStateChanged(recyclerView: RecyclerView?, newState: Int) {
isScrolling = when (newState) {
RecyclerView.SCROLL_STATE_SETTLING, RecyclerView.SCROLL_STATE_IDLE -> false
RecyclerView.SCROLL_STATE_DRAGGING -> true
else -> isScrolling
}
}
})
val data = viewModel.buffer_liveData.switchMapNotNull { buffer ->
......@@ -124,15 +131,6 @@ class MessageListFragment : ServiceBoundFragment() {
if (message != null && bufferSyncer != null && previousMessageId != message.messageId) {
markAsRead(bufferSyncer, message.bufferId, message.messageId)
previousMessageId = message.messageId
if (firstVisibleItemPosition < 2) {
activity?.runOnUiThread { messageList.scrollToPosition(0) }
runInBackgroundDelayed(16) {
activity?.runOnUiThread {
messageList.scrollToPosition(0)
}
}
}
}
}
})
......@@ -144,6 +142,17 @@ class MessageListFragment : ServiceBoundFragment() {
}
})
fun checkScroll() {
if (linearLayoutManager.findFirstVisibleItemPosition() < 2 && !isScrolling) {
messageList.scrollToPosition(0)
}
}
adapter.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
override fun onChanged() = checkScroll()
override fun onItemRangeInserted(positionStart: Int, itemCount: Int) = checkScroll()
})
var lastBuffer = -1
data.observe(this, Observer { list ->
val firstVisibleItemPosition = linearLayoutManager.findFirstVisibleItemPosition()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment