Skip to content

Commit 736d345

Browse files
committed
Version 6.1.0
- Bar hides if the recyclerView fits on the screen without scrolling. - Hiding is done more efficiently
1 parent 022ae7a commit 736d345

File tree

2 files changed

+34
-54
lines changed

2 files changed

+34
-54
lines changed

lib/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ ext {
1313
siteUrl = 'https://github.com/krimin-killr21/MaterialScrollBar'
1414
gitUrl = 'https://github.com/krimin-killr21/MaterialScrollBar.git'
1515

16-
libraryVersion = '6.0'
16+
libraryVersion = '6.1.0'
1717

1818
developerId = 'krimin-killr21'
1919
developerName = 'Turing Technologies'
@@ -31,8 +31,8 @@ android {
3131
defaultConfig {
3232
minSdkVersion 7
3333
targetSdkVersion 23
34-
versionCode 11
35-
versionName "6.0"
34+
versionCode 12
35+
versionName "6.1.0"
3636
}
3737
}
3838

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

Lines changed: 31 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import android.graphics.Color;
2525
import android.graphics.drawable.GradientDrawable;
2626
import android.os.Build;
27+
import android.os.Handler;
28+
import android.os.Looper;
2729
import android.support.v4.content.ContextCompat;
2830
import android.support.v7.widget.LinearLayoutManager;
2931
import android.support.v7.widget.RecyclerView;
@@ -54,46 +56,15 @@ public class MaterialScrollBar extends RelativeLayout {
5456
private boolean lightOnTouch;
5557
private boolean totallyHidden = false;
5658
MaterialScrollBar.ScrollListener scrollListener;
59+
private Handler mUIHandler = new Handler(Looper.getMainLooper());
5760

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() {
7362

7463
@Override
7564
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();
9366
}
94-
}
95-
96-
private BarFade fade;
67+
};
9768

9869
/**
9970
* For testing only. Should not generally be accessed.
@@ -125,7 +96,7 @@ public MaterialScrollBar(Context context, RecyclerView recyclerView, boolean lig
12596
LayoutParams lp = new RelativeLayout.LayoutParams(Utils.getDP(12, this), LayoutParams.MATCH_PARENT);
12697
lp.addRule(ALIGN_PARENT_RIGHT);
12798
background.setLayoutParams(lp);
128-
background.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));
99+
background.setBackgroundColor(ContextCompat.getColor(context, android.R.color.darker_gray));
129100
ViewHelper.setAlpha(background, 0.4F);
130101

131102
handle = new View(context);
@@ -163,9 +134,6 @@ public MaterialScrollBar(Context context, RecyclerView recyclerView, boolean lig
163134

164135
setTouchIntercept();
165136

166-
fade = new BarFade(this);
167-
fade.start();
168-
169137
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);
170138
anim.setFillAfter(true);
171139
startAnimation(anim);
@@ -188,8 +156,7 @@ public boolean onTouch(View v, MotionEvent event) {
188156
handle.setBackgroundColor(handleColour);
189157
}
190158

191-
fade.held = true;
192-
159+
mUIHandler.removeCallbacks(mFadeBar);
193160
fadeIn();
194161
} else {
195162
if(indicator != null && indicator.getVisibility() == VISIBLE){
@@ -204,10 +171,9 @@ public boolean onTouch(View v, MotionEvent event) {
204171
}
205172

206173
if (hide) {
207-
fade.run = true;
208-
fade.time = System.currentTimeMillis() + hideDuration;
174+
mUIHandler.removeCallbacks(mFadeBar);
175+
mUIHandler.postDelayed(mFadeBar, hideDuration);
209176
}
210-
fade.held = false;
211177
}
212178
return true;
213179
}
@@ -371,12 +337,10 @@ public MaterialScrollBar setTextColour(String colour){
371337
*/
372338
public MaterialScrollBar setAutoHide(Boolean hide){
373339
if(!hide){
374-
fade.interrupt();
340+
mUIHandler.removeCallbacks(mFadeBar);
375341
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);
376342
anim.setFillAfter(true);
377343
startAnimation(anim);
378-
} else if (!this.hide){
379-
fade.start();
380344
}
381345
this.hide = hide;
382346
return this;
@@ -518,11 +482,27 @@ public void onScrollStateChanged(final RecyclerView recyclerView, int newState)
518482

519483
if(hide){
520484
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);
523487
} 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+
}
526506
}
527507
}
528508
}

0 commit comments

Comments
 (0)