Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView OnScrollListener() Issue

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);
                    }
                }
            }
        });
    }
like image 226
Oreo Avatar asked Mar 12 '16 06:03

Oreo


2 Answers

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();
like image 142
Veeresh Charantimath Avatar answered Nov 07 '22 21:11

Veeresh Charantimath


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.

like image 42
Ragesh Ramesh Avatar answered Nov 07 '22 20:11

Ragesh Ramesh