Skip to content
This repository was archived by the owner on Dec 22, 2023. It is now read-only.

Commit 68dfc7b

Browse files
committed
* [o] Optimizes the touch relative offsets
* [m] Increases the height of first page elements to showcase long items
1 parent f8f0000 commit 68dfc7b

File tree

2 files changed

+12
-17
lines changed

2 files changed

+12
-17
lines changed

recyclerviewfastscroller/src/main/java/com/qtalk/recyclerviewfastscroller/RecyclerViewFastScroller.kt

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ import androidx.core.content.ContextCompat
4343
import androidx.core.widget.TextViewCompat
4444
import androidx.recyclerview.widget.LinearLayoutManager
4545
import androidx.recyclerview.widget.RecyclerView
46-
import kotlin.math.abs
4746
import kotlin.math.max
4847
import kotlin.math.min
4948
import kotlin.math.roundToInt
@@ -310,33 +309,24 @@ class RecyclerViewFastScroller @JvmOverloads constructor(
310309

311310
// getting the position of this view on the screen, getting absolute X and Y coordinates
312311
getLocationInWindow(locationArray)
312+
val yAbsPosition: Int = locationArray[1]
313313

314-
// this offset is to nullify the difference in the height between the handle and touch's relative value
315-
var touchRelativeOffset = 0f
316314
val touchListener = OnTouchListener { _, motionEvent ->
317315

318-
val yAbsPosition: Int = locationArray[1]
319-
// val xAbsPosition : Int = locationArray[0]
320-
321316
val touchAction = motionEvent.action.and(motionEvent.actionMasked)
322317
log("Touch Action: $touchAction")
318+
323319
when (touchAction) {
324320
MotionEvent.ACTION_MOVE, MotionEvent.ACTION_DOWN -> {
321+
325322
// disallow parent to spy on touch events
326323
requestDisallowInterceptTouchEvent(true)
324+
327325
if (motionEvent.action == MotionEvent.ACTION_DOWN) {
328326
if (!adapterDataObserver.isInitialized()) {
329327
registerDataObserver()
330328
}
331329

332-
// compute relative touch offset only on DOWN and use the same for MOVE events
333-
touchRelativeOffset =
334-
motionEvent.rawY - yAbsPosition - handleImageView.y
335-
336-
// check if offset is in bounds
337-
if (abs(touchRelativeOffset) > handleImageView.height) touchRelativeOffset =
338-
0f
339-
340330
// set the engaged flag to prevent the handle from scrolling again as the OnScrolled event in the ScrollListener is called even for programmatic scrolls
341331
isEngaged = true
342332

@@ -346,12 +336,17 @@ class RecyclerViewFastScroller @JvmOverloads constructor(
346336
popupTextView.animateVisibility()
347337
}
348338
}
339+
349340
//
350341
// ------------- Common methods to Move and Down events -------------------
351342
// calculate relative Y position internal to the view, from motion absolute px touch value and absolute start point of the view
352343
//
344+
345+
// subtract the handle height offset
346+
val handleHeightOffset = handleImageView.height / 2
347+
353348
val currentRelativeYPos =
354-
motionEvent.rawY - yAbsPosition - touchRelativeOffset
349+
motionEvent.rawY - yAbsPosition - handleHeightOffset
355350

356351
// move the handle only if fastScrolled, else leave the translation of the handle to the onScrolled method on the listener
357352

sample/src/main/res/layout/recycler_view_list_item_center.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
android:layout_width="match_parent"
4-
android:layout_height="wrap_content"
4+
android:layout_height="400dp"
55
xmlns:app="http://schemas.android.com/apk/res-auto">
66
<TextView
77
app:layout_constraintTop_toTopOf="parent"
88
android:id="@+id/text_view"
99
android:layout_width="match_parent"
10-
android:layout_height="wrap_content"
10+
android:layout_height="match_parent"
1111
android:textSize="18sp"
1212
android:gravity="center"
1313
android:padding="@dimen/text_view_padding"/>

0 commit comments

Comments
 (0)