Skip to content

Commit 49df655

Browse files
committed
Update deprecated RecyclerView.OnScrollListener
Changed calculation of proportion value for smooth and correct Bubble and Handle position
1 parent 131933b commit 49df655

1 file changed

Lines changed: 14 additions & 26 deletions

File tree

  • app/src/main/java/com/lb/lollipop_contacts_recyclerview_fast_scroller

app/src/main/java/com/lb/lollipop_contacts_recyclerview_fast_scroller/FastScroller.java

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,13 @@
1717

1818
import com.lb.lollipopcontactsrecyclerviewfastscroller.R;
1919

20-
import static android.support.v7.widget.RecyclerView.OnScrollListener;
21-
2220
public class FastScroller extends LinearLayout{
2321
private static final int BUBBLE_ANIMATION_DURATION=100;
2422
private static final int TRACK_SNAP_RANGE=5;
2523

2624
private TextView bubble;
2725
private View handle;
2826
private RecyclerView recyclerView;
29-
private final ScrollListener scrollListener=new ScrollListener();
3027
private int height;
3128

3229
private ObjectAnimator currentAnimator=null;
@@ -98,7 +95,20 @@ public boolean onTouchEvent(@NonNull MotionEvent event)
9895
public void setRecyclerView(RecyclerView recyclerView)
9996
{
10097
this.recyclerView=recyclerView;
101-
recyclerView.setOnScrollListener(scrollListener);
98+
RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener()
99+
{
100+
@Override
101+
public void onScrolled(final RecyclerView recyclerView, final int dx, final int dy)
102+
{
103+
if (handle.isSelected())
104+
return;
105+
int verticalScrollOffset = recyclerView.computeVerticalScrollOffset();
106+
int verticalScrollRange = recyclerView.computeVerticalScrollRange();
107+
float proportion = (float) verticalScrollOffset / ((float) verticalScrollRange - height);
108+
setBubbleAndHandlePosition(height * proportion);
109+
}
110+
};
111+
recyclerView.addOnScrollListener(onScrollListener);
102112
}
103113

104114
private void setRecyclerViewPosition(float y)
@@ -115,7 +125,6 @@ else if(handle.getY()+handle.getHeight()>=height-TRACK_SNAP_RANGE)
115125
proportion=y/(float)height;
116126
int targetPos=getValueInRange(0,itemCount-1,(int)(proportion*(float)itemCount));
117127
((LinearLayoutManager)recyclerView.getLayoutManager()).scrollToPositionWithOffset(targetPos,0);
118-
// recyclerView.oPositionWithOffset(targetPos);
119128
String bubbleText=((BubbleTextGetter)recyclerView.getAdapter()).getTextToShowInBubble(targetPos);
120129
bubble.setText(bubbleText);
121130
}
@@ -169,25 +178,4 @@ public void onAnimationCancel(Animator animation)
169178
});
170179
currentAnimator.start();
171180
}
172-
173-
private class ScrollListener extends OnScrollListener{
174-
@Override
175-
public void onScrolled(RecyclerView rv,int dx,int dy)
176-
{
177-
View firstVisibleView=recyclerView.getChildAt(0);
178-
int firstVisiblePosition=recyclerView.getChildPosition(firstVisibleView);
179-
int visibleRange=recyclerView.getChildCount();
180-
int lastVisiblePosition=firstVisiblePosition+visibleRange;
181-
int itemCount=recyclerView.getAdapter().getItemCount();
182-
int position;
183-
if(firstVisiblePosition==0)
184-
position=0;
185-
else if(lastVisiblePosition==itemCount)
186-
position=itemCount;
187-
else
188-
position=(int)(((float)firstVisiblePosition/(((float)itemCount-(float)visibleRange)))*(float)itemCount);
189-
float proportion=(float)position/(float)itemCount;
190-
setBubbleAndHandlePosition(height*proportion);
191-
}
192-
}
193181
}

0 commit comments

Comments
 (0)