Skip to content

Commit

Permalink
[Feature] Add checks requiring intrinsic size of track and thumb.
Browse files Browse the repository at this point in the history
Bug: #30
  • Loading branch information
zhanghai committed Jun 20, 2021
1 parent dce089d commit 17ab093
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ For more customization, please use the methods on [`FastScrollerBuilder`](librar
- `setViewHelper()` allows providing a custom `ViewHelper` to support more views.
- `setPopupTextProvider()` allows providing a custom `PopupTextProvider` if your `RecyclerView.Adapter` cannot implement that interface.
- `setPadding()` allows setting a custom padding for the scrollbar, instead of the padding of the view.
- `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.
- `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.
- `setPopupStyle()` allows customizing the popup view with a lambda that will receive the view.
- `setAnimationHelper()` allows providing a custom `AnimationHelper` to use an alternative scrollbar animation.
- `disableScrollbarAutoHide()` allows disabling the auto hide animation for scrollbar. This implies using a `DefaultAnimationHelper`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,12 @@ public FastScroller(@NonNull ViewGroup view, @NonNull ViewHelper viewHelper,
mUserPadding = padding;
mAnimationHelper = animationHelper;

mTrackWidth = trackDrawable.getIntrinsicWidth();
mThumbWidth = thumbDrawable.getIntrinsicWidth();
mThumbHeight = thumbDrawable.getIntrinsicHeight();
mTrackWidth = requireNonNegative(trackDrawable.getIntrinsicWidth(),
"trackDrawable.getIntrinsicWidth() < 0");
mThumbWidth = requireNonNegative(thumbDrawable.getIntrinsicWidth(),
"thumbDrawable.getIntrinsicWidth() < 0");
mThumbHeight = requireNonNegative(thumbDrawable.getIntrinsicHeight(),
"thumbDrawable.getIntrinsicHeight() < 0");

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

private static int requireNonNegative(int value, @NonNull String message) {
if (value < 0) {
throw new IllegalArgumentException(message);
}
return value;
}

public void setPadding(int left, int top, int right, int bottom) {
if (mUserPadding != null && mUserPadding.left == left && mUserPadding.top == top
&& mUserPadding.right == right && mUserPadding.bottom == bottom) {
Expand Down

0 comments on commit 17ab093

Please sign in to comment.