I have applied the pagination as per the below code but the new data is not added to the Recyclerview. it loading previous data again and again. In this, I have applied the filter in the recycler view to filter the list according to the category. But I am unable to apply the pagination to the filterable recyclerview. Please assist.
I am using Firestore database in Android Application. If any more information is required, Please let me know
CODE
public void onFilter(Filters filters) {
// Construct query basic query
Query query = mFirestore.collection("posts").orderBy("timestamp", Query.Direction.DESCENDING);
// Category (equality filter)
if (filters.hasCategory()) {
query = query.whereEqualTo("postcategory", filters.getCategory());
}
// Limit items
query = query.limit(5);
Query finalQuery = query;
finalQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (DocumentSnapshot document : task.getResult()) {
PostsModel productModel = document.toObject(PostsModel.class);
mQuery = finalQuery;
adapter.setQuery(mQuery);
// list.add(productModel);
}
adapter.notifyDataSetChanged();
lastVisible = task.getResult().getDocuments().get(task.getResult().size() - 1);
RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
isScrolling = true;
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
LinearLayoutManager linearLayoutManager = ((LinearLayoutManager) recyclerView.getLayoutManager());
int firstVisibleItemPosition = linearLayoutManager.findFirstVisibleItemPosition();
int visibleItemCount = linearLayoutManager.getChildCount();
int totalItemCount = linearLayoutManager.getItemCount();
if (isScrolling && (firstVisibleItemPosition + visibleItemCount == totalItemCount) && !isLastItemReached) {
isScrolling = false;
Query nextQuery = PostsRef.orderBy("timestamp", Query.Direction.DESCENDING).startAfter(lastVisible).limit(5);
nextQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> t) {
if (t.isSuccessful()) {
for (DocumentSnapshot d : t.getResult()) {
PostsModel productModel = d.toObject(PostsModel.class);
mQuery = finalQuery;
adapter.setQuery(mQuery);
// list.add(productModel);
}
adapter.notifyDataSetChanged();
lastVisible = t.getResult().getDocuments().get(t.getResult().size() - 1);
if (t.getResult().size() < 5) {
isLastItemReached = true;
}
}
}
});
}
}
};
recyclerView.addOnScrollListener(onScrollListener);
}
}
});
// Update the query
// Set header
mCurrentSearchView.setText(Html.fromHtml(filters.getSearchDescription(getActivity().getApplicationContext())));
// Save filters
mViewModel.setFilters(filters);
}