I have around 32 records in json, I am using RecyclerView to show them and I have implemented OnScrollListener(...)
Question
I started an Activity, I fetched all 32 records, now when I do scroll, why I am again getting same 32 records again and again, whenever I do scroll, here is my implementation of OnScrollListener()
public void initializeOnScrollForRecyclerView() {
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
int visibleItemCount = recyclerView.getLayoutManager().getChildCount();
int totalItemCount = recyclerView.getLayoutManager().getItemCount();
int pastVisiblesItems = ((LinearLayoutManager) recyclerView.getLayoutManager()).findFirstVisibleItemPosition();
if (!isLoading) {
if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) {
isLoading = true;
mPostPresenter.loadPosts(false);
}
}
}
});
}
Here's another way to trigger something when last item is reached
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
//When last item is reached
if(position == yourList.size() - 1){
loadMoreMessages();
}
}
public abstract void loadMoreMessages();
You are using this
if ((visibleItemCount + pastVisiblesItems) >= totalItemCount)
Which means once you have viewed 22 records you are loading again. And while loading again you are getting same data and appending it to your list.
I can see that you are using RXjava. When you are subscribing in loadPost check what data your observable is emitting. i think its emitting the same data again i.e your 32 records and those records are added again and this is an endless loop.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With