Skip to content

Commit 17ab093

Browse files
committed
[Feature] Add checks requiring intrinsic size of track and thumb.
Bug: #30
1 parent dce089d commit 17ab093

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ For more customization, please use the methods on [`FastScrollerBuilder`](librar
7171
- `setViewHelper()` allows providing a custom `ViewHelper` to support more views.
7272
- `setPopupTextProvider()` allows providing a custom `PopupTextProvider` if your `RecyclerView.Adapter` cannot implement that interface.
7373
- `setPadding()` allows setting a custom padding for the scrollbar, instead of the padding of the view.
74-
- `setTrackDrawable()` and `setThumbDrawable()` allow setting custom drawables for the scrollbar. The `android:state_pressed` state will be updated for them so you can use a selector.
74+
- `setTrackDrawable()` and `setThumbDrawable()` allow setting custom drawables for the scrollbar. The `android:state_pressed` state will be updated for them so you can use a selector. The track drawable needs to have an intrinsic width and the thumb drawable needs to have an intrinsic size, in order to allow proper touch event handling.
7575
- `setPopupStyle()` allows customizing the popup view with a lambda that will receive the view.
7676
- `setAnimationHelper()` allows providing a custom `AnimationHelper` to use an alternative scrollbar animation.
7777
- `disableScrollbarAutoHide()` allows disabling the auto hide animation for scrollbar. This implies using a `DefaultAnimationHelper`.

library/src/main/java/me/zhanghai/android/fastscroll/FastScroller.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,12 @@ public FastScroller(@NonNull ViewGroup view, @NonNull ViewHelper viewHelper,
9393
mUserPadding = padding;
9494
mAnimationHelper = animationHelper;
9595

96-
mTrackWidth = trackDrawable.getIntrinsicWidth();
97-
mThumbWidth = thumbDrawable.getIntrinsicWidth();
98-
mThumbHeight = thumbDrawable.getIntrinsicHeight();
96+
mTrackWidth = requireNonNegative(trackDrawable.getIntrinsicWidth(),
97+
"trackDrawable.getIntrinsicWidth() < 0");
98+
mThumbWidth = requireNonNegative(thumbDrawable.getIntrinsicWidth(),
99+
"thumbDrawable.getIntrinsicWidth() < 0");
100+
mThumbHeight = requireNonNegative(thumbDrawable.getIntrinsicHeight(),
101+
"thumbDrawable.getIntrinsicHeight() < 0");
99102

100103
mTrackView = new View(context);
101104
mTrackView.setBackground(trackDrawable);
@@ -119,6 +122,13 @@ public FastScroller(@NonNull ViewGroup view, @NonNull ViewHelper viewHelper,
119122
mViewHelper.addOnTouchEventListener(this::onTouchEvent);
120123
}
121124

125+
private static int requireNonNegative(int value, @NonNull String message) {
126+
if (value < 0) {
127+
throw new IllegalArgumentException(message);
128+
}
129+
return value;
130+
}
131+
122132
public void setPadding(int left, int top, int right, int bottom) {
123133
if (mUserPadding != null && mUserPadding.left == left && mUserPadding.top == top
124134
&& mUserPadding.right == right && mUserPadding.bottom == bottom) {

0 commit comments

Comments
 (0)