Skip to content

Commit

Permalink
Use scroll extent to calculate scroll proportion (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
randr0id committed Mar 2, 2022
1 parent f73d7cc commit c0e5c40
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Publish.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ object Publish {
override val major = 3
override val minor = 0
override val patch = 0
override val identifier = "beta.1"
override val identifier = "beta.2"
override fun toString() = version
}
}
Expand Down
24 changes: 11 additions & 13 deletions fastscroll/src/main/java/com/l4digital/fastscroll/FastScroller.kt
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,13 @@ class FastScroller : LinearLayout {
}
}

private val RecyclerView.scrollProportion: Float
get() {
val rangeDiff = computeVerticalScrollRange() - computeVerticalScrollExtent()
val proportion = computeVerticalScrollOffset() / if (rangeDiff > 0) rangeDiff.toFloat() else 1f
return viewHeight * proportion
}

@JvmOverloads
constructor(context: Context, size: Size = Size.NORMAL) : super(context) {
context.layout(size = size)
Expand All @@ -190,6 +197,10 @@ class FastScroller : LinearLayout {
)
}

override fun onSizeChanged(w: Int, h: Int, oldW: Int, oldH: Int) = super.onSizeChanged(w, h, oldW, oldH).also {
viewHeight = h
}

@SuppressLint("ClickableViewAccessibility")
override fun onTouchEvent(event: MotionEvent): Boolean {
val setYPositions: () -> Unit = {
Expand Down Expand Up @@ -478,19 +489,6 @@ class FastScroller : LinearLayout {
bubbleView.textSize = size.toFloat()
}

override fun onSizeChanged(w: Int, h: Int, oldW: Int, oldH: Int) = super.onSizeChanged(w, h, oldW, oldH).also {
viewHeight = h
}

private val RecyclerView.scrollProportion: Float
get() {
val verticalScrollOffset = computeVerticalScrollOffset()
val verticalScrollRange = computeVerticalScrollRange()
val rangeDiff = (verticalScrollRange - viewHeight).toFloat()
val proportion = verticalScrollOffset.toFloat() / if (rangeDiff > 0) rangeDiff else 1f
return viewHeight * proportion
}

private fun getRecyclerViewTargetPosition(y: Float) = recyclerView?.let { recyclerView ->
val itemCount = recyclerView.adapter?.itemCount ?: 0

Expand Down

0 comments on commit c0e5c40

Please sign in to comment.