Pagination With RxJava In Android
Posted By : Rahul Baboria | 30-Apr-2018
Introduction:
RxJava is based on live data, pagination is also kind of live data scheme where when a user scrolls it loads more and more data.
Paging Library Components
The principal segment of a paging library are DataSource, PagedList, and PagedListAdapter
DataSource
This a segment in charge of stacking the information. Contingent upon how you have to get to your information, you would expand one of its subclasses. At our utilization case, we stack the following page by knowing the last component id in the wake of perusing the docs sound helpful to utilize ItemKeyedDataSource for more points of interest and different subclass check the docs.
PagedList
This an A wrapper list that holds your information and advises the information source when to stack information. You can design how much information is stacked at once, and how much information ought to be prefetched. It gives updates to the connector as the information stacked in pages
PagedListAdapter
This class is the usage of RecyclerView.adapter that presents information from PagedList and take DiffUtil as a parameter to figure the distinction in the information and do all the refreshing work for you.
How about we consider we have REST API here when the information originates from DataSource on a foundation string. It will refresh the PagedList value, The PagedList informs its eyewitnesses of the new rundown on the mainThread. Presently the PagedListAdapter will get the new rundown. On the foundation,
Add following dependencies in build.gradle
//Networking retrofit implementation
com.squareup.retrofit2:retrofit:2.3.0
implementation com.squareup.retrofit2:converter-gson:2.3.0
implementation com.squareup.retrofit2:adapter-rxjava2:2.3.0
//RxJava implementation
io.reactivex.rxjava2:rxjava:2.1.9
implementation io.reactivex.rxjava2:rxandroid:2.0.2
Disposable disposable = paginator
.onBackpressureDrop()
.subscribeOn(Schedulers.io())
.concatMap(new Function<Integer, Publisher<List<Model>>>() {
@Override
public Publisher<List<Model>> apply(@NonNull Integer page) throws Exception {
loading = true;
// show progressbar
return networkService.getDataFromApi(accessToken,new AppContext(mContext).getKeyApiSession(), page+"")
.subscribeOn(Schedulers.newThread())
.doOnError(new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
// handle exceptions here
}
});
}
})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<List<Model>>() {
@Override
public void accept(@NonNull List<Hotel> items) throws Exception {
//paginationAdapter.addItems(items);
//paginationAdapter.notifyDataSetChanged();
loading = false;
// hide loadmore
}
});
finally add disposable to composite disposables list.
compositeDisposable.add(disposable);
paginator.onNext(pageNumber);
We need addOnScrollLister to the recycled view when user scrolls it triggers according to a threshold. Let's take an example, if VISIBLE_THRESHHOLD = 50, infinite scroll API will be called if listing contains less than 50 items from the current position.
private void setUpLoadMoreListener() {
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView,
int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
totalItemCount = layoutManager.getItemCount();
lastVisibleItem = layoutManager
.findLastVisibleItemPosition();
if (!loading
&& totalItemCount <= (lastVisibleItem + VISIBLE_THRESHOLD)) {
pageNumber++;
paginator.onNext(pageNumber);
loading = true;
}
}
});
}
Thanks.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Rahul Baboria
Rahul Baboria is having good knowledge over Android Application.