-
Notifications
You must be signed in to change notification settings - Fork 7
6. Pagination
Andrii Konovalenko edited this page Apr 12, 2017
·
3 revisions
The library offers you a mechanism for working with pagination without the need to explicitly control the request headers. Consider the sequence of actions for working with pagination in your project.
a) In App class call configurePagination method
EasyNet.getInstance()
.setDefaultNBuilderListener(new EasyNet.NBuilderDefaultListener() {
@Override
public Request defaultConfig(Request request) {
return request
.setHost(APIConfig.API_URL)
.configurePagination("offset", "limit");
}
})
Specify the keys that your API uses for pagination.
b) Implement the PaginationInterface
, for example in recycler adapter
public class NotesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements
PaginationModel.PaginationInterface{
private List<SomeModel> models;
...
@Override
public int getPaginationValue(String key) {
switch (key) {
case "limit":
return 20;
case "offset":
return models.size();
default:
return 0;
}
}
}
c) Start the request with enablePagination(PaginationInterface)
calling
EasyNet.get("path")
.enablePagination(adapter)
.start(new NCallbackGson<SomeModel>(SomeModel.class) {
@Override
public void onSuccess(List<SomeModel> models, NResponseModel responseModel) {
adapter.addAll(models);
}
});
The library will be automatically send pagination headers with current counters.
Note!
I recommend use this library for dynamic lists https://github.com/jaksab/TrueRecyclerAdapter.