17
17
18
18
import com .lb .lollipopcontactsrecyclerviewfastscroller .R ;
19
19
20
- import static android .support .v7 .widget .RecyclerView .OnScrollListener ;
21
-
22
20
public class FastScroller extends LinearLayout {
23
21
private static final int BUBBLE_ANIMATION_DURATION =100 ;
24
22
private static final int TRACK_SNAP_RANGE =5 ;
25
23
26
24
private TextView bubble ;
27
25
private View handle ;
28
26
private RecyclerView recyclerView ;
29
- private final ScrollListener scrollListener =new ScrollListener ();
30
27
private int height ;
31
28
32
29
private ObjectAnimator currentAnimator =null ;
@@ -98,7 +95,18 @@ public boolean onTouchEvent(@NonNull MotionEvent event)
98
95
public void setRecyclerView (RecyclerView recyclerView )
99
96
{
100
97
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
+ float proportion = (float ) recyclerView .computeVerticalScrollOffset () / ((float ) recyclerView .computeVerticalScrollRange () - height );
106
+ setBubbleAndHandlePosition (height * proportion );
107
+ }
108
+ };
109
+ recyclerView .addOnScrollListener (onScrollListener );
102
110
}
103
111
104
112
private void setRecyclerViewPosition (float y )
@@ -115,7 +123,6 @@ else if(handle.getY()+handle.getHeight()>=height-TRACK_SNAP_RANGE)
115
123
proportion =y /(float )height ;
116
124
int targetPos =getValueInRange (0 ,itemCount -1 ,(int )(proportion *(float )itemCount ));
117
125
((LinearLayoutManager )recyclerView .getLayoutManager ()).scrollToPositionWithOffset (targetPos ,0 );
118
- // recyclerView.oPositionWithOffset(targetPos);
119
126
String bubbleText =((BubbleTextGetter )recyclerView .getAdapter ()).getTextToShowInBubble (targetPos );
120
127
bubble .setText (bubbleText );
121
128
}
@@ -169,25 +176,4 @@ public void onAnimationCancel(Animator animation)
169
176
});
170
177
currentAnimator .start ();
171
178
}
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
- }
193
179
}
0 commit comments