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

Attempt at fixing the scrolling bug

parent 9f50849f
No related branches found
No related tags found
No related merge requests found
......@@ -82,6 +82,8 @@ class MessageListFragment : ServiceBoundFragment() {
messageList.layoutManager = linearLayoutManager
messageList.itemAnimator = null
messageList.setItemViewCacheSize(20)
var isScrolling = false
messageList.addOnScrollListener(
object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
......@@ -91,6 +93,14 @@ class MessageListFragment : ServiceBoundFragment() {
scrollDown.visibility = View.VISIBLE
scrollDown.toggle(canScrollDown && isScrollingDown)
}
override fun onScrollStateChanged(recyclerView: RecyclerView?, newState: Int) {
isScrolling = when (newState) {
RecyclerView.SCROLL_STATE_DRAGGING -> true
RecyclerView.SCROLL_STATE_SETTLING -> true
else -> false
}
}
})
val data = viewModel.buffer_liveData.switchMapNotNull { buffer ->
......@@ -113,6 +123,7 @@ class MessageListFragment : ServiceBoundFragment() {
viewModel.sessionManager_liveData.zip(lastMessageId).observe(
this, Observer {
val firstVisibleItemPosition = linearLayoutManager.findFirstVisibleItemPosition()
runInBackground {
val session = it?.first?.orNull()
val message = it?.second
......@@ -120,6 +131,15 @@ class MessageListFragment : ServiceBoundFragment() {
if (message != null && bufferSyncer != null && previousMessageId != message.messageId) {
markAsRead(bufferSyncer, message.bufferId, message.messageId)
previousMessageId = message.messageId
if (firstVisibleItemPosition < 2 && !isScrolling) {
activity?.runOnUiThread { messageList.scrollToPosition(0) }
runInBackgroundDelayed(16) {
activity?.runOnUiThread {
messageList.scrollToPosition(0)
}
}
}
}
}
})
......@@ -140,15 +160,6 @@ class MessageListFragment : ServiceBoundFragment() {
list?.let(adapter::submitList)
}
if (firstVisibleItemPosition < 2) {
activity?.runOnUiThread { messageList.scrollToPosition(0) }
runInBackgroundDelayed(16) {
activity?.runOnUiThread {
messageList.scrollToPosition(0)
}
}
}
val buffer = viewModel.buffer.value ?: -1
if (buffer != lastBuffer) {
adapter.clearCache()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment