Skip to content Skip to sidebar Skip to footer

How To Implement Pagination With Recyclerview Via Api Url Indexing

I know the title seems weird, but basically what I want is to apply pagination to my list and control it using the url api endpoint as a parameter.! so the endpoint looks like this

Solution 1:

Here is how I did it in a project... In this case I add more elements when bottom of the list is reached

rv_flohtainer_events.addOnScrollListener(newRecyclerView.OnScrollListener() {
        @OverridepublicvoidonScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            if (!rv_flohtainer_events.canScrollVertically(1)){
                if (!(eventsResponse.getCurrent_page() == eventsResponse.getLast_page())) {
                    pageEventsRequest++;
                    makeVendorEventsRequest();
                }
            }
        }
    });

The request I do is with retrofit, and it looks like this

@GET(RestClient.API_EVENTS_URL)
Call<EventsResponse> getEvents(@Query("page") int page)

Solution 2:

After some time, I did it using Paginate Library


The Code

publicclassChallengesListFragmentextendsBaseFragmentimplementsOnRefreshListener, Paginate.Callbacks { 

// other variables..private int currentPage = 1;
  privateboolean hasMore;
  privateboolean loadingInProgress;


  @RequiresApi(api = VERSION_CODES.LOLLIPOP)
  @OverridepublicViewonCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_challenges_list, container, false);
    initView(rootView);
    initToolbar();
    initRecycler();
    getData();

    return rootView;
  }

  privatevoidinitRecycler() {
    data = newArrayList<>();
    mLayoutManager = newLinearLayoutManager(getActivity());
    recyclerview.setLayoutManager(mLayoutManager);
    adapter = newChallengesAdapter(data, getActivity(), this);
    recyclerview.setAdapter(adapter);

    Paginate.with(recyclerview, this)
        .setLoadingTriggerThreshold(3)
        .addLoadingListItem(false)
        .build();
  }

  privatevoidgetData() {

     swipeRefresh.setRefreshing(true);
    WebRequests.GetActiveChallenges(currentPage, newonResult() {
      @OverridepublicvoidonSuccess(Objectobject) {

        ArrayList<Challenge> list = (ArrayList<Challenge>) object;
        data.addAll(list);
        adapter.notifyDataSetChanged();

        if (list.size() == 10) {
          currentPage++;
          hasMore = true;
        } else {
          hasMore = false;
        }
        loadingInProgress = false;
        swipeRefresh.setRefreshing(false);


      }

      @OverridepublicvoidonFail(Objectobject) {
        Toast.makeText(getActivity(), "Something went wrong, try again later",
            Toast.LENGTH_SHORT).show(); 
        swipeRefresh.setRefreshing(false);

      }
    });

  } 

  publicChallengesListFragment() {
   } 

  @OverridepublicvoidonRefresh() {
    currentPage = 1;
    loadingInProgress = true;
    getData();
   }

  @OverridepublicvoidonLoadMore() {
    loadingInProgress = true;
     Handler mHandler = newHandler();
    final Runnable hintRunnable = newRunnable() {
      @Overridepublicvoidrun() {
        getData();
      }
    };
    mHandler.postDelayed(hintRunnable, 1500);

  }

  @OverridepublicbooleanisLoading() {
    return loadingInProgress;
  }

  @OverridepublicbooleanhasLoadedAllItems() {
    return !hasMore;
  } 

}

Post a Comment for "How To Implement Pagination With Recyclerview Via Api Url Indexing"