When using Paging Library, observer showing list size as zero
To get the size of the item inserted or size of that RecyclerView, we need to register an AdapterDataObserver
to our RecyclerView.
1) Create an AdapterDataObserver and override the functions you want.
public class SearchListAdapterDataObserver extends RecyclerView.AdapterDataObserver {
private RecyclerView mRecyclerView;
private ChangeListener mChangeListener;
public SearchListAdapterDataObserver(RecyclerView view, ChangeListener changeListener){
this.mRecyclerView = view;
this.mChangeListener = changeListener;
}
@Override
public void onChanged() {
super.onChanged();
sendItemCount();
}
private void sendItemCount() {
if(mRecyclerView.getAdapter() != null) {
mChangeListener.onChanged(getSize());
}
}
@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
super.onItemRangeInserted(positionStart, itemCount);
sendItemCount();
}
@Override
public void onItemRangeChanged(int positionStart, int itemCount) {
super.onItemRangeChanged(positionStart, itemCount);
sendItemCount();
}
@Override
public void onItemRangeRemoved(int positionStart, int itemCount) {
super.onItemRangeRemoved(positionStart, itemCount);
sendItemCount();
}
public int getSize() {
if(mRecyclerView.getAdapter() != null) {
return mRecyclerView.getAdapter().getItemCount();
}
return 0;
}
public interface ChangeListener {
void onChanged(int size);
}
}
2) Register it to your adapter and listen to the changes.
if(mViewModel.getSearchPaginatedAdapter() != null){
mViewModel.getSearchPaginatedAdapter().registerAdapterDataObserver
(new SearchListAdapterDataObserver(mRecyclerView, new SearchListAdapterDataObserver.ChangeListener() {
@Override
public void onChanged(int size) {
onListChanges(size);
}
}));
}
You can implement BoundaryCallback in LivePagedListBuilder. It contains method onZeroItemsLoaded.
listLiveData = new LivePagedListBuilder<>
(new MyPatientPagedListProvider(context)
.getAll()
, pagedListConfig)
.setBoundaryCallback(new PagedList.BoundaryCallback() {
@Override
public void onZeroItemsLoaded() {
super.onZeroItemsLoaded();
// do smth here. For example, post boolean value to MutableLiveData to notify activity //that result is empty
}
})
.build();
Here is the simple solution of pagination .
First time private void jsonRequestList(int pageCount) {
will called with pageCount=1
and next time increment with 2,3,4...
Inside private void jsonRequestList(int pageCount) {
method (JSON REQUEST) first time if (!isScrollCalled) {
will called & next time else
block will called.
Activity/Fragment :
private boolean loading = false;
private boolean isScrollCalled;
int isLastPage = 10;
int pageCount = 1;
//Paigination
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
int lastvisibleitemposition = linearLayoutManager.findLastVisibleItemPosition();
if (lastvisibleitemposition == adapter.getItemCount() - 1) {
if (!loading && pageCount != isLastPage) {
loading = true;
jsonRequestList(++pageCount);
isScrollCalled = Boolean.TRUE;
}
}
}
});
private void jsonRequestList(int pageCount) {
//Json Request
if (!isScrollCalled) {
adapter = new FlowerListAdapter(FlowerListActivity.this, list);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
// Log.e("LIST_SIZE", "" + list.size());
} else {
adapter.updateList(list);
//Log.e("LIST_SIZE", "" + list.size());
}
loading = false;
}
Recycler Adapter :
ArrayList<YourModelClass> list;
Context context;
//Pagination
public void updateList(ArrayList<YourModelClass> list) {
this.list.addAll(list);
this.notifyDataSetChanged();
}