I search web a lot as well as stackoverflow but my question is remaining same. I tried a lot but not working.
After a lots of try, I wrote this code but its working only one-way. I mean I can scroll up and pagination working nicely (with some flashes :) ) but its not allow me to scroll up. I mean new items are adding continuously but I cannot browse previous items of recyclerview.
Here's my code
mQuery = mFirestore.collection("artists")
.orderBy(Artist.FIELD_ARTISTNAME, Query.Direction.ASCENDING)
.limit(LIMIT);
fillList(mQuery);
mQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
lastVisible = task.getResult().getDocuments().get(task.getResult().size() - 1);
Toast.makeText(MainActivity.this, "first page loaded", Toast.LENGTH_SHORT).show();
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);
int firstVisibleItem = ((LinearLayoutManager) recyclerView.getLayoutManager()).findFirstVisibleItemPosition();
int visibleItemCount = linearLayoutManager.getChildCount();
int totalItemCount = linearLayoutManager.getItemCount();
Toast.makeText(MainActivity.this, firstVisibleItem + " and " + visibleItemCount + " and " + totalItemCount, Toast.LENGTH_SHORT).show();
if (isScrolling && (firstVisibleItem + visibleItemCount == totalItemCount) && !isLastItemReached) {
isScrolling = false;
mQuery = mFirestore.collection("artists")
.orderBy(Artist.FIELD_ARTISTNAME, Query.Direction.ASCENDING)
.startAfter(lastVisible)
.limit(LIMIT);
mQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
fillList(mQuery);
mAdapter.notifyDataSetChanged();
lastVisible = task.getResult().getDocuments().get(task.getResult().size() - 1);
Toast.makeText(MainActivity.this, "next page loaded", Toast.LENGTH_SHORT).show();
if (task.getResult().size() < LIMIT) {
isLastItemReached = true;
}
}
});
}
}
};
mRestaurantsRecycler.addOnScrollListener(onScrollListener);
}
});
Here's code for fillList function
public void fillList(Query myQuery) {
// RecyclerView
mAdapter = new ArtistAdapter(myQuery, this) {
@Override
protected void onDataChanged() {
// Show/hide content if the query returns empty.
if (getItemCount() == 0) {
mRestaurantsRecycler.setVisibility(View.GONE);
mEmptyViewText.setText(getResources().getString(R.string.message_no_results));
mEmptyViewExit.setTag("noRecords");
mEmptyViewExit.setText(getResources().getString(R.string.no_records_button));
mEmptyView.setVisibility(View.VISIBLE);
} else {
mRestaurantsRecycler.setVisibility(View.VISIBLE);
mEmptyView.setVisibility(View.GONE);
}
}
@Override
protected void onError(FirebaseFirestoreException e) {
// Show a snackbar on errors
Snackbar.make(findViewById(android.R.id.content),
"Error: check logs for info.", Snackbar.LENGTH_LONG).show();
}
};
mRestaurantsRecycler.setLayoutManager(new LinearLayoutManager(this));
mRestaurantsRecycler.setAdapter(mAdapter);
}