Endless recycler view scoll listener called when there are not enough items on screen

endlessrecyclerviewscrolllistener
endless scrolling recyclerview android kotlin
android gridview automatically load more data when scroll to the bottom
sqlite pagination android recyclerview

I implemented this to get inifinte scrolling working with my recycler view. I set it up like this:

mRecyclerView.addOnScrollListener(new EndlessRecyclerViewScrollListener(glm) {
        @Override
        public void onLoadMore(int page, int totalItemsCount) {
            anotherOne(page, mScope, mCurrentSortPreference, false);
            Log.v(LOG_TAG, "onloadmore called");
        }
    });

The method anotherOne() loads data in the recycler view from an API with an async task. If I have 20+ items to fetch from the API (in th einitial call) it works fine. This is because my grid allows to diplay maximum 20 results on screen at the same time.

But if the api returns few items (like 2 or 3) instead of only those being displayed, the onLoadMore() gets called repeatedly, until it fills the screen with the same 2 or 3 items. Then it stops.

Schreenshot

This is the only line I modified in the EndlessRecyclerViewScrollListener.java:

private int visibleThreshold = 1;

Thank you.

E: added code

private void anotherOne(int currPage, int scope, String sort, boolean o){ 
    String override = Boolean.valueOf(o).toString();
    (new FetchMoviesTask()).execute(Integer.valueOf(currPage).toString(),
            Integer.valueOf(scope).toString(), sort, override, "");
}

From the source documentation:

// The minimum number of items to have below your current scroll position

// before loading more.

private int visibleThreshold = 5;

This means that it must have at least visibleThreshold more itens not being shown, in order to be safe-ish and not gather more data... so you are requesting:

private int visibleThreshold = 1;

This means that your data set must have "full screen plus visibleThreshold", since you have no more data, it assumes you do, requests the wrong offset, and gets the last one repeatedly, until you have "full screen plus visibleThreshold" data units.

Lower your Treshold, or return false on onLoadMore

Edit:
From documentation again, please try the following:

@Override
      public boolean onLoadMore(int page, int totalItemsCount) {
          // Triggered only when new data needs to be appended to the list
          // Add whatever code is needed to append new items to your AdapterView
          loadNextDataFromApi(totalItemsCount); 
          return true; // ONLY if more data is actually being loaded; false otherwise.
      }

public void loadNextDataFromApi(int offset) {
  // Send an API request to retrieve appropriate paginated data 
  //  --> Send the request including an offset value (i.e `page`) as a query parameter.
  //  --> Deserialize and construct new model objects from the API response
  //  --> Append the new data objects to the existing set of items inside the array of items
  //  --> Notify the adapter of the new items made with `notifyDataSetChanged()`
}

Android RecyclerView Load More, Endless Scrolling, If there aren't enough items on the screen, the recyclerView will automatically scroll and the onScrolled method would be called. Since there aren't  It was not working for me because I was wrapping the recycler view in a scroll view in my layout file. I spent days to find that it was the root cause of the issue and that I should remove the scroll view.

In my Case i forget to add line.Hope it help somebody. recyclerView.addOnScrollListener(scrollListener);

Endless RecyclerView OnScrollListener · GitHub, How to create infinite scrolling RecyclerView items in android app example. to the end of the RecyclerView, we need to implement OnScrollListener() on the RecyclerView. Enough Talk. It's there where we check the scrolled state of the RecyclerView and if the bottom-most Your email address will not be published. Endless Scroll Listener for Recycler Views. Endless Scrolling for Paginated APIs as shown in : Google Design - Progress Activity Behavior which has the following components: Endless Scroll Listener for Recycler Views; concrete class implements loadMore(pageNumber) to signal the API to load more data; List Adapter which shows: Empty View when

If you set a breakpoint in the onScrolled() method in EndlessRecyclerViewScrollListener you will know why. If there aren't enough items on the screen, the recyclerView will automatically scroll and the onScrolled method would be called. Since there aren't enough items to occupy the screen, the lastVisibleItemPosition + threshold will always be larger than the totalNumberOfItems. As a result of which, the onLoadMore method will be repeatedly called.

Problem with endless scrolling and LOAD_MORE_RESET delay , private int previousTotal = 0; // The total number of items in the dataset of items to have below your current scroll position before loading more. public EndlessRecyclerOnScrollListener(LinearLayoutManager linearLayoutManager) { RecyclerView not have enough (2 or 3 row) data as height of screen. The LayoutManager, which is responsible in the RecyclerView for rendering where items should be positioned and manages scrolling, provides information about the current scroll position relative to the adapter. For this reason, we need to pass an instance of what LayoutManager is being used to collect the necessary information to ascertain when

Endless Scrolling with AdapterViews and RecyclerView, Working with the endless scrolling feature and items limit per page in server replies, you should receive enough elements with 1 call only, if there are no items that, once rendeded on screen, will make the recyclerview scrollable. no load more, then you need to scroll down to trigger again the listener. Now call addOnScrollListener () on your RecyclerView to enable endless scrolling and pass EndlessRecyclerViewOnScollListener as parameter in addOnScrollListener () and implement onLoadMore () method which would be called when user scroll down bottom in the list.

Using the RecyclerView, The approaches for ListView, GridView and RecyclerView (the successor to ListView) To provide the appearance of endless scrolling, it's important to fetch data Pass in an instance of EndlessRecyclerViewScrollListener and implement the if there is no more data to load. public abstract boolean onLoadMore(int page,  Imagine, your list of emails occupies full screen height and can contain 10 emails to be showed. You start scrolling up. ⬆️. Once the top view is out of view, it is being reused by RecyclerView, with new data and pushed as a new bottom element. Something like that. Enough of theory, let’s do practical part! 🔨

Pagination in RecyclerView in Android, OnItemClickListener interface for binding to the click events for individual items in LinearLayoutManager shows items in a vertical or horizontal scrolling list. There is a Logcat error though E/RecyclerView: No layout manager attached; to call notifyDataSetChanged() on the entire adapter to update the entire screen,​  Recyclerview and cardview dependency. Add recyclerview in activity_main.xml where you removed the textview as shown below. <android.support.v7.widget.RecyclerView android:layout_width="match

Comments
  • Do you check if you are not downloading data already? I mean perhaps there are few requests going for the same set of data and hence the duplicates
  • From the logcat I see that this log is being spammed Log.v(LOG_TAG, "onloadmore called");
  • add some flag, that will block further load until the previous one finished
  • I think you need add some code to check that the items.size() < size_data_on_api then call anotherOne() so if the items.size() already same that will not call repeatedly
  • There must be an easier way, this involves lots of coding
  • If I set visibleThreshold to 0 it doesn't load anything even when it should.
  • Edit your question, detail the anotherOne method. Read the answer again. If your Treshold is 0, then you have 0 more itens "out of screen", in your onLoadMore, you should check the total or if the data set received is not "complete", "empty", or even "failed".
  • I edited the answer. I don't understand very good. What should I check for in onLoadMore? it gives me the total item and the current page, but I don't even know what totalItems is.
  • Just out of curiosity, with not enough items instantiated with the recyclerView, can you actually see the new items being added to the recyclerView if you dont manually scroll the screen?