Step 1- Defined here some necessary variable
boolean isLoading=false; -> this variable will use to show progressbar below to recyclerview in your xml file when you scroll your recyclerview list .
int mPageSize=10; -> Number of items you want to fetch from server in a once.
Step-2 Set addOnScrollListener to recyclerview
int mTotalItemsCount -> Total item on server side.
recyclerView.addOnScrollListener(new RecyclerView.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);
// if dy>0 scrolling down side
// if dy<0 scrolling upper side
if (dy < 0) {
if ((!isLoading && linearLayoutManager.findFirstCompletelyVisibleItemPosition() == 1) && itemsList.size() < mTotalItemsCount) {
isLoading = true;
// this method return pageIndex every time when you hit server to get new data.
mPageIndex = returnPageIndex(itemsList.size());
loadMoreItems(mPageIndex);
}
}
}
});
Step-3 Call loadMoreItems method
private void loadMoreItems(int mPageIndex) {
if (AppUtility.isInternetAvailable(this) == false) {
AppUtility.showAlertDialog(this, getString(R.string.app_name), getString(R.string.connection_error));
return;
}
// Here will go your server side request code
// after get response from server gone visibility to progressbar
mLoadMoreProgressbar.setVisibility(View.GONE);
// add response items to you items list
itemsList.add(chatMessageBean);
// notify adapter
adapter.notifyDataSetChanged();
// set isLoading to false
isLoading = false;
}
Step-4 Call returnPageIndex method
private int returnPageIndex(int sizeOfList) {
int index = sizeOfList / mPageSize;
return index;
}
1 Comment(s)