I am working on a Android project where I am showing 1000+ text formatted data in the recyclerview and I am taking data from Firebase Realtime database, now problem is when user open app and scroll they face lagging issue so that is why I decided to add pagination in the Recyclerview using this I and load only few amounts of data and when user scroll the recyclerview and go to the end point they got another few data, In this way I can handle the recyclerview lagging issue. I search for it in the google and go through the different tutorials but I am not getting the actual code & I am also a beginner so that is why I am not understand how to implement it, so anyone please help me to figure out....I am pasting my code here
Main Recyclerview Fragment Class
private List<QuotesItem> quoteList;
private DatabaseReference dbquote;
private QuotesAdapter adapter;
private LinearLayoutManager layoutManager;
final int ITEM_LOAD_COUNT = 10;
int total_item = 0, last_visible_item;
boolean isLoading = false, isMixData = false;
String last_node = "", last_key = "";
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
binding.homeProgress.setVisibility(View.VISIBLE);
getDataFromFirebase();
binding.homeShayari.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
binding.homeShayari.setLayoutManager(layoutManager);
adapter = new QuotesAdapter(getContext());
binding.homeShayari.setAdapter(adapter);
getUsers();
binding.homeShayari.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
total_item = layoutManager.getItemCount();
last_visible_item = layoutManager.findLastVisibleItemPosition();
if (!isLoading && total_item <= ((last_visible_item + ITEM_LOAD_COUNT))) {
getUsers();
isLoading = true;
}
}
});
binding.homeShayariRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
binding.homeShayariRefresh.setRefreshing(false);
RearrangeItems();
}
}, 1000);
}
});
}
private void getUsers() {
if (!isMixData) {
Query query;
if (TextUtils.isEmpty(last_node)) {
query = FirebaseDatabase.getInstance().getReference()
.child("quotes")
.orderByKey()
.limitToFirst(ITEM_LOAD_COUNT);
} else {
query = FirebaseDatabase.getInstance().getReference()
.child("quotes")
.orderByKey()
.startAt(last_node)
.limitToFirst(ITEM_LOAD_COUNT);
}
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (snapshot.hasChildren()) {
List<QuotesItem> quotes = new ArrayList<>();
for (DataSnapshot data : snapshot.getChildren()) {
for (DataSnapshot snap : data.getChildren()) {
quotes.add(snap.getValue(QuotesItem.class));
}
}
last_node = quotes.get(quotes.size() - 1).getId();
if (!last_node.equals(last_key)) {
quotes.remove(quotes.size() - 1);
} else {
last_node = "end";
}
adapter.addAll(quotes);
isLoading = false;
} else {
isLoading = false;
isMixData = true;
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
isLoading = false;
}
});
}
}
private void getDataFromFirebase() {
Query query = FirebaseDatabase.getInstance().getReference()
.child("quotes")
.orderByKey()
.limitToLast(1);
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot quotesKey : snapshot.getChildren()) {
for (DataSnapshot data : quotesKey.getChildren()) {
last_key = data.getKey();
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
And here is my firebase realtime database nodes