Skip to content

Instantly share code, notes, and snippets.

@voghDev
Created January 26, 2016 11:03
Show Gist options
  • Save voghDev/21184b9f82eff3bc409c to your computer and use it in GitHub Desktop.
Save voghDev/21184b9f82eff3bc409c to your computer and use it in GitHub Desktop.
Simple RecyclerView customization that implements "OnLastItemVisible" feature, where the number of "items left to load more" can be set
import android.content.Context;
import android.content.res.TypedArray;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
public class ExtRecyclerView extends RecyclerView {
private static final int DEFAULT_ITEMS_LEFT_TO_LOAD_MORE = 5;
Context context;
int itemsLeftToLoadMore = DEFAULT_ITEMS_LEFT_TO_LOAD_MORE;
OnLastItemVisibleListener onLastItemVisibleListener = new EmptyLastItemListener();
private boolean loading = false;
final Object lock = new Object();
public ExtRecyclerView(Context context) {
super(context);
this.context = context;
init(null);
}
public ExtRecyclerView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
init(attrs);
}
public ExtRecyclerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;
init(attrs);
}
protected void init(AttributeSet attrs) {
if(attrs != null){
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ExtRecyclerView);
itemsLeftToLoadMore = a.getInt(R.styleable.ExtRecyclerView_itemsLeftToLoadMore, DEFAULT_ITEMS_LEFT_TO_LOAD_MORE );
}
addOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
LinearLayoutManager layoutManager = (LinearLayoutManager) getLayoutManager();
int last = layoutManager.findLastVisibleItemPosition();
int total = layoutManager.getItemCount();
if( lastItemsVisible(last, total) && !isLoading() ){
loading = true;
onLastItemVisibleListener.onLastItemVisible(last, total);
}
}
});
}
private boolean lastItemsVisible(int last, int total) {
return (last >= total - itemsLeftToLoadMore);
}
public boolean isLoading() {
synchronized (lock) {
return loading;
}
}
public void finishLoadingMore(){
synchronized (lock) {
loading = false;
}
}
//region Listeners
public void setOnLastItemVisibleListener(OnLastItemVisibleListener onLastItemVisibleListener) {
this.onLastItemVisibleListener = onLastItemVisibleListener;
}
public interface OnLastItemVisibleListener{
public void onLastItemVisible(int visibleIndex, int total); // See parameters
}
protected class EmptyLastItemListener implements OnLastItemVisibleListener{
public void onLastItemVisible(int visibleIndex, int total) {}
}
//endregion
}
@voghDev
Copy link
Author

voghDev commented Jan 26, 2016

Also, in res/values/attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="ExtRecyclerView">
        <attr name="itemsLeftToLoadMore" format="integer" />
    </declare-styleable>
</resources>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment