Last active
April 7, 2016 03:07
-
-
Save ryandt/a91222fc4f60369373c3 to your computer and use it in GitHub Desktop.
A RecyclerView.OnScrollListener that adds support for scroll-enabled pagination. Attach it to your RecyclerView via your RecyclerView's addOnScrollListener(RecyclerView.OnScrollListener) method.
This file contains hidden or 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
import android.support.v7.widget.LinearLayoutManager; | |
import android.support.v7.widget.RecyclerView; | |
/** | |
* A RecyclerView.OnScrollListener that adds support for scroll-enabled pagination. | |
*/ | |
public abstract class ContinuousScrollListener extends RecyclerView.OnScrollListener { | |
private static String TAG = ContinuousScrollListener.class.getSimpleName(); | |
// Layout manager used on the RecyclerView. | |
private LinearLayoutManager mLinearLayoutManager; | |
// Number of items to load at a time. | |
private int mItemLoadLimit = 15; | |
// Minimum number of items to have below the currently visible items before loading more. | |
private int mThreshold = mItemLoadLimit / 2; | |
// Total number of items in the dataset after the last load. | |
private int mPreviousTotalItemCount; | |
// True if application is waiting for the most recently requested set of data to load. | |
private boolean mLoading = true; | |
public ContinuousScrollListener(LinearLayoutManager linearLayoutManager) { | |
mLinearLayoutManager = linearLayoutManager; | |
} | |
public ContinuousScrollListener(LinearLayoutManager linearLayoutManager, int itemLoadLimit) { | |
this(linearLayoutManager); | |
mItemLoadLimit = itemLoadLimit; | |
} | |
public ContinuousScrollListener(LinearLayoutManager linearLayoutManager, int itemLoadLimit, int threshold) { | |
this(linearLayoutManager, itemLoadLimit); | |
mThreshold = threshold; | |
} | |
/** | |
* Defines the process of loading more data. | |
* Note that the caller is responsible for determining the offset. | |
* @param itemLimit: The number of items to load. | |
*/ | |
public abstract void onLoadMore(int itemLimit); | |
@Override | |
public void onScrolled(RecyclerView recyclerView, int deltaX, int deltaY) { | |
super.onScrolled(recyclerView, deltaX, deltaY); | |
int totalItemCount = mLinearLayoutManager.getItemCount(); | |
int lastVisibleItemPosition = mLinearLayoutManager.findLastVisibleItemPosition(); | |
if (mLoading && totalItemCount > mPreviousTotalItemCount) { | |
// Since the dataset count has increased, assume that the loading has finished. | |
mLoading = false; | |
mPreviousTotalItemCount = totalItemCount; | |
} | |
// If a load is not in progress, check to see if the scroll threshold has been breached. | |
else if (!mLoading && totalItemCount <= (lastVisibleItemPosition + mThreshold)) { | |
// The threshold has been breached; attempt to fetch more data. | |
onLoadMore(mItemLoadLimit); | |
mLoading = true; | |
} | |
else if (mPreviousTotalItemCount > totalItemCount) { | |
// The adapter has been refreshed or invalided; reset mPreviousTotalItemCount. | |
mPreviousTotalItemCount = 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment