Oreo
Oreo

Reputation: 2594

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);
                    }
                }
            }
        });
    }

Upvotes: 12

Views: 18093

Answers (3)

Veeresh Charantimath
Veeresh Charantimath

Reputation: 4719

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();

Upvotes: 2

Ragesh Ramesh
Ragesh Ramesh

Reputation: 3520

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.

Upvotes: 1

Veeresh Charantimath
Veeresh Charantimath

Reputation: 4719

The Implementation seems to be correct but for one condition it fails, try when dy > 0 like this (Also put this in the OnCreate of the Activity) :

    private boolean loading = true;

 recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
        }


        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy)
        {

            if (loading) {
                if (dy > 0) //check for scroll down
                {
                    visibleItemCount = layoutManager.getChildCount();
                    totalItemCount = layoutManager.getItemCount();
                    pastVisiblesItems = layoutManager.findFirstVisibleItemPosition();

                    if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) {
                        loading = false;

                        Log.v("...", " Reached Last Item");
                        loadMoreVideos(searchVideos);
                    }

                }
            }
        }
    });

Upvotes: 10

Related Questions