Created
October 20, 2016 14:51
-
-
Save Popalay/9a2425c97a09928e2021a13731e7e5b0 to your computer and use it in GitHub Desktop.
Pagination
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
public interface PaginationCallback { | |
boolean isRefreshing(); | |
void setRefreshing(boolean refreshing); | |
void getPage(int page); | |
UltimateAdapter getAdapter(); | |
} |
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
public class PaginationScrollListener extends RecyclerView.OnScrollListener { | |
private static final String TAG = "PaginationScrollListener"; | |
private static final int ITEMS_OFFSET_TO_LOAD_DEFAULT = 3; | |
private final OnRecyclerViewScrolledToPageListener mCallback; | |
private final int mOffset; | |
private LinearLayoutManager mLayoutManager; | |
private boolean loading; | |
private int previousTotal; | |
public interface OnRecyclerViewScrolledToPageListener { | |
void onRecyclerViewScrolledToEnd(); | |
} | |
public PaginationScrollListener(OnRecyclerViewScrolledToPageListener callback) { | |
this.mCallback = callback; | |
this.mOffset = ITEMS_OFFSET_TO_LOAD_DEFAULT; | |
if (mCallback == null) { | |
throw new IllegalStateException("OnRecyclerViewScrolledToPageListener is NULL"); | |
} | |
} | |
@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); | |
if (mLayoutManager == null) { | |
mLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager(); | |
} | |
final int visibleItemCount = recyclerView.getChildCount(); | |
final int totalItemCount = mLayoutManager.getItemCount(); | |
final int firstVisibleItem = mLayoutManager.findFirstCompletelyVisibleItemPosition(); | |
final int lastVisibleItem = firstVisibleItem + visibleItemCount; | |
if (loading) { | |
if (totalItemCount != previousTotal) { | |
loading = false; | |
previousTotal = totalItemCount; | |
} | |
} | |
if (!loading && (lastVisibleItem + mOffset) > totalItemCount) { | |
// End has been reached | |
mCallback.onRecyclerViewScrolledToEnd(); | |
// Do something | |
loading = true; | |
} | |
} | |
public void reset() { | |
loading = false; | |
previousTotal = -1; | |
} | |
} |
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
public class PaginationSupport implements PaginationScrollListener.OnRecyclerViewScrolledToPageListener, PullRefreshLayout.OnPullRefreshListener { | |
private static final String TAG = "PaginationSupport"; | |
private final int mFirstPageNumber; | |
private final PaginationScrollListener mPaginationScrollListener; | |
private final PaginationCallback mPaginationCallback; | |
private int mPage; | |
private boolean mIsLoading = false; | |
public PaginationSupport(PaginationCallback paginationCallback, RecyclerView recycler, int firstPageNumber) { | |
mPaginationCallback = paginationCallback; | |
mFirstPageNumber = firstPageNumber; | |
mPage = firstPageNumber; | |
mPaginationScrollListener = new PaginationScrollListener(this); | |
recycler.addOnScrollListener(mPaginationScrollListener); | |
} | |
@Override | |
public void onRecyclerViewScrolledToEnd() { | |
if (getAdapter() != null && getAdapter().getDataSize() > 0) { | |
getAdapter().setFooterVisibility(true); | |
loadPage(); | |
} | |
} | |
@Override | |
public void onRefresh() { | |
reset(); | |
} | |
public void onLoadComplete(boolean loaded) { | |
mIsLoading = false; | |
if (isRefreshing()) { | |
setRefreshing(false); | |
} | |
getAdapter().setFooterVisibility(false); | |
if (loaded) { | |
mPage++; | |
} | |
} | |
public void loadPage() { | |
if (mIsLoading) { | |
return; | |
} | |
mIsLoading = true; | |
if (mPage == mFirstPageNumber) { | |
setRefreshing(true); | |
} | |
getPage(mPage); | |
} | |
public void reset() { | |
mPaginationScrollListener.reset(); | |
mPage = mFirstPageNumber; | |
getAdapter().clear(); | |
getAdapter().notifyDataSetChanged(); | |
loadPage(); | |
} | |
private boolean isRefreshing() { | |
return mPaginationCallback.isRefreshing(); | |
} | |
private void setRefreshing(boolean refreshing) { | |
mPaginationCallback.setRefreshing(refreshing); | |
} | |
private void getPage(int page) { | |
mPaginationCallback.getPage(page); | |
} | |
private UltimateAdapter getAdapter() { | |
return mPaginationCallback.getAdapter(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment