Skip to content

Commit 7ed97c0

Browse files
klinker24turing-tech
authored andcommitted
Add the ability to snap the fast scroll to a given percent of the scroll progress (#92)
1 parent ebead9c commit 7ed97c0

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-3.5-all.zip

gradlew

100644100755
File mode changed.

lib/src/main/java/com/turingtechnologies/materialscrollbar/MaterialScrollBar.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public abstract class MaterialScrollBar<T> extends RelativeLayout {
7878
private TypedArray a; //XML attributes
7979
private Boolean rtl = false;
8080
boolean hiddenByUser = false;
81+
private float fastScrollSnapPercent = 0;
8182

8283
//Associated Objects
8384
RecyclerView recyclerView;
@@ -88,6 +89,7 @@ public abstract class MaterialScrollBar<T> extends RelativeLayout {
8889
//Misc
8990
private OnLayoutChangeListener indicatorLayoutListener;
9091
private Runnable onSetup;
92+
private float previousScrollPercent = 0;
9193

9294

9395
//CHAPTER I - INITIAL SETUP
@@ -277,6 +279,10 @@ void identifySwipeRefreshParents(){
277279
}
278280
}
279281

282+
boolean isScrollChangeLargeEnoughForFastScroll(float currentScrollPercent) {
283+
return Math.abs(currentScrollPercent - previousScrollPercent) > fastScrollSnapPercent;
284+
}
285+
280286
boolean sizeUnchecked = true;
281287

282288
//Checks each time the bar is laid out. If there are few enough view that
@@ -371,6 +377,20 @@ private void checkCustomScrollingInterface(){
371377
}
372378
}
373379

380+
/**
381+
* With very long lists, it may be advantageous to put a buffer on the drag bar to give the
382+
* user some time to actually see the scroll handle and the content. This will make the
383+
* bar less "smooth scrolling" and instead, snap to specific scroll percents. This could
384+
* be useful for the {@link DateAndTimeIndicator} style scrollbars, where you don't need to see
385+
* every single date available.
386+
*
387+
* @param snapPercent percentage that the fast scroll bar should snap to.
388+
*/
389+
public T setFastScrollSnapPercent(float snapPercent) {
390+
fastScrollSnapPercent = snapPercent;
391+
return (T)this;
392+
}
393+
374394
/**
375395
* The scrollBar should attempt to use dev provided scrolling logic and not default logic.
376396
*
@@ -698,9 +718,15 @@ public void onAnimationEnd(Animator animation) {
698718
int top = handleThumb.getHeight() / 2;
699719
int bottom = recyclerView.getHeight() - Utils.getDP(72, recyclerView.getContext());
700720
float boundedY = Math.max(top, Math.min(bottom, event.getY() - getHandleOffset()));
701-
scrollUtils.scrollToPositionAtProgress((boundedY - top) / (bottom - top));
702-
scrollUtils.scrollHandleAndIndicator();
703-
recyclerView.onScrolled(0, 0);
721+
722+
float currentScrollPercent = (boundedY - top) / (bottom - top);
723+
if (isScrollChangeLargeEnoughForFastScroll(currentScrollPercent) ||
724+
currentScrollPercent == 0 || currentScrollPercent == 1) {
725+
previousScrollPercent = currentScrollPercent;
726+
scrollUtils.scrollToPositionAtProgress(currentScrollPercent);
727+
scrollUtils.scrollHandleAndIndicator();
728+
recyclerView.onScrolled(0, 0);
729+
}
704730

705731
if (lightOnTouch) {
706732
handleThumb.setBackgroundColor(handleColour);

0 commit comments

Comments
 (0)