0

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;

                    }

                }
            });

}

}

fadhli-sulaimi
  • 686
  • 12
  • 17
  • you can use https://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html – Adeel Turk May 03 '17 at 12:12
  • Hi, thanks I am using this. Actually my concern is I am not sure on how I can add the previous page comments to the top of the current list. And also how do i notify the adapter once its loaded. – fadhli-sulaimi May 03 '17 at 12:23
  • create an array list pass it to the adapter then fecth the list of comments and add all the comments list to the arraylits you have passed to adapter and call adapter.notifyDatasetChanged() to show them in reverse order you can reverse the arraylist or set LInearLayoutManger reverse layout to ture , given link will help you http://stackoverflow.com/questions/27727354/linearlayoutmanager-setreverselayout-true-but-items-stack-from-bottom – Adeel Turk May 03 '17 at 12:40

0 Answers0