Skip to content

Commit

Permalink
Add second option for smaller bubble size
Browse files Browse the repository at this point in the history
Add method to set LayoutManager for FastScrollView
  • Loading branch information
Randy Webster committed Sep 13, 2018
1 parent ef2d3ec commit bb57d44
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 47 deletions.
32 changes: 20 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# FastScroll
[![License](http://img.shields.io/badge/License-Apache%202.0-blue.svg?style=flat-square)](http://www.apache.org/licenses/LICENSE-2.0) [![API](https://img.shields.io/badge/API-14%2B-blue.svg?style=flat-square)](https://developer.android.com/about/versions/android-4.0.html) [![Download](https://img.shields.io/badge/JCenter-2.0.0‒beta1-brightgreen.svg?style=flat-square)](https://bintray.com/l4digital/maven/FastScroll/_latestVersion)
[![License](http://img.shields.io/badge/License-Apache%202.0-blue.svg?style=flat-square)](http://www.apache.org/licenses/LICENSE-2.0) [![API](https://img.shields.io/badge/API-14%2B-blue.svg?style=flat-square)](https://developer.android.com/about/versions/android-4.0.html) [![Download](https://img.shields.io/badge/JCenter-2.0.0‒beta2-brightgreen.svg?style=flat-square)](https://bintray.com/l4digital/maven/FastScroll/_latestVersion)

A ListView-like FastScroller for Android’s RecyclerView.

Expand All @@ -15,7 +15,7 @@ FastScroll was inspired by this [Styling Android blog post](https://blog.styling
#### Gradle:
~~~groovy
dependencies {
implementation 'com.l4digital.fastscroll:fastscroll:2.0.0-beta1'
implementation 'com.l4digital.fastscroll:fastscroll:2.0.0-beta2'
}
~~~

Expand All @@ -24,7 +24,7 @@ dependencies {
<dependency>
<groupId>com.l4digital.fastscroll</groupId>
<artifactId>fastscroll</artifactId>
<version>2.0.0-beta1</version>
<version>2.0.0-beta2</version>
</dependency>
~~~

Expand Down Expand Up @@ -54,10 +54,7 @@ Add the `FastScrollRecyclerView` to your xml layout and set your customizations
android:layout_height="match_parent"
app:bubbleColor="#00bb00"
app:bubbleTextColor="#ffffff"
app:handleColor="#999999"
app:trackColor="#bbbbbb"
app:hideScrollbar="false"
app:showTrack="false" />
app:handleColor="#999999" />

</FrameLayout>
~~~
Expand Down Expand Up @@ -104,10 +101,7 @@ Add the `FastScrollView` to your xml layout and set your customizations using at
android:layout_height="match_parent"
app:bubbleColor="#00bb00"
app:bubbleTextColor="#ffffff"
app:handleColor="#999999"
app:trackColor="#bbbbbb"
app:hideScrollbar="false"
app:showTrack="false" />
app:handleColor="#999999" />

</android.support.v4.widget.SwipeRefreshLayout>
~~~
Expand All @@ -120,7 +114,7 @@ override fun onCreate(savedInstanceState: Bundle?) {
setContentView(R.layout.activity_example)

val fastScrollView: FastScrollView? = findViewById(R.id.fastscroll_view)
fastScrollView?.recyclerView.layoutManager = LinearLayoutManager(this)
fastScrollView?.setLayoutManager(LinearLayoutManager(this))
fastScrollView?.setAdapter(ExampleAdapter())
}
~~~
Expand All @@ -142,6 +136,20 @@ class ExampleAdapter : RecyclerView.Adapter<ExampleAdapter.ViewHolder>(), FastSc
If you are unable to use the `FastScrollRecyclerView` or `FastScrollView`, you can add a `FastScroller` to your layout and implement with any `RecyclerView`. See this [github issue](https://github.com/L4Digital/FastScroll/issues/4#issuecomment-256975634) for an example.


## Customization
The following attributes can be set to customize the visibility and appearance of the elements within the FastScroller view:

* `app:hideScrollbar=["true" | "false"]` *Hides the scrollbar when not scrolling*
* `app:showBubble=["true" | "false"]` *Shows the section bubble while scrolling*
* `app:showTrack=["true" | "false"]` *Shows the scroll track while scrolling*
* `app:handleColor=["#rrggbb" | "@color"]` *Sets the color of the scroll handle*
* `app:trackColor=["#rrggbb" | "@color"]` *Sets the color of the scroll track*
* `app:bubbleColor=["#rrggbb" | "@color"]` *Sets the background color of the section bubble*
* `app:bubbleSize=["normal" | "small"]` *Sets the size of the section bubble*
* `app:bubbleTextColor=["#rrggbb" | "@color"]` *Sets the text color of the section bubble*
* `app:bubbleTextSize=["sp" | "@dimen"]` *Sets the scaled pixel text size of the section bubble*


## License
Copyright 2018 Globant. All rights reserved.

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ ext {

buildscript {
ext.gradleVersion = '3.1.4'
ext.kotlinVersion = '1.2.61'
ext.kotlinVersion = '1.2.70'

repositories {
google()
Expand Down
4 changes: 2 additions & 2 deletions fastscroll/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ apply plugin: 'maven-publish'
def libraryName = 'FastScroll'
def libraryArtifact = 'fastscroll'
def libraryGroup = 'com.l4digital.fastscroll'
def libraryVersion = '2.0.0-beta1'
def libraryVersion = '2.0.0-beta2'

android {
compileSdkVersion rootProject.ext.sdkVersion
Expand All @@ -38,7 +38,7 @@ android {
}

dependencies {
api 'com.android.support.constraint:constraint-layout:1.1.2'
api 'com.android.support.constraint:constraint-layout:1.1.3'
api "com.android.support:design:$supportLibVersion"
api "com.android.support:recyclerview-v7:$supportLibVersion"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public void setHideScrollbar(boolean hideScrollbar) {
}

/**
* Display a scroll track while scrolling.
* Show the scroll track while scrolling.
*
* @param visible True to show scroll track, false to hide
*/
Expand All @@ -132,7 +132,7 @@ public void setTrackColor(@ColorInt int color) {
}

/**
* Set the color for the scroll handle.
* Set the color of the scroll handle.
*
* @param color The color for the scroll handle
*/
Expand All @@ -150,27 +150,27 @@ public void setBubbleVisible(boolean visible) {
}

/**
* Set the background color of the index bubble.
* Set the background color of the section bubble.
*
* @param color The background color for the index bubble
* @param color The background color for the section bubble
*/
public void setBubbleColor(@ColorInt int color) {
fastScroller.setBubbleColor(color);
}

/**
* Set the text color of the index bubble.
* Set the text color of the section bubble.
*
* @param color The text color for the index bubble
* @param color The text color for the section bubble
*/
public void setBubbleTextColor(@ColorInt int color) {
fastScroller.setBubbleTextColor(color);
}

/**
* Set the scaled pixel text size of the index bubble.
* Set the scaled pixel text size of the section bubble.
*
* @param size The scaled pixel text size for the index bubble
* @param size The scaled pixel text size for the section bubble
*/
public void setBubbleTextSize(int size) {
fastScroller.setBubbleTextSize(size);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ public void setAdapter(@Nullable RecyclerView.Adapter adapter) {
}
}

/**
* Set a new {@link RecyclerView.LayoutManager} for the RecyclerView.
*
* @param layoutManager The new {@link RecyclerView.LayoutManager} to set
*/
public void setLayoutManager(@NonNull RecyclerView.LayoutManager layoutManager) {
recyclerView.setLayoutManager(layoutManager);
}

@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
Expand Down
89 changes: 65 additions & 24 deletions fastscroll/src/main/java/com/l4digital/fastscroll/FastScroller.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.ColorInt;
import android.support.annotation.DimenRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
Expand Down Expand Up @@ -64,13 +66,32 @@
* {@link R.styleable#FastScroller_hideScrollbar}
* {@link R.styleable#FastScroller_showBubble}
* {@link R.styleable#FastScroller_showTrack}
* {@link R.styleable#FastScroller_bubbleColor}
* {@link R.styleable#FastScroller_bubbleTextColor}
* {@link R.styleable#FastScroller_handleColor}
* {@link R.styleable#FastScroller_trackColor}
* {@link R.styleable#FastScroller_bubbleColor}
* {@link R.styleable#FastScroller_bubbleSize}
* {@link R.styleable#FastScroller_bubbleTextColor}
* {@link R.styleable#FastScroller_bubbleTextSize}
*/
public class FastScroller extends LinearLayout {

public enum Size {
NORMAL(R.drawable.fastscroll_bubble, R.dimen.fastscroll_bubble_text_size),
SMALL(R.drawable.fastscroll_bubble_small, R.dimen.fastscroll_bubble_text_size_small);

@DrawableRes public int drawableId;
@DimenRes public int textSizeId;

Size(@DrawableRes int drawableId, @DimenRes int textSizeId) {
this.drawableId = drawableId;
this.textSizeId = textSizeId;
}

public static Size fromOrdinal(int ordinal) {
return ordinal >= 0 && ordinal < values().length ? values()[ordinal] : NORMAL;
}
}

private static final int BUBBLE_ANIM_DURATION = 100;
private static final int SCROLLBAR_ANIM_DURATION = 300;
private static final int SCROLLBAR_HIDE_DELAY = 1000;
Expand All @@ -90,6 +111,7 @@ public class FastScroller extends LinearLayout {
private ImageView handleView;
private ImageView trackView;
private RecyclerView recyclerView;
private Size bubbleSize;
private SwipeRefreshLayout swipeRefreshLayout;
private TextView bubbleView;
private View scrollbar;
Expand Down Expand Up @@ -150,8 +172,12 @@ public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
};

public FastScroller(@NonNull Context context) {
this(context, Size.NORMAL);
}

public FastScroller(@NonNull Context context, Size size) {
super(context);
layout(context, null);
layout(context, size);
setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));
}

Expand Down Expand Up @@ -394,16 +420,7 @@ public void setHideScrollbar(boolean hideScrollbar) {
}

/**
* Show the section bubble while scrolling.
*
* @param visible True to show the bubble, false to hide
*/
public void setBubbleVisible(boolean visible) {
showBubble = visible;
}

/**
* Display a scroll track while scrolling.
* Show the scroll track while scrolling.
*
* @param visible True to show scroll track, false to hide
*/
Expand Down Expand Up @@ -433,7 +450,7 @@ public void setTrackColor(@ColorInt int color) {
}

/**
* Set the color for the scroll handle.
* Set the color of the scroll handle.
*
* @param color The color for the scroll handle
*/
Expand All @@ -454,15 +471,24 @@ public void setHandleColor(@ColorInt int color) {
}

/**
* Set the background color of the index bubble.
* Show the section bubble while scrolling.
*
* @param visible True to show the bubble, false to hide
*/
public void setBubbleVisible(boolean visible) {
showBubble = visible;
}

/**
* Set the background color of the section bubble.
*
* @param color The background color for the index bubble
* @param color The background color for the section bubble
*/
public void setBubbleColor(@ColorInt int color) {
bubbleColor = color;

if (bubbleImage == null) {
Drawable drawable = ContextCompat.getDrawable(getContext(), R.drawable.fastscroll_bubble);
Drawable drawable = ContextCompat.getDrawable(getContext(), bubbleSize.drawableId);

if (drawable != null) {
bubbleImage = DrawableCompat.wrap(drawable);
Expand All @@ -481,18 +507,18 @@ public void setBubbleColor(@ColorInt int color) {
}

/**
* Set the text color of the index bubble.
* Set the text color of the section bubble.
*
* @param color The text color for the index bubble
* @param color The text color for the section bubble
*/
public void setBubbleTextColor(@ColorInt int color) {
bubbleView.setTextColor(color);
}

/**
* Set the scaled pixel text size of the index bubble.
* Set the scaled pixel text size of the section bubble.
*
* @param size The scaled pixel text size for the index bubble
* @param size The scaled pixel text size for the section bubble
*/
public void setBubbleTextSize(int size) {
bubbleView.setTextSize(size);
Expand Down Expand Up @@ -679,8 +705,16 @@ private void setHandleSelected(boolean selected) {
DrawableCompat.setTint(handleImage, selected ? bubbleColor : handleColor);
}

@SuppressWarnings("ConstantConditions")
private void layout(Context context, AttributeSet attrs) {
layout(context, attrs, Size.NORMAL);
}

private void layout(Context context, Size size) {
layout(context, null, size);
}

@SuppressWarnings("ConstantConditions")
private void layout(Context context, AttributeSet attrs, Size size) {
inflate(context, R.layout.fast_scroller, this);

setClipChildren(false);
Expand All @@ -691,6 +725,8 @@ private void layout(Context context, AttributeSet attrs) {
trackView = findViewById(R.id.fastscroll_track);
scrollbar = findViewById(R.id.fastscroll_scrollbar);

bubbleSize = size;

@ColorInt int bubbleColor = Color.GRAY;
@ColorInt int handleColor = Color.DKGRAY;
@ColorInt int trackColor = Color.LTGRAY;
Expand All @@ -700,7 +736,7 @@ private void layout(Context context, AttributeSet attrs) {
boolean showBubble = true;
boolean showTrack = false;

float textSize = getResources().getDimension(R.dimen.fastscroll_bubble_text_size);
float textSize = getResources().getDimension(size.textSizeId);

if (attrs != null) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.FastScroller, 0, 0);
Expand All @@ -711,10 +747,15 @@ private void layout(Context context, AttributeSet attrs) {
handleColor = typedArray.getColor(R.styleable.FastScroller_handleColor, handleColor);
trackColor = typedArray.getColor(R.styleable.FastScroller_trackColor, trackColor);
textColor = typedArray.getColor(R.styleable.FastScroller_bubbleTextColor, textColor);
textSize = typedArray.getDimension(R.styleable.FastScroller_bubbleTextSize, textSize);
hideScrollbar = typedArray.getBoolean(R.styleable.FastScroller_hideScrollbar, hideScrollbar);
showBubble = typedArray.getBoolean(R.styleable.FastScroller_showBubble, showBubble);
showTrack = typedArray.getBoolean(R.styleable.FastScroller_showTrack, showTrack);

int sizeOrdinal = typedArray.getInt(R.styleable.FastScroller_bubbleSize, size.ordinal());
bubbleSize = Size.fromOrdinal(sizeOrdinal);

textSize = typedArray.getDimension(R.styleable.FastScroller_bubbleTextSize,
getResources().getDimension(bubbleSize.textSizeId));
} finally {
typedArray.recycle();
}
Expand Down
Loading

0 comments on commit bb57d44

Please sign in to comment.