-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* RecyclerView scroll up, dawn, left, right aware
- Loading branch information
Showing
3 changed files
with
162 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
hfradapter/src/main/java/com/mbh/hfradapter/MBRecyclerView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |