I am fetching data from firebase. At first i am fetching only 10 items then when user scrolls recyclerview then next 10 items are fetching. Now my problem is that i am getting duplicate data when i am scrolling recyclerview. I am not able to find out why is this happening. Why same data is fetching again. Please look at my code and tell me my mistake.
At first time when i am fetching only 10 items :
content = getIntent().getStringExtra("jokecontent");
funnyJokesQuery = FirebaseDatabase.getInstance().getReference("ContentJokes").orderByChild("subcategory")
.equalTo(content).limitToFirst(10);
funnyJokesQuery.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
String contentdisplay = dataSnapshot.child("content").getValue(String.class);
ContentModel contentModel = new ContentModel(contentdisplay);
contentname.add(0,contentModel);
arrayAdapter.notifyDataSetChanged();
progressBar.setVisibility(View.GONE);
arrayAdapter.notifyItemInserted(0);
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
Code of onScrollListener of recyclerview for fetching next items:
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if(newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL)
{
isScrolling = true;
}
}
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
currentitems = manager.getChildCount();
totalitems = manager.getItemCount();
scrollOutItems = manager.findFirstVisibleItemPosition();
lastItemPosition = manager.findLastVisibleItemPosition();
if(isScrolling &&(currentitems +scrollOutItems == totalitems)){
funnyJokesQuery = FirebaseDatabase.getInstance().getReference("ContentJokes")
.orderByChild("subcategory")
.startAt(lastItemPosition)
.limitToFirst(11);
progressBar.setVisibility(View.VISIBLE);
funnyJokesQuery.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
String contentdisplay = dataSnapshot.child("content").getValue(String.class);
ContentModel contentModel = new ContentModel(contentdisplay);
contentname.add(lastItemPosition + 1, contentModel);
arrayAdapter.notifyDataSetChanged();
progressBar.setVisibility(View.GONE);
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
}
});