24
24
import android .graphics .Color ;
25
25
import android .graphics .drawable .GradientDrawable ;
26
26
import android .os .Build ;
27
+ import android .os .Handler ;
28
+ import android .os .Looper ;
27
29
import android .support .v4 .content .ContextCompat ;
28
30
import android .support .v7 .widget .LinearLayoutManager ;
29
31
import android .support .v7 .widget .RecyclerView ;
@@ -54,46 +56,15 @@ public class MaterialScrollBar extends RelativeLayout {
54
56
private boolean lightOnTouch ;
55
57
private boolean totallyHidden = false ;
56
58
MaterialScrollBar .ScrollListener scrollListener ;
59
+ private Handler mUIHandler = new Handler (Looper .getMainLooper ());
57
60
58
- //Thread which checks every 1/10th of a second to decide if the scrollBar should slide away.
59
- class BarFade extends Thread {
60
-
61
- MaterialScrollBar materialScrollBar ;
62
-
63
- BarFade (MaterialScrollBar msb ){
64
- materialScrollBar = msb ;
65
- }
66
-
67
- //Variable which is later set to indicate the time after which the scrollBar should disappear
68
- long time = 0 ;
69
-
70
- //Variable which is set to false when dragging is occurring and true when dragging is stopped.
71
- boolean run = false ;
72
- boolean held = false ;
61
+ private Runnable mFadeBar = new Runnable () {
73
62
74
63
@ Override
75
64
public void run () {
76
- try {
77
- while (true ){
78
- //Is it past the time where the bar should be animated away AND is no scrolling occurring?
79
- if (run && !held && time <= System .currentTimeMillis ()){
80
- run = false ;
81
- materialScrollBar .a .runOnUiThread (new Runnable () {
82
- @ Override
83
- public void run () {
84
- materialScrollBar .fadeOut ();
85
- }
86
- });
87
- }
88
- Thread .sleep (100 );
89
- }
90
- } catch (InterruptedException e ){
91
- e .printStackTrace ();
92
- }
65
+ fadeOut ();
93
66
}
94
- }
95
-
96
- private BarFade fade ;
67
+ };
97
68
98
69
/**
99
70
* For testing only. Should not generally be accessed.
@@ -125,7 +96,7 @@ public MaterialScrollBar(Context context, RecyclerView recyclerView, boolean lig
125
96
LayoutParams lp = new RelativeLayout .LayoutParams (Utils .getDP (12 , this ), LayoutParams .MATCH_PARENT );
126
97
lp .addRule (ALIGN_PARENT_RIGHT );
127
98
background .setLayoutParams (lp );
128
- background .setBackgroundColor (getResources () .getColor (android .R .color .darker_gray ));
99
+ background .setBackgroundColor (ContextCompat .getColor (context , android .R .color .darker_gray ));
129
100
ViewHelper .setAlpha (background , 0.4F );
130
101
131
102
handle = new View (context );
@@ -163,9 +134,6 @@ public MaterialScrollBar(Context context, RecyclerView recyclerView, boolean lig
163
134
164
135
setTouchIntercept ();
165
136
166
- fade = new BarFade (this );
167
- fade .start ();
168
-
169
137
TranslateAnimation anim = new TranslateAnimation (Animation .RELATIVE_TO_PARENT , 0.0f , Animation .RELATIVE_TO_SELF , 1.0f , Animation .RELATIVE_TO_PARENT , 0.0f , Animation .RELATIVE_TO_PARENT , 0.0f );
170
138
anim .setFillAfter (true );
171
139
startAnimation (anim );
@@ -188,8 +156,7 @@ public boolean onTouch(View v, MotionEvent event) {
188
156
handle .setBackgroundColor (handleColour );
189
157
}
190
158
191
- fade .held = true ;
192
-
159
+ mUIHandler .removeCallbacks (mFadeBar );
193
160
fadeIn ();
194
161
} else {
195
162
if (indicator != null && indicator .getVisibility () == VISIBLE ){
@@ -204,10 +171,9 @@ public boolean onTouch(View v, MotionEvent event) {
204
171
}
205
172
206
173
if (hide ) {
207
- fade . run = true ;
208
- fade . time = System . currentTimeMillis () + hideDuration ;
174
+ mUIHandler . removeCallbacks ( mFadeBar ) ;
175
+ mUIHandler . postDelayed ( mFadeBar , hideDuration ) ;
209
176
}
210
- fade .held = false ;
211
177
}
212
178
return true ;
213
179
}
@@ -371,12 +337,10 @@ public MaterialScrollBar setTextColour(String colour){
371
337
*/
372
338
public MaterialScrollBar setAutoHide (Boolean hide ){
373
339
if (!hide ){
374
- fade . interrupt ( );
340
+ mUIHandler . removeCallbacks ( mFadeBar );
375
341
TranslateAnimation anim = new TranslateAnimation (Animation .RELATIVE_TO_PARENT , 0.0f , Animation .RELATIVE_TO_SELF , 0.0f , Animation .RELATIVE_TO_PARENT , 0.0f , Animation .RELATIVE_TO_PARENT , 0.0f );
376
342
anim .setFillAfter (true );
377
343
startAnimation (anim );
378
- } else if (!this .hide ){
379
- fade .start ();
380
344
}
381
345
this .hide = hide ;
382
346
return this ;
@@ -518,11 +482,27 @@ public void onScrollStateChanged(final RecyclerView recyclerView, int newState)
518
482
519
483
if (hide ){
520
484
if (newState == RecyclerView .SCROLL_STATE_IDLE ){
521
- fade . time = System . currentTimeMillis () + hideDuration ;
522
- fade . run = true ;
485
+ mUIHandler . removeCallbacks ( mFadeBar ) ;
486
+ mUIHandler . postDelayed ( mFadeBar , hideDuration ) ;
523
487
} else if (newState == RecyclerView .SCROLL_STATE_DRAGGING ) {
524
- fade .run = false ;
525
- materialScrollBar .fadeIn ();
488
+ if (Build .VERSION .SDK_INT >= 14 ){
489
+ if (recyclerView .canScrollVertically (1 ) || recyclerView .canScrollVertically (-1 ) || recyclerView .canScrollHorizontally (1 ) || recyclerView .canScrollHorizontally (-1 )) {
490
+ mUIHandler .removeCallbacks (mFadeBar );
491
+ materialScrollBar .fadeIn ();
492
+ }
493
+ } else {
494
+ View visibleChild = recyclerView .getChildAt (0 );
495
+ RecyclerView .ViewHolder holder = recyclerView .getChildViewHolder (visibleChild );
496
+ int itemHeight = holder .itemView .getHeight ();
497
+ int recyclerHeight = recyclerView .getHeight ();
498
+ int itemsInWindow = recyclerHeight / itemHeight ;
499
+
500
+ int numItemsInList = recyclerView .getAdapter ().getItemCount ();
501
+ if (numItemsInList > itemsInWindow ){
502
+ mUIHandler .removeCallbacks (mFadeBar );
503
+ materialScrollBar .fadeIn ();
504
+ }
505
+ }
526
506
}
527
507
}
528
508
}
0 commit comments