0

I'm looking for best practices. I want to implement load more [progressbar] after every 10 items (Using RecyclerView). In past, I did this by simply creating Loader class item and just added into the specific position in my List. Well, this time, I'm using PairList and it's impossible to add Loader class item (even tho I personally think, that's a pretty clean way to do it).

I found a few solutions on SO, which are almost the same: How to implement load more recyclerview in android

Is there any other way I could implement this load more progressbar after every 10th element (First load 10 items from API, then, when user reaches 10th item, we show progress bar [meanwhile we fetch the next 10 items], remove progress bar, add 10 items and so on).

1 Answers1

2

Use this InfiniteScrollListener in your recycler scrollListener:

public abstract class InfiniteScrollListener extends RecyclerView.OnScrollListener {

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);
    if (dy > 0) {
        LinearLayoutManager linearLayoutManager = (LinearLayoutManager)recyclerView.getLayoutManager();
        int visibleItemCount = linearLayoutManager.getChildCount();
        int totalItemCount = linearLayoutManager.getItemCount();
        int pastVisiblesItems = linearLayoutManager.findFirstVisibleItemPosition();

        if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) {
            loadMore();
        }
    }
}

protected abstract void loadMore();}

recyclerView Listener:

recyclerView.addOnScrollListener(new InfiniteScrollListener() {
        @Override
        protected void loadMore() {
            boolean willLoad = //get your last api call data. if empty set false
            int offset = recyclerView.getAdapter().getItemCount();
            if(willLoad){

                willLoad=false;
                onCallApi(offset,10);
            }

        }
    });

your api:

 private void onCallApi(int offset,int limit){
    //your api
}
vishal patel
  • 294
  • 2
  • 4
  • 13