0

I am looking for a good way to implement a paging mechanism into my RecyclerView lists. I'm using an AsyncTaskLoader to populate and cache the data into the RecyclerView.

public class HeadlinesListLoader extends AsyncTaskLoader<List<Headline>> {

    public HeadlinesListLoader(Context context) {
        super(context);
    }

    @Override
    protected void onStartLoading() {
        forceLoad();
    }

    @Override
    public List<Headline> loadInBackground() {
        return DataManager.getInstance().findAllHealines();
    }

    @Override
    public void deliverResult(List<Headline> data) {
        super.deliverResult(data);
    }
} 

Where DataManager.getInstance().findAllHealines(); makes the RESTful call to retrieve the list data, however, it's currently loading all the data at once which might take significantly long.

I've been looking into an efficient way to gradually load the data into the RecyclerView as needed. I came across this Paging Library recommended by Android:

https://developer.android.com/topic/libraries/architecture/paging.html

However, the way it's querying the database in the front-end doesn't sound like the right solution in my case and since I'm using AsyncTaskLoader, I'm wondering if there is a way to solve this problem using the Loaders framework itself? Or what would be a better option?

I'm open to any suggestions. Thanks.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
  • Do you have pagination support from your back-end server? – Reaz Murshed Feb 11 '18 at 03:23
  • @ReazMurshed No, I don't. I thought there might be a way to go about it from the front-end using Loaders. However, I think I should do it that way where I'll have a nextLoad() function at the back-end that gets triggered whenever a new load is needed at the front-end? – Mohamed Abulgasem Feb 12 '18 at 07:52
  • Exactly. For example, the back-end server will provide you first 10 rows first. Then you will have a load-more button in your end and clicking on that button will trigger the same API call with page number 2. When the back-end will receive a request with page number 2, it will serve you the next 10 rows which you will add in your `RecyclerView` and so on. – Reaz Murshed Feb 12 '18 at 09:13
  • @ReazMurshed Thanks, this is how I'm going about it now. – Mohamed Abulgasem Feb 16 '18 at 15:17
  • Glad to know that I could help you. You are most welcome! – Reaz Murshed Feb 16 '18 at 16:55

1 Answers1

0

You can do this on scroll event.

YourListElement.addOnScrollListener(new RecyclerView.OnScrollListener() {
        boolean isSlidingToLast = false;
        int lastVisibleItemPosition = 0;
        int lastPositions = 0;

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            if (newState == RecyclerView.SCROLL_STATE_IDLE && linearLayoutManager.getItemCount() > 0) {
                lastPositions = linearLayoutManager.getItemCount();
                lastVisibleItemPosition = linearLayoutManager.findLastVisibleItemPosition();

                if (lastPositions - 1 == lastVisibleItemPosition && isSlidingToLast) {
                    loadData(false);
                }
            }
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            isSlidingToLast = dy > 0;
        }
    });

load data is the function that get the new data to populate

Canato
  • 3,598
  • 5
  • 33
  • 57
  • Thank you, this solves my problem from the android side. Then will have to implement loadData() so that it makes a RESTful call to my back-end services that will trigger the next load. – Mohamed Abulgasem Feb 16 '18 at 15:23