Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically hide scroller when not scrolling (issue #4) #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
import android.os.Build.VERSION_CODES;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.OnScrollListener;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.widget.FrameLayout;
import android.widget.SectionIndexer;

Expand All @@ -30,6 +32,14 @@
public abstract class AbsRecyclerViewFastScroller extends FrameLayout implements RecyclerViewScroller {

private static final int[] STYLEABLE = R.styleable.AbsRecyclerViewFastScroller;

private static final int AUTO_HIDE_SCROLLER_TIMEOUT_MILLS = 1000;

private static final int CURRENT_ANIMATION_NONE = 0;
private static final int CURRENT_ANIMATION_SHOW = 1;
private static final int CURRENT_ANIMATION_HIDE = 2;


/** The long bar along which a handle travels */
protected final View mBar;
/** The handle that signifies the user's progress in the list */
Expand All @@ -46,6 +56,15 @@ public abstract class AbsRecyclerViewFastScroller extends FrameLayout implements
* {@link OnScrollListener} an abstract class instead of an interface. Hmmm */
protected OnScrollListener mOnScrollListener;

/** true: user is grabbing the handle, false: otherwise */
private boolean mIsGrabbingHandle;

/** true: always show the scroller, false: hide the scroller automatically */
private boolean mFastScrollAlwaysVisible = false;
/** Type of the view animation (CURRENT_ANIMATION_xxx) */
private int mCurrentAnimationType = CURRENT_ANIMATION_NONE;
private Runnable mAutoHideProcessRunnable;

public AbsRecyclerViewFastScroller(Context context) {
this(context, null, 0);
}
Expand Down Expand Up @@ -82,6 +101,29 @@ public AbsRecyclerViewFastScroller(Context context, AttributeSet attrs, int defS
setOnTouchListener(new FastScrollerTouchListener(this));
}

@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();

mAutoHideProcessRunnable = new Runnable() {
@Override
public void run() {
hideWithAnimation();
}
};
if (!mFastScrollAlwaysVisible) {
hide();
}
}

@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();

cancelAutoHideScrollerProcess();
mAutoHideProcessRunnable = null;
}

private void applyCustomAttributesToView(View view, Drawable drawable, int color) {
if (drawable != null) {
setViewBackground(view, drawable);
Expand Down Expand Up @@ -182,6 +224,14 @@ public OnScrollListener getOnScrollListener() {
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
float scrollProgress = getScrollProgressCalculator().calculateScrollProgress(recyclerView);
moveHandleToPosition(scrollProgress);

// show scroll bar
if (!mFastScrollAlwaysVisible) {
showWithAnimation();
if (!mIsGrabbingHandle) {
scheduleAutoHideScrollerProcess();
}
}
}
};
}
Expand All @@ -206,6 +256,162 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
*/
protected abstract void onCreateScrollProgressCalculator();

/**
* Returns true if the fast scroller is set to always show on this view.
*
* @return true if the fast scroller will always show
*/
public boolean isFastScrollAlwaysVisible() {
return mFastScrollAlwaysVisible;
}

/**
* Sets whether or not the fast scroller should always be shown.
*
* @param alwaysVisible true if the fast scroller should always be displayed, false otherwise
*/
public void setFastScrollAlwaysVisible(boolean alwaysVisible) {
if (mFastScrollAlwaysVisible == alwaysVisible) {
return;
}
mFastScrollAlwaysVisible = alwaysVisible;
if (mFastScrollAlwaysVisible) {
show();
} else {
cancelAutoHideScrollerProcess();
}
}

private void scheduleAutoHideScrollerProcess() {
cancelAutoHideScrollerProcess();

if (!mFastScrollAlwaysVisible) {
postDelayed(mAutoHideProcessRunnable, AUTO_HIDE_SCROLLER_TIMEOUT_MILLS);
}
}

private void cancelAutoHideScrollerProcess() {
removeCallbacks(mAutoHideProcessRunnable);
}

private void show() {
cancelAutoHideScrollerProcess();

if (getAnimation() != null) {
getAnimation().cancel();
}

ViewCompat.setTranslationX(this, 0);
ViewCompat.setTranslationY(this, 0);

setVisibility(View.VISIBLE);
}

private void hide() {
cancelAutoHideScrollerProcess();

if (getAnimation() != null) {
getAnimation().cancel();
}
setVisibility(View.INVISIBLE);
}

