40
40
import android .view .animation .TranslateAnimation ;
41
41
import android .widget .RelativeLayout ;
42
42
43
+ import java .util .ArrayList ;
44
+
43
45
/*
44
46
* Table Of Contents:
45
47
*
@@ -90,6 +92,8 @@ public abstract class MaterialScrollBar<T> extends RelativeLayout {
90
92
private OnLayoutChangeListener indicatorLayoutListener ;
91
93
private float previousScrollPercent = 0 ;
92
94
Boolean draggableFromAnywhere = false ;
95
+ ArrayList <Runnable > onAttach = new ArrayList <>();
96
+ private boolean attached = false ;
93
97
94
98
//CHAPTER I - INITIAL SETUP
95
99
@@ -122,12 +126,6 @@ public abstract class MaterialScrollBar<T> extends RelativeLayout {
122
126
addView (setUpHandle (context , a .getBoolean (R .styleable .MaterialScrollBar_msb_lightOnTouch , true ))); //Adds the handle
123
127
124
128
setRightToLeft (Utils .isRightToLeft (context )); //Detects and applies the Right-To-Left status of the app
125
-
126
- implementPreferences ();
127
-
128
- implementFlavourPreferences (a );
129
-
130
- a .recycle ();
131
129
}
132
130
133
131
//Unpacks XML attributes and ensures that no mandatory attributes are missing, then applies them.
@@ -167,14 +165,14 @@ Handle setUpHandle(Context context, Boolean lightOnTouch){
167
165
handleThumb .setLayoutParams (lp );
168
166
169
167
this .lightOnTouch = lightOnTouch ;
170
- int colourToSet ;
168
+ int colorToSet ;
171
169
handleColour = fetchAccentColour (context );
172
170
if (lightOnTouch ){
173
- colourToSet = Color .parseColor ("#9c9c9c" );
171
+ colorToSet = Color .parseColor ("#9c9c9c" );
174
172
} else {
175
- colourToSet = handleColour ;
173
+ colorToSet = handleColour ;
176
174
}
177
- handleThumb .setBackgroundColor (colourToSet );
175
+ handleThumb .setBackgroundColor (colorToSet );
178
176
return handleThumb ;
179
177
}
180
178
@@ -217,8 +215,10 @@ public T setRecyclerView(RecyclerView rv){
217
215
protected void onAttachedToWindow () {
218
216
super .onAttachedToWindow ();
219
217
218
+ attached = true ;
219
+
220
220
if (seekId != 0 ){
221
- recyclerView = ( RecyclerView ) getRootView ().findViewById (seekId );
221
+ recyclerView = getRootView ().findViewById (seekId );
222
222
generalSetup ();
223
223
}
224
224
}
@@ -228,12 +228,22 @@ private void generalSetup(){
228
228
recyclerView .setVerticalScrollBarEnabled (false ); // disable any existing scrollbars
229
229
recyclerView .addOnScrollListener (new scrollListener ()); // lets us read when the recyclerView scrolls
230
230
231
+ implementPreferences ();
232
+
233
+ implementFlavourPreferences (a );
234
+
235
+ a .recycle ();
236
+
231
237
setTouchIntercept (); // catches touches on the bar
232
238
233
239
identifySwipeRefreshParents ();
234
240
235
241
checkCustomScrolling ();
236
242
243
+ for (int i = 0 ; i < onAttach .size (); i ++) {
244
+ onAttach .get (i ).run ();
245
+ }
246
+
237
247
//Hides the view
238
248
TranslateAnimation anim = new TranslateAnimation (
239
249
Animation .RELATIVE_TO_PARENT , 0.0f ,
@@ -399,31 +409,31 @@ public void onLayoutChange(View v, int left, int top, int right, int bottom, int
399
409
}
400
410
401
411
/**
402
- * Provides the ability to programmatically set the colour of the scrollbar handleThumb.
403
- * @param colour to set the handleThumb.
412
+ * Provides the ability to programmatically set the color of the scrollbar handleThumb.
413
+ * @param color to set the handleThumb.
404
414
*/
405
- public T setHandleColour (String colour ){
406
- handleColour = Color .parseColor (colour );
415
+ public T setHandleColour (final String color ){
416
+ handleColour = Color .parseColor (color );
407
417
setHandleColour ();
408
418
return (T )this ;
409
419
}
410
420
411
421
/**
412
- * Provides the ability to programmatically set the colour of the scrollbar handleThumb.
413
- * @param colour to set the handleThumb.
422
+ * Provides the ability to programmatically set the color of the scrollbar handleThumb.
423
+ * @param color to set the handleThumb.
414
424
*/
415
- public T setHandleColour (@ ColorInt int colour ){
416
- handleColour = colour ;
425
+ public T setHandleColour (@ ColorInt final int color ){
426
+ handleColour = color ;
417
427
setHandleColour ();
418
428
return (T )this ;
419
429
}
420
430
421
431
/**
422
- * Provides the ability to programmatically set the colour of the scrollbar handleThumb.
423
- * @param colourResId to set the handleThumb.
432
+ * Provides the ability to programmatically set the color of the scrollbar handleThumb.
433
+ * @param colorResId to set the handleThumb.
424
434
*/
425
- public T setHandleColourRes (@ ColorRes int colourResId ){
426
- handleColour = ContextCompat .getColor (getContext (), colourResId );
435
+ public T setHandleColourRes (@ ColorRes final int colorResId ){
436
+ handleColour = ContextCompat .getColor (getContext (), colorResId );
427
437
setHandleColour ();
428
438
return (T )this ;
429
439
}
@@ -438,74 +448,74 @@ private void setHandleColour(){
438
448
}
439
449
440
450
/**
441
- * Provides the ability to programmatically set the colour of the scrollbar handleThumb when unpressed. Only applies if lightOnTouch is true.
442
- * @param colour to set the handleThumb when unpressed.
451
+ * Provides the ability to programmatically set the color of the scrollbar handleThumb when unpressed. Only applies if lightOnTouch is true.
452
+ * @param color to set the handleThumb when unpressed.
443
453
*/
444
- public T setHandleOffColour (String colour ){
445
- handleOffColour = Color .parseColor (colour );
454
+ public T setHandleOffColour (final String color ){
455
+ handleOffColour = Color .parseColor (color );
446
456
if (lightOnTouch ){
447
457
handleThumb .setBackgroundColor (handleOffColour );
448
458
}
449
459
return (T )this ;
450
460
}
451
461
452
462
/**
453
- * Provides the ability to programmatically set the colour of the scrollbar handleThumb when unpressed. Only applies if lightOnTouch is true.
454
- * @param colour to set the handleThumb when unpressed.
463
+ * Provides the ability to programmatically set the color of the scrollbar handleThumb when unpressed. Only applies if lightOnTouch is true.
464
+ * @param color to set the handleThumb when unpressed.
455
465
*/
456
- public T setHandleOffColour (@ ColorInt int colour ){
457
- handleOffColour = colour ;
466
+ public T setHandleOffColour (@ ColorInt final int color ){
467
+ handleOffColour = color ;
458
468
if (lightOnTouch ){
459
469
handleThumb .setBackgroundColor (handleOffColour );
460
470
}
461
471
return (T )this ;
462
472
}
463
473
464
474
/**
465
- * Provides the ability to programmatically set the colour of the scrollbar handleThumb when unpressed. Only applies if lightOnTouch is true.
466
- * @param colourResId to set the handleThumb when unpressed.
475
+ * Provides the ability to programmatically set the color of the scrollbar handleThumb when unpressed. Only applies if lightOnTouch is true.
476
+ * @param colorResId to set the handleThumb when unpressed.
467
477
*/
468
- public T setHandleOffColourRes (@ ColorRes int colourResId ){
469
- handleOffColour = ContextCompat .getColor (getContext (), colourResId );
478
+ public T setHandleOffColourRes (@ ColorRes final int colorResId ){
479
+ handleOffColour = ContextCompat .getColor (getContext (), colorResId );
470
480
if (lightOnTouch ){
471
481
handleThumb .setBackgroundColor (handleOffColour );
472
482
}
473
483
return (T )this ;
474
484
}
475
485
476
486
/**
477
- * Provides the ability to programmatically set the colour of the scrollbar.
478
- * @param colour to set the bar.
487
+ * Provides the ability to programmatically set the color of the scrollbar.
488
+ * @param color to set the bar.
479
489
*/
480
- public T setBarColour (String colour ){
481
- handleTrack .setBackgroundColor (Color .parseColor (colour ));
490
+ public T setBarColour (final String color ){
491
+ handleTrack .setBackgroundColor (Color .parseColor (color ));
482
492
return (T )this ;
483
493
}
484
494
485
495
/**
486
- * Provides the ability to programmatically set the colour of the scrollbar.
487
- * @param colour to set the bar.
496
+ * Provides the ability to programmatically set the color of the scrollbar.
497
+ * @param color to set the bar.
488
498
*/
489
- public T setBarColour (@ ColorInt int colour ){
490
- handleTrack .setBackgroundColor (colour );
499
+ public T setBarColour (@ ColorInt final int color ){
500
+ handleTrack .setBackgroundColor (color );
491
501
return (T )this ;
492
502
}
493
503
494
504
/**
495
- * Provides the ability to programmatically set the colour of the scrollbar.
496
- * @param colourResId to set the bar.
505
+ * Provides the ability to programmatically set the color of the scrollbar.
506
+ * @param colorResId to set the bar.
497
507
*/
498
- public T setBarColourRes (@ ColorRes int colourResId ){
499
- handleTrack .setBackgroundColor (ContextCompat .getColor (getContext (), colourResId ));
508
+ public T setBarColourRes (@ ColorRes final int colorResId ){
509
+ handleTrack .setBackgroundColor (ContextCompat .getColor (getContext (), colorResId ));
500
510
return (T )this ;
501
511
}
502
512
503
513
/**
504
- * Provides the ability to programmatically set the text colour of the indicator. Will do nothing if there is no section indicator.
505
- * @param colour to set the text of the indicator.
514
+ * Provides the ability to programmatically set the text color of the indicator. Will do nothing if there is no section indicator.
515
+ * @param color to set the text of the indicator.
506
516
*/
507
- public T setTextColour (@ ColorInt int colour ){
508
- textColour = colour ;
517
+ public T setTextColour (@ ColorInt final int color ){
518
+ textColour = color ;
509
519
if (indicator != null ){
510
520
indicator .setTextColour (textColour );
511
521
}
@@ -514,23 +524,23 @@ public T setTextColour(@ColorInt int colour){
514
524
515
525
516
526
/**
517
- * Provides the ability to programmatically set the text colour of the indicator. Will do nothing if there is no section indicator.
518
- * @param colourResId to set the text of the indicator.
527
+ * Provides the ability to programmatically set the text color of the indicator. Will do nothing if there is no section indicator.
528
+ * @param colorResId to set the text of the indicator.
519
529
*/
520
- public T setTextColourRes (@ ColorRes int colourResId ){
521
- textColour = ContextCompat .getColor (getContext (), colourResId );
530
+ public T setTextColourRes (@ ColorRes final int colorResId ){
531
+ textColour = ContextCompat .getColor (getContext (), colorResId );
522
532
if (indicator != null ){
523
533
indicator .setTextColour (textColour );
524
534
}
525
535
return (T )this ;
526
536
}
527
537
528
538
/**
529
- * Provides the ability to programmatically set the text colour of the indicator. Will do nothing if there is no section indicator.
530
- * @param colour to set the text of the indicator.
539
+ * Provides the ability to programmatically set the text color of the indicator. Will do nothing if there is no section indicator.
540
+ * @param color to set the text of the indicator.
531
541
*/
532
- public T setTextColour (String colour ){
533
- textColour = Color .parseColor (colour );
542
+ public T setTextColour (final String color ){
543
+ textColour = Color .parseColor (color );
534
544
if (indicator != null ){
535
545
indicator .setTextColour (textColour );
536
546
}
@@ -586,7 +596,15 @@ private void setupIndicator(Indicator indicator, boolean addSpaceSide){
586
596
* Allows the developer to set a custom bar thickness.
587
597
* @param thickness The desired bar thickness.
588
598
*/
589
- public T setBarThickness (int thickness ){
599
+ public T setBarThickness (final int thickness ){
600
+ if (!attached ) {
601
+ onAttach .add (new Runnable () {
602
+ @ Override
603
+ public void run () {
604
+ setBarThickness (thickness );
605
+ }
606
+ });
607
+ }
590
608
LayoutParams layoutParams = (LayoutParams ) handleThumb .getLayoutParams ();
591
609
layoutParams .width = thickness ;
592
610
handleThumb .setLayoutParams (layoutParams );
@@ -639,7 +657,7 @@ public void setDraggableFromAnywhere(boolean draggableFromAnywhere){
639
657
640
658
//CHAPTER IV - MISC METHODS
641
659
642
- //Fetch accent colour .
660
+ //Fetch accent color .
643
661
static int fetchAccentColour (Context context ) {
644
662
TypedValue typedValue = new TypedValue ();
645
663
0 commit comments