Skip to content

Commit bc123da

Browse files
committed
Fix crash on setting custom thickness.
Closes #100
1 parent 521e19b commit bc123da

File tree

4 files changed

+96
-78
lines changed

4 files changed

+96
-78
lines changed

app/build.gradle

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
apply plugin: 'com.android.application'
22

33
android {
4-
compileSdkVersion 25
4+
compileSdkVersion 26
55
buildToolsVersion '25.0.3'
66

77
defaultConfig {
88
applicationId "com.turingtechnologies.materialscrollbardemo"
9-
minSdkVersion 11
10-
targetSdkVersion 25
9+
minSdkVersion 14
10+
targetSdkVersion 26
1111
versionCode 6
1212
versionName "6.0"
1313
}
@@ -20,9 +20,9 @@ android {
2020
}
2121

2222
dependencies {
23-
implementation project(':lib')
24-
implementation "com.android.support:recyclerview-v7:${supportLibVersion}"
25-
implementation 'com.pnikosis:materialish-progress:1.7'
26-
implementation "com.android.support:design:${supportLibVersion}"
27-
implementation "com.android.support:appcompat-v7:${supportLibVersion}"
23+
compile project(':lib')
24+
compile "com.android.support:recyclerview-v7:${supportLibVersion}"
25+
compile 'com.pnikosis:materialish-progress:1.7'
26+
compile "com.android.support:design:${supportLibVersion}"
27+
compile "com.android.support:appcompat-v7:${supportLibVersion}"
2828
}

build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ buildscript {
88
}
99
dependencies {
1010
classpath 'com.github.ben-manes:gradle-versions-plugin:0.15.0'
11-
classpath 'com.android.tools.build:gradle:3.0.0-alpha6'
11+
classpath 'com.android.tools.build:gradle:2.3.3'
1212

1313
// NOTE: Do not place your application dependencies here; they belong
1414
// in the individual module build.gradle files
@@ -19,8 +19,8 @@ allprojects {
1919
repositories {
2020
jcenter()
2121
maven { url 'https://jitpack.io' }
22-
maven { url "https://maven.google.com" }
22+
google()
2323
}
2424

25-
project.ext.supportLibVersion = '25.4.0'
25+
project.ext.supportLibVersion = '26.0.0'
2626
}

lib/build.gradle

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
apply plugin: 'com.android.library'
22

3-
def libVersion = '12.5.0'
3+
def libVersion = '12.5.1'
44

55
android {
6-
compileSdkVersion 25
6+
compileSdkVersion 26
77
buildToolsVersion '25.0.3'
88

99
defaultConfig {
10-
minSdkVersion 11
11-
targetSdkVersion 25
10+
minSdkVersion 14
11+
targetSdkVersion 26
1212
versionName libVersion
1313
}
1414
}
@@ -38,6 +38,6 @@ artifacts {
3838
}
3939

4040
dependencies {
41-
implementation "com.android.support:recyclerview-v7:${supportLibVersion}"
42-
implementation "com.android.support:design:${supportLibVersion}"
41+
compile "com.android.support:recyclerview-v7:${supportLibVersion}"
42+
compile "com.android.support:design:${supportLibVersion}"
4343
}

lib/src/main/java/com/turingtechnologies/materialscrollbar/MaterialScrollBar.java

Lines changed: 79 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
import android.view.animation.TranslateAnimation;
4141
import android.widget.RelativeLayout;
4242

43+
import java.util.ArrayList;
44+
4345
/*
4446
* Table Of Contents:
4547
*
@@ -90,6 +92,8 @@ public abstract class MaterialScrollBar<T> extends RelativeLayout {
9092
private OnLayoutChangeListener indicatorLayoutListener;
9193
private float previousScrollPercent = 0;
9294
Boolean draggableFromAnywhere = false;
95+
ArrayList<Runnable> onAttach = new ArrayList<>();
96+
private boolean attached = false;
9397

9498
//CHAPTER I - INITIAL SETUP
9599

@@ -122,12 +126,6 @@ public abstract class MaterialScrollBar<T> extends RelativeLayout {
122126
addView(setUpHandle(context, a.getBoolean(R.styleable.MaterialScrollBar_msb_lightOnTouch, true))); //Adds the handle
123127

124128
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();
131129
}
132130

133131
//Unpacks XML attributes and ensures that no mandatory attributes are missing, then applies them.
@@ -167,14 +165,14 @@ Handle setUpHandle(Context context, Boolean lightOnTouch){
167165
handleThumb.setLayoutParams(lp);
168166

169167
this.lightOnTouch = lightOnTouch;
170-
int colourToSet;
168+
int colorToSet;
171169
handleColour = fetchAccentColour(context);
172170
if(lightOnTouch){
173-
colourToSet = Color.parseColor("#9c9c9c");
171+
colorToSet = Color.parseColor("#9c9c9c");
174172
} else {
175-
colourToSet = handleColour;
173+
colorToSet = handleColour;
176174
}
177-
handleThumb.setBackgroundColor(colourToSet);
175+
handleThumb.setBackgroundColor(colorToSet);
178176
return handleThumb;
179177
}
180178

@@ -217,8 +215,10 @@ public T setRecyclerView(RecyclerView rv){
217215
protected void onAttachedToWindow() {
218216
super.onAttachedToWindow();
219217

218+
attached = true;
219+
220220
if(seekId != 0){
221-
recyclerView = (RecyclerView) getRootView().findViewById(seekId);
221+
recyclerView = getRootView().findViewById(seekId);
222222
generalSetup();
223223
}
224224
}
@@ -228,12 +228,22 @@ private void generalSetup(){
228228
recyclerView.setVerticalScrollBarEnabled(false); // disable any existing scrollbars
229229
recyclerView.addOnScrollListener(new scrollListener()); // lets us read when the recyclerView scrolls
230230

231+
implementPreferences();
232+
233+
implementFlavourPreferences(a);
234+
235+
a.recycle();
236+
231237
setTouchIntercept(); // catches touches on the bar
232238

233239
identifySwipeRefreshParents();
234240

235241
checkCustomScrolling();
236242

243+
for(int i = 0; i < onAttach.size(); i++) {
244+
onAttach.get(i).run();
245+
}
246+
237247
//Hides the view
238248
TranslateAnimation anim = new TranslateAnimation(
239249
Animation.RELATIVE_TO_PARENT, 0.0f,
@@ -399,31 +409,31 @@ public void onLayoutChange(View v, int left, int top, int right, int bottom, int
399409
}
400410

401411
/**
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.
404414
*/
405-
public T setHandleColour(String colour){
406-
handleColour = Color.parseColor(colour);
415+
public T setHandleColour(final String color){
416+
handleColour = Color.parseColor(color);
407417
setHandleColour();
408418
return (T)this;
409419
}
410420

411421
/**
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.
414424
*/
415-
public T setHandleColour(@ColorInt int colour){
416-
handleColour = colour;
425+
public T setHandleColour(@ColorInt final int color){
426+
handleColour = color;
417427
setHandleColour();
418428
return (T)this;
419429
}
420430

421431
/**
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.
424434
*/
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);
427437
setHandleColour();
428438
return (T)this;
429439
}
@@ -438,74 +448,74 @@ private void setHandleColour(){
438448
}
439449

440450
/**
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.
443453
*/
444-
public T setHandleOffColour(String colour){
445-
handleOffColour = Color.parseColor(colour);
454+
public T setHandleOffColour(final String color){
455+
handleOffColour = Color.parseColor(color);
446456
if(lightOnTouch){
447457
handleThumb.setBackgroundColor(handleOffColour);
448458
}
449459
return (T)this;
450460
}
451461

452462
/**
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.
455465
*/
456-
public T setHandleOffColour(@ColorInt int colour){
457-
handleOffColour = colour;
466+
public T setHandleOffColour(@ColorInt final int color){
467+
handleOffColour = color;
458468
if(lightOnTouch){
459469
handleThumb.setBackgroundColor(handleOffColour);
460470
}
461471
return (T)this;
462472
}
463473

464474
/**
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.
467477
*/
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);
470480
if(lightOnTouch){
471481
handleThumb.setBackgroundColor(handleOffColour);
472482
}
473483
return (T)this;
474484
}
475485

476486
/**
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.
479489
*/
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));
482492
return (T)this;
483493
}
484494

