Skip to content

Instantly share code, notes, and snippets.

@lidemin
Created February 6, 2015 08:18
Show Gist options
  • Save lidemin/a16b6aa303c28b4dfdb3 to your computer and use it in GitHub Desktop.
Save lidemin/a16b6aa303c28b4dfdb3 to your computer and use it in GitHub Desktop.
RecyclerView which can listen scroll to the bottom or end.
public class DLRecyclerView extends RecyclerView {
public interface OnScrollListener {
/**
* only works when layoutmanager is LinearLayoutManager
*/
public abstract void onScrollToEnd();
}
private OnScrollListener _onScrollListener;
public void setOnScrollListener(OnScrollListener onScrollListener) {
_onScrollListener = onScrollListener;
}
public DLRecyclerView(Context context) {
super(context);
init();
}
public DLRecyclerView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public DLRecyclerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private boolean _loading = false;
int pastVisibleItems, visibleItemCount, totalItemCount;
public boolean isLoading() {
return _loading;
}
public void setLoading(boolean loading) {
this._loading = loading;
}
private void init() {
if (!isInEditMode()) {
setOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if (getLayoutManager() instanceof LinearLayoutManager) {
visibleItemCount = getLayoutManager().getChildCount();
totalItemCount = getLayoutManager().getItemCount();
pastVisibleItems = ((LinearLayoutManager) getLayoutManager()).findFirstVisibleItemPosition();
if (!_loading) {
if ((visibleItemCount + pastVisibleItems) >= totalItemCount) {
_loading = true;
if (_onScrollListener != null) {
_onScrollListener.onScrollToEnd();
}
}
}
}
}
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment