I would like to know how i can load the previous page items using swipe to refresh method on a recyclerview. Cant seem to find the solution anywhere. I have created an app which contains an endless Recyclerview, containing data from an online forum. When the user scrolls all the way down to the bottom, it will load the next page and populate the next comments on the recyclerview. It is working fine.
Right now i want to achieve the same thing but load the previous page when the user scrolls to the top. I want the user to do a swipe, which will load the previous page and populate the recyclerview with the previous comments from the top. How do i do that?
MainActivity.java
private ArrayList<CommentItem> commentsList = new ArrayList<CommentItem>();
private RecyclerAdapter adapter_thread;
private int pageNumber = 1;
//recyclerview initialization have been done here
adapter_replies.setOnLoadMoreListener(new OnLoadMoreListener() {
@Override
public void onLoadMore() {
commentsList.add(null); //to show progressbar at the bottom while loading the next page
adapter_thread.notifyItemInserted(commentsList.size() - 1);
++pageNumber;
getComments(); //async task load next page comments
}
});
RecyclerAdapter.java
private boolean loading;
private OnLoadMoreListener onLoadMoreListener;
private int visibleThreshold = 1;
public RecyclerAdapter(Context context, final ArrayList<CommentItem> commentsList, RecyclerView recyclerView) {
this.commentsList= commentsList;
this.context = context;
if (recyclerView.getLayoutManager() instanceof LinearLayoutManager) {
final LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView
.getLayoutManager();
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView,
int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
totalItemCount = linearLayoutManager.getItemCount();
lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
firstVisibleItem = linearLayoutManager.findFirstVisibleItemPosition();
verylastVisibleItem = linearLayoutManager.findLastCompletelyVisibleItemPosition();
if (!loading && totalItemCount <= (lastVisibleItem + visibleThreshold)) {
if (onLoadMoreListener != null) {
onLoadMoreListener.onLoadMore();
}
loading = true;
}
}
});
}
}