Skip to content

Commit 69441f7

Browse files
committed
Merge pull request #6 from h6ah4i/fix_handler_position_on_screen_rotation
Fix scroller handle position is not preserved bug (issue #3)
2 parents 023f492 + 2ac8166 commit 69441f7

2 files changed

Lines changed: 23 additions & 10 deletions

File tree

Application/src/main/java/com/example/recyclerviewfastscroller/ui/scroller/AbsRecyclerViewFastScroller.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,24 @@ public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
189189
return mOnScrollListener;
190190
}
191191

192+
@Override
193+
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
194+
super.onLayout(changed, left, top, right, bottom);
195+
196+
if (getScrollProgressCalculator() == null) {
197+
onCreateScrollProgressCalculator();
198+
}
199+
200+
// synchronize the handle position to the RecyclerView
201+
float scrollProgress = getScrollProgressCalculator().calculateScrollProgress(mRecyclerView);
202+
moveHandleToPosition(scrollProgress);
203+
}
204+
205+
/**
206+
* Sub classes have to override this method and create the ScrollProgressCalculator instance in this method.
207+
*/
208+
protected abstract void onCreateScrollProgressCalculator();
209+
192210
/**
193211
* Takes a touch event and determines how much scroll progress this translates into
194212
* @param event touch event received by the layout

Application/src/main/java/com/example/recyclerviewfastscroller/ui/scroller/vertical/VerticalRecyclerViewFastScroller.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,10 @@ public void moveHandleToPosition(float scrollProgress) {
5050
mHandle.setY(mScreenPositionCalculator.getYPositionFromScrollProgress(scrollProgress));
5151
}
5252

53-
@Override
54-
public void onLayout(boolean changed, int left, int top, int right, int bottom) {
55-
super.onLayout(changed, left, top, right, bottom);
56-
if (mScrollProgressCalculator == null || mScreenPositionCalculator == null) {
57-
VerticalScrollBoundsProvider boundsProvider =
58-
new VerticalScrollBoundsProvider(mBar.getY(), mBar.getY() + mBar.getHeight() - mHandle.getHeight());
59-
mScrollProgressCalculator = new VerticalLinearLayoutManagerScrollProgressCalculator(boundsProvider);
60-
mScreenPositionCalculator = new VerticalScreenPositionCalculator(boundsProvider);
61-
}
53+
protected void onCreateScrollProgressCalculator() {
54+
VerticalScrollBoundsProvider boundsProvider =
55+
new VerticalScrollBoundsProvider(mBar.getY(), mBar.getY() + mBar.getHeight() - mHandle.getHeight());
56+
mScrollProgressCalculator = new VerticalLinearLayoutManagerScrollProgressCalculator(boundsProvider);
57+
mScreenPositionCalculator = new VerticalScreenPositionCalculator(boundsProvider);
6258
}
63-
6459
}

0 commit comments

Comments
 (0)