Fix scroll-up glitch when new message arrives

This commit is contained in:
Naveen 2023-01-15 08:42:15 +05:30
parent e98351c146
commit 2d6ff0bc30
3 changed files with 11 additions and 4 deletions

View File

@ -36,6 +36,8 @@ import androidx.annotation.StringRes
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.res.ResourcesCompat
import androidx.core.view.*
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.simplemobiletools.commons.dialogs.ConfirmationDialog
@ -394,8 +396,10 @@ class ThreadActivity : SimpleActivity() {
runOnUiThread {
refreshMenuItems()
getOrCreateThreadAdapter().apply {
val scrollPosition = if (currentList.lastOrNull() != threadItems.lastOrNull()) {
threadItems.lastIndex
val lastPosition = itemCount - 1
val lastVisiblePosition = (thread_messages_list.layoutManager as LinearLayoutManager).findLastVisibleItemPosition()
val scrollPosition = if (currentList.lastOrNull() != threadItems.lastOrNull() && lastPosition - lastVisiblePosition <= 2) {
lastPosition
} else {
-1
}
@ -1235,7 +1239,7 @@ class ThreadActivity : SimpleActivity() {
val newItems = getThreadItems()
runOnUiThread {
getOrCreateThreadAdapter().updateMessages(newItems)
getOrCreateThreadAdapter().updateMessages(newItems, newItems.lastIndex)
if (!refreshedSinceSent) {
refreshMessages()
}

View File

@ -227,7 +227,7 @@ class ThreadAdapter(
private fun isThreadDateTime(position: Int) = currentList.getOrNull(position) is ThreadDateTime
fun updateMessages(newMessages: ArrayList<ThreadItem>, scrollPosition: Int = newMessages.lastIndex) {
fun updateMessages(newMessages: ArrayList<ThreadItem>, scrollPosition: Int = -1) {
val latestMessages = newMessages.toMutableList()
submitList(latestMessages) {
if (scrollPosition != -1) {

View File

@ -0,0 +1,3 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="24dp" android:height="24dp" android:viewportWidth="24" android:viewportHeight="24">
<path android:fillColor="@android:color/white" android:pathData="M11 5v11.17l-4.88-4.88c-0.39-0.39-1.03-0.39-1.42 0-0.39 0.39-0.39 1.02 0 1.41l6.59 6.59c0.39 0.39 1.02 0.39 1.41 0l6.59-6.59c0.39-0.39 0.39-1.02 0-1.41-0.39-0.39-1.02-0.39-1.41 0L13 16.17V5c0-0.55-0.45-1-1-1s-1 0.45-1 1z"/>
</vector>