Recyclerview now smoothly scrolls to top when pressing the toolbar

Instead of snapping, it snaps to a few items before it, then smoothly scrolls up

Inspired by whatever beta reddit sync is doing
This commit is contained in:
Jays2Kings 2021-04-08 16:57:18 -04:00
parent a5585f1d8c
commit 1cde93593b
3 changed files with 30 additions and 2 deletions

View File

@ -91,6 +91,7 @@ import eu.kanade.tachiyomi.util.view.isVisible
import eu.kanade.tachiyomi.util.view.scrollViewWith
import eu.kanade.tachiyomi.util.view.setOnQueryTextChangeListener
import eu.kanade.tachiyomi.util.view.setStyle
import eu.kanade.tachiyomi.util.view.smoothScrollToTop
import eu.kanade.tachiyomi.util.view.snack
import eu.kanade.tachiyomi.util.view.updateLayoutParams
import eu.kanade.tachiyomi.util.view.updatePaddingRelative
@ -565,7 +566,7 @@ class LibraryController(
true
}
binding.roundedCategoryHopper.upCategory.setOnLongClickListener {
binding.libraryGridRecycler.recycler.scrollToPosition(0)
binding.libraryGridRecycler.recycler.smoothScrollToTop()
true
}
binding.roundedCategoryHopper.categoryButton.setOnClickListener {

View File

@ -204,7 +204,7 @@ fun Controller.scrollViewWith(
activityBinding!!.toolbar.tag = randomTag
activityBinding!!.toolbar.setOnClickListener {
if ((this@scrollViewWith as? BottomSheetController)?.sheetIsExpanded() != true) {
recycler.scrollToPosition(0)
recycler.smoothScrollToTop()
} else {
(this@scrollViewWith as? BottomSheetController)?.toggleSheet()
}

View File

@ -20,6 +20,10 @@ import androidx.annotation.IdRes
import androidx.annotation.Px
import androidx.appcompat.view.menu.MenuBuilder
import androidx.core.graphics.ColorUtils
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.LinearSmoothScroller
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.SmoothScroller
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.google.android.material.bottomnavigation.BottomNavigationItemView
import com.google.android.material.bottomnavigation.BottomNavigationMenuView
@ -310,3 +314,26 @@ fun BottomNavigationView.getItemView(@IdRes id: Int): BottomNavigationItemView?
val order = (menu as MenuBuilder).findItemIndex(id)
return (getChildAt(0) as BottomNavigationMenuView).getChildAt(order) as? BottomNavigationItemView
}
fun RecyclerView.smoothScrollToTop() {
val linearLayoutManager = layoutManager as? LinearLayoutManager
if (linearLayoutManager != null) {
val smoothScroller: SmoothScroller = object : LinearSmoothScroller(context) {
override fun getVerticalSnapPreference(): Int {
return SNAP_TO_START
}
}
smoothScroller.targetPosition = 0
val firstItemPos = linearLayoutManager.findFirstVisibleItemPosition()
if (firstItemPos > 15) {
scrollToPosition(15)
post {
linearLayoutManager.startSmoothScroll(smoothScroller)
}
} else {
linearLayoutManager.startSmoothScroll(smoothScroller)
}
} else {
scrollToPosition(0)
}
}