private void showWithAnimation() {
if ((mCurrentAnimationType == CURRENT_ANIMATION_SHOW) || (getVisibility() == View.VISIBLE)) {
return;
}

cancelAutoHideScrollerProcess();

if (getAnimation() != null) {
getAnimation().cancel();
}

final Animation anim = loadShowAnimation();

if (anim != null) {
anim.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
setVisibility(View.VISIBLE);
}

@Override
public void onAnimationEnd(Animation animation) {
mCurrentAnimationType = CURRENT_ANIMATION_NONE;
}

@Override
public void onAnimationRepeat(Animation animation) {
}
});

mCurrentAnimationType = CURRENT_ANIMATION_SHOW;

startAnimation(anim);
} else {
show();
}
}

private void hideWithAnimation() {
if ((mCurrentAnimationType == CURRENT_ANIMATION_HIDE) || (getVisibility() != View.VISIBLE)) {
return;
}

cancelAutoHideScrollerProcess();

if (getAnimation() != null) {
getAnimation().cancel();
}

final Animation anim = loadHideAnimation();

if (anim != null) {
anim.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}

@Override
public void onAnimationEnd(Animation animation) {
mCurrentAnimationType = CURRENT_ANIMATION_NONE;
setVisibility(View.INVISIBLE);
}

@Override
public void onAnimationRepeat(Animation animation) {
}
});

mCurrentAnimationType = CURRENT_ANIMATION_HIDE;

startAnimation(anim);
} else {
hide();
}
}

/**
* Sets whether user is grabbing the scroller handle
* @param isGrabbingHandle true: grabbing, false: not grabbing
*/
public void setIsGrabbingHandle(boolean isGrabbingHandle) {
if (mIsGrabbingHandle == isGrabbingHandle) {
return;
}

mIsGrabbingHandle = isGrabbingHandle;

if (!mFastScrollAlwaysVisible) {
if (isGrabbingHandle) {
show();
} else {
scheduleAutoHideScrollerProcess();
}
}
}

/**
* Takes a touch event and determines how much scroll progress this translates into
* @param event touch event received by the layout
Expand Down Expand Up @@ -234,4 +440,15 @@ public float getScrollProgress(MotionEvent event) {
*/
public abstract void moveHandleToPosition(float scrollProgress);

/**
* Loads scroller show animation
* @return animation which is used for the showWithAnimation() method
*/
protected abstract Animation loadShowAnimation();

/**
* Loads scroller hide animation
* @return animation which is used for the hideWithAnimation() method
*/
protected abstract Animation loadHideAnimation();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import xyz.danoz.recyclerviewfastscroller.sectionindicator.SectionIndicator;

import android.support.annotation.Nullable;
import android.support.v4.view.MotionEventCompat;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
Expand All @@ -23,6 +24,17 @@ public FastScrollerTouchListener(AbsRecyclerViewFastScroller fastScroller) {

@Override
public boolean onTouch(View v, MotionEvent event) {
int action = MotionEventCompat.getActionMasked(event);

switch (action) {
case MotionEvent.ACTION_DOWN:
mFastScroller.setIsGrabbingHandle(true);
break;
case MotionEvent.ACTION_UP:
mFastScroller.setIsGrabbingHandle(false);
break;
}

SectionIndicator sectionIndicator = mFastScroller.getSectionIndicator();
showOrHideIndicator(sectionIndicator, event);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import xyz.danoz.recyclerviewfastscroller.R;
import xyz.danoz.recyclerviewfastscroller.AbsRecyclerViewFastScroller;
Expand Down Expand Up @@ -50,6 +52,17 @@ public void moveHandleToPosition(float scrollProgress) {
mHandle.setY(mScreenPositionCalculator.getYPositionFromScrollProgress(scrollProgress));
}

@Override
protected Animation loadShowAnimation() {
return AnimationUtils.loadAnimation(getContext(), R.anim.fast_scroller_slide_in_right);
}

@Override
protected Animation loadHideAnimation() {
return AnimationUtils.loadAnimation(getContext(), R.anim.fast_scroller_slide_out_right);
}

@Override
protected void onCreateScrollProgressCalculator() {
VerticalScrollBoundsProvider boundsProvider =
new VerticalScrollBoundsProvider(mBar.getY(), mBar.getY() + mBar.getHeight() - mHandle.getHeight());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromXDelta="100%" android:toXDelta="0"
android:duration="@android:integer/config_shortAnimTime"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromXDelta="0" android:toXDelta="100%"
android:duration="@android:integer/config_shortAnimTime"/>