485495
/**
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.
488498
*/
489-
public T setBarColour(@ColorInt int colour){
490-
handleTrack.setBackgroundColor(colour);
499+
public T setBarColour(@ColorInt final int color){
500+
handleTrack.setBackgroundColor(color);
491501
return (T)this;
492502
}
493503

494504
/**
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.
497507
*/
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));
500510
return (T)this;
501511
}
502512

503513
/**
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.
506516
*/
507-
public T setTextColour(@ColorInt int colour){
508-
textColour = colour;
517+
public T setTextColour(@ColorInt final int color){
518+
textColour = color;
509519
if(indicator != null){
510520
indicator.setTextColour(textColour);
511521
}
@@ -514,23 +524,23 @@ public T setTextColour(@ColorInt int colour){
514524

515525

516526
/**
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.
519529
*/
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);
522532
if(indicator != null){
523533
indicator.setTextColour(textColour);
524534
}
525535
return (T)this;
526536
}
527537

528538
/**
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.
531541
*/
532-
public T setTextColour(String colour){
533-
textColour = Color.parseColor(colour);
542+
public T setTextColour(final String color){
543+
textColour = Color.parseColor(color);
534544
if(indicator != null){
535545
indicator.setTextColour(textColour);
536546
}
@@ -586,7 +596,15 @@ private void setupIndicator(Indicator indicator, boolean addSpaceSide){
586596
* Allows the developer to set a custom bar thickness.
587597
* @param thickness The desired bar thickness.
588598
*/
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+
}
590608
LayoutParams layoutParams = (LayoutParams) handleThumb.getLayoutParams();
591609
layoutParams.width = thickness;
592610
handleThumb.setLayoutParams(layoutParams);
@@ -639,7 +657,7 @@ public void setDraggableFromAnywhere(boolean draggableFromAnywhere){
639657

640658
//CHAPTER IV - MISC METHODS
641659

642-
//Fetch accent colour.
660+
//Fetch accent color.
643661
static int fetchAccentColour(Context context) {
644662
TypedValue typedValue = new TypedValue();
645663

0 commit comments

Comments
 (0)