Skip to content

Commit

Permalink
* RecyclerView scroll up, dawn, left, right aware
Browse files Browse the repository at this point in the history
  • Loading branch information
iballan committed May 3, 2017
1 parent 8de15cb commit f82d5ee
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 3 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,19 @@ You can install using Gradle:
maven { url "https://jitpack.io" }
}
dependencies {
compile 'com.github.iballan:hfradapter:1.0.0'
compile 'com.github.iballan:hfradapter:1.0.2'
}
```

### TODO:

- [x] Add Scroll aware RecyclerView
- [ ] Add Screenshots
- [ ] Add Example of Scroll aware RecyclerView
- [ ] Test AutoLoadMore listener
- [ ] Test Scroll aware RecyclerView (MBRecyclerView)


Contact me:
--------

Expand Down
4 changes: 2 additions & 2 deletions hfradapter/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ android {
defaultConfig {
minSdkVersion 9
targetSdkVersion 25
versionCode 2
versionName "1.0.1"
versionCode 3
versionName "1.0.2"
}

compileOptions {
Expand Down
150 changes: 150 additions & 0 deletions hfradapter/src/main/java/com/mbh/hfradapter/MBRecyclerView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package com.mbh.hfradapter;


import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;

/**
* Createdby MBH on 02/05/2017.
*/

public class MBRecyclerView extends RecyclerView {

private OnScrollListener wrappedUpDownScrollListener;
private OnScrollListener wrappedLeftRightScrollListener;

public MBRecyclerView(Context context) {
super(context);
init();
}

public MBRecyclerView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}

public MBRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}

private void init() {}

// region Scrolling Listener for Up, Down, Left and Right
public void setOnUpDownScrollListener(final OnUpDownScrollListener onUpDownScrollListener) {
if (wrappedUpDownScrollListener == null) {
wrappedUpDownScrollListener = getWrappedUpDownScrollListener(onUpDownScrollListener);
addOnScrollListener(wrappedUpDownScrollListener);
}
}

public void removeOnUpDownScrollListener() {
if (wrappedUpDownScrollListener != null) {
removeOnScrollListener(wrappedUpDownScrollListener);
wrappedUpDownScrollListener = null;
}
}

public void setLeftOnRightScrollListener(final OnLeftRightScrollListener onLeftRightScrollListener) {
if (wrappedLeftRightScrollListener == null) {
wrappedLeftRightScrollListener = getWrappedLeftRightScrollListener(onLeftRightScrollListener);
addOnScrollListener(wrappedLeftRightScrollListener);
}
}

public void removeOnLeftRightScrollListener() {
if (wrappedLeftRightScrollListener != null) {
removeOnScrollListener(wrappedLeftRightScrollListener);
wrappedLeftRightScrollListener = null;
}
}

private OnScrollListener getWrappedUpDownScrollListener(final OnUpDownScrollListener onUpDownScrollListener) {
return new OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if (onUpDownScrollListener != null) {
// Negative to check scrolling up, positive to check scrolling down
if (!ViewCompat.canScrollVertically(recyclerView, -1)) {
onUpDownScrollListener.onScrolledToTop();
} else if (!ViewCompat.canScrollVertically(recyclerView, 1)) {
onUpDownScrollListener.onScrolledToBottom();
}
if (dy > 0) {
onUpDownScrollListener.onScrollDown(dy);
} else if (dy < 0) {
onUpDownScrollListener.onScrollUp(dy);
}
}
}

@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
if (onUpDownScrollListener != null) {
onUpDownScrollListener.onScrollStopped();
}
}
}
};
}

private OnScrollListener getWrappedLeftRightScrollListener(final OnLeftRightScrollListener onLeftRightScrollListener) {
return new OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (onLeftRightScrollListener != null) {
// Negative to check scrolling left, positive to check scrolling right
if (!ViewCompat.canScrollHorizontally(recyclerView, -1)) {
onLeftRightScrollListener.onScrolledToMostLeft();
} else if (!ViewCompat.canScrollHorizontally(recyclerView, 1)) {
onLeftRightScrollListener.onScrolledToMostRight();
}
if (dy > 0) {
onLeftRightScrollListener.onScrollRight(dx);
} else if (dy < 0) {
onLeftRightScrollListener.onScrollLeft(dx);
}
}
}

@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
if (onLeftRightScrollListener != null) {
onLeftRightScrollListener.onScrollStopped();
}
}
}
};
}

public abstract class OnUpDownScrollListener {
public void onScrollUp(int dy) {}

public void onScrollDown(int dy) {}

public void onScrolledToTop() {}

public void onScrolledToBottom() {}

public void onScrollStopped() {}
}

public abstract class OnLeftRightScrollListener {
public void onScrollLeft(int dx) {}

public void onScrollRight(int dx) {}

public void onScrolledToMostRight() {}

public void onScrolledToMostLeft() {}

public void onScrollStopped() {}
}
// endregion
}

0 comments on commit f82d5ee

Please sign in to comment.