Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 62 additions & 10 deletions AnkiDroid/src/main/java/com/ichi2/anki/ui/RecyclerFastScroller.kt
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,49 @@ class RecyclerFastScroller

private var hideOverride = false
private var adapter: RecyclerView.Adapter<*>? = null

private var cachedHandleHeight = 0
private var cachedHandleHeightItemCount = RecyclerView.NO_POSITION
private var cachedHandleHeightBarHeight = 0

private fun resetCachedHandleHeight() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

document it

cachedHandleHeight = 0
cachedHandleHeightItemCount = RecyclerView.NO_POSITION
cachedHandleHeightBarHeight = 0
}

private val adapterObserver: RecyclerView.AdapterDataObserver =
object : RecyclerView.AdapterDataObserver() {
override fun onChanged() {
super.onChanged()
resetCachedHandleHeight()
requestLayout()
}

override fun onItemRangeChanged(
positionStart: Int,
itemCount: Int,
) {
super.onItemRangeChanged(positionStart, itemCount)
resetCachedHandleHeight()
requestLayout()
}

override fun onItemRangeInserted(
positionStart: Int,
itemCount: Int,
) {
super.onItemRangeInserted(positionStart, itemCount)
resetCachedHandleHeight()
requestLayout()
}

override fun onItemRangeRemoved(
positionStart: Int,
itemCount: Int,
) {
super.onItemRangeRemoved(positionStart, itemCount)
resetCachedHandleHeight()
requestLayout()
}
}
Expand Down Expand Up @@ -253,6 +292,7 @@ class RecyclerFastScroller
this.adapter?.unregisterAdapterDataObserver(adapterObserver)
adapter?.registerAdapterDataObserver(adapterObserver)
this.adapter = adapter
resetCachedHandleHeight()
}

/**
Expand Down Expand Up @@ -405,16 +445,7 @@ class RecyclerFastScroller
val barHeight = bar.height
val ratio = scrollOffset.toFloat() / (verticalScrollRange - barHeight)

var calculatedHandleHeight = (barHeight.toFloat() / verticalScrollRange * barHeight).toInt()
if (calculatedHandleHeight < minScrollHandleHeight) {
calculatedHandleHeight = minScrollHandleHeight
}

if (calculatedHandleHeight >= barHeight) {
translationX = hiddenTranslationX.toFloat()
hideOverride = true
return
}
val calculatedHandleHeight = getCachedHandleHeight(barHeight, verticalScrollRange)

hideOverride = false

Expand All @@ -423,6 +454,27 @@ class RecyclerFastScroller
handle.layout(handle.left, y.toInt(), handle.right, y.toInt() + calculatedHandleHeight)
}

private fun getCachedHandleHeight(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

document it

barHeight: Int,
verticalScrollRange: Int,
): Int {
val itemCount = adapter?.itemCount ?: RecyclerView.NO_POSITION
if (itemCount != cachedHandleHeightItemCount || barHeight != cachedHandleHeightBarHeight) {
resetCachedHandleHeight()
cachedHandleHeightItemCount = itemCount
cachedHandleHeightBarHeight = barHeight
}

if (cachedHandleHeight == 0) {
cachedHandleHeight =
(barHeight.toFloat() / verticalScrollRange * barHeight)
.toInt()
.coerceAtLeast(minScrollHandleHeight)
}

return cachedHandleHeight
}

companion object {
private val DEFAULT_AUTO_HIDE_DELAY = 1500.milliseconds
}
Expand Down