smooth scroll instead of jump

This commit is contained in:
Matthieu 2021-05-22 13:03:13 +02:00
parent 8fa3072de0
commit 8d7d45a267
2 changed files with 28 additions and 1 deletions

View File

@ -9,6 +9,7 @@ import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.paging.* import androidx.paging.*
import androidx.paging.LoadState.* import androidx.paging.LoadState.*
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.* import kotlinx.coroutines.flow.*
@ -18,6 +19,7 @@ import org.pixeldroid.app.utils.BaseFragment
import org.pixeldroid.app.utils.api.objects.FeedContentDatabase import org.pixeldroid.app.utils.api.objects.FeedContentDatabase
import org.pixeldroid.app.utils.db.AppDatabase import org.pixeldroid.app.utils.db.AppDatabase
import org.pixeldroid.app.utils.db.dao.feedContent.FeedContentDao import org.pixeldroid.app.utils.db.dao.feedContent.FeedContentDao
import org.pixeldroid.app.utils.limitedLengthSmoothScrollToPosition
/** /**
* A fragment representing a list of [FeedContentDatabase] items that are cached by the database. * A fragment representing a list of [FeedContentDatabase] items that are cached by the database.
@ -78,7 +80,7 @@ open class CachedFeedFragment<T: FeedContentDatabase> : BaseFragment() {
} }
fun onTabReClicked() { fun onTabReClicked() {
binding.list.scrollToPosition(0) binding.list.limitedLengthSmoothScrollToPosition(0)
} }
} }

View File

@ -15,6 +15,8 @@ import androidx.browser.customtabs.CustomTabsIntent
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.lifecycle.DefaultLifecycleObserver import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import org.pixeldroid.app.R import org.pixeldroid.app.R
import okhttp3.HttpUrl import okhttp3.HttpUrl
import kotlin.properties.ReadWriteProperty import kotlin.properties.ReadWriteProperty
@ -79,6 +81,29 @@ fun BaseActivity.openUrl(url: String): Boolean{
} }
} }
fun RecyclerView.limitedLengthSmoothScrollToPosition(targetItem: Int) {
layoutManager?.apply {
val maxScroll = 3
when (this) {
is LinearLayoutManager -> {
val topItem = findFirstVisibleItemPosition()
val distance = topItem - targetItem
val anchorItem = when {
distance > maxScroll -> targetItem + maxScroll
distance < -maxScroll -> targetItem - maxScroll
else -> topItem
}
if (anchorItem != topItem) scrollToPosition(anchorItem)
post {
smoothScrollToPosition(targetItem)
}
}
else -> smoothScrollToPosition(targetItem)
}
}
}
/** /**
* @brief Updates the application's theme depending on the given preferences and resources * @brief Updates the application's theme depending on the given preferences and resources
*/ */