# Class InfiniteScrollAdapter

java.lang.Object
com.codename1.components.InfiniteScrollAdapter

public final class InfiniteScrollAdapter extends Object

Allows adapting a scroll container to scroll indefinitely (or at least until running out of data), this effectively works by showing an infinite progress indicator when reaching scroll end then allowing code to fetch additional components.

Warning: If you call `com.codename1.ui.Container#removeAll()` on the container to which an InfiniteScrollAdapter is installed, it will disable the infinite scrolling behavior. You can re-enable infinite scrolling by calling `com.codename1.ui.Component[], boolean)` again.

The sample code shows the usage of the nestoria API to fill out an infinitely scrolling list.

    public void showForm() {
        Form hi = new Form("InfiniteScrollAdapter", new BoxLayout(BoxLayout.Y_AXIS));

        Style s = UIManager.getInstance().getComponentStyle("MultiLine1");
        FontImage p = FontImage.createMaterial(FontImage.MATERIAL_PORTRAIT, s);
        EncodedImage placeholder = EncodedImage.createFromImage(p.scaled(p.getWidth() * 3, p.getHeight() * 3), false);

        InfiniteScrollAdapter.createInfiniteScroll(hi.getContentPane(), () -> {
            java.util.List> data = fetchPropertyData("Leeds");
            MultiButton[] cmps = new MultiButton[data.size()];
            for(int iter = 0 ; iter  currentListing = data.get(iter);
                if(currentListing == null) {
                    InfiniteScrollAdapter.addMoreComponents(hi.getContentPane(), new Component[0], false);
                    return;
                }
                String thumb_url = (String)currentListing.get("thumb_url");
                String guid = (String)currentListing.get("guid");
                String summary = (String)currentListing.get("summary");
                cmps[iter] = new MultiButton(summary);
                cmps[iter].setIcon(URLImage.createToStorage(placeholder, guid, thumb_url));
            }
            InfiniteScrollAdapter.addMoreComponents(hi.getContentPane(), cmps, true);
        }, true);
        hi.show();
    }
    int pageNumber = 1;
    java.util.List> fetchPropertyData(String text) {
        try {
            ConnectionRequest r = new ConnectionRequest();
            r.setPost(false);
            r.setUrl("http://api.nestoria.co.uk/api");
            r.addArgument("pretty", "0");
            r.addArgument("action", "search_listings");
            r.addArgument("encoding", "json");
            r.addArgument("listing_type", "buy");
            r.addArgument("page", "" + pageNumber);
            pageNumber++;
            r.addArgument("country", "uk");
            r.addArgument("place_name", text);
            NetworkManager.getInstance().addToQueueAndWait(r);
            Map result = new JSONParser().parseJSON(new InputStreamReader(new ByteArrayInputStream(r.getResponseData()), "UTF-8"));
            Map response = (Map)result.get("response");
            return (java.util.List>)response.get("listings");
        } catch(Exception err) {
            Log.e(err);
            return null;
        }
    }

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `addMoreComponents(Component[] components, boolean areThereMore)`
  Invoke this method to add additional components to the container, if you use addComponent/removeComponent you will get undefined behavior.
  `static void`
  `addMoreComponents(Container cnt, Component[] components, boolean areThereMore)`
  Invoke this method to add additional components to the container, if you use addComponent/removeComponent you will get undefined behavior.
  `void`
  `continueFetching()`
  If we previously added components with false for are there more this method can continue the process of fetching.
  `static void`
  `continueFetching(Container cnt)`
  If we previously added components with false for are there more this method can continue the process of fetching.
  `static InfiniteScrollAdapter`
  `createInfiniteScroll(Container cont, Runnable fetchMore)`
  Creates an instance of the InfiniteScrollAdapter that will invoke the fetch more callback to fetch additional components, once that method completes its task it should add the components via the addMoreComponents() invocation.
  `static InfiniteScrollAdapter`
  `createInfiniteScroll(Container cont, Runnable fetchMore, boolean fetchOnCreate)`
  Creates an instance of the InfiniteScrollAdapter that will invoke the fetch more callback to fetch additional components, once that method completes its task it should add the components via the addMoreComponents() invocation.
  `int`
  `getComponentLimit()`
  The component limit defines the number of components that should be within the infinite scroll adapter, if more than component limit is added then the appropriate number of components is removed from the top.
  `InfiniteProgress`
  `getInfiniteProgress()`
  Lets us manipulate the infinite progress object e.g.
  `void`
  `setComponentLimit(int componentLimit)`
  The component limit defines the number of components that should be within the infinite scroll adapter, if more than component limit is added then the appropriate number of components is removed from the top.

  ### Methods inherited from class Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Method Details

  - ### createInfiniteScroll

    public static InfiniteScrollAdapter createInfiniteScroll(Container cont, Runnable fetchMore)

    Creates an instance of the InfiniteScrollAdapter that will invoke the fetch more callback to fetch additional components, once that method completes its task it should add the components via the addMoreComponents() invocation. Notice that the container MUST be empty when invoking this method, fetchMore will be invoked immediately and you can add your data when ready.

    ###### Parameters

    - `cont`: the container to bind, it MUST be empty and must be scrollable on the Y axis

    - `fetchMore`: a callback that will be invoked on the EDT to fetch more data (do not block this method)

    ###### Returns

    an instance of this class that can be used to add components

  - ### createInfiniteScroll

    public static InfiniteScrollAdapter createInfiniteScroll(Container cont, Runnable fetchMore, boolean fetchOnCreate)

    Creates an instance of the InfiniteScrollAdapter that will invoke the fetch more callback to fetch additional components, once that method completes its task it should add the components via the addMoreComponents() invocation. Notice that the container MUST be empty when invoking this method, fetchMore will be invoked immediately and you can add your data when ready.

    ###### Parameters

    - `cont`: the container to bind, it MUST be empty and must be scrollable on the Y axis

    - `fetchMore`: a callback that will be invoked on the EDT to fetch more data (do not block this method)

    - `fetchOnCreate`: if true the fetchMore callback is called upon calling this method

    ###### Returns

    an instance of this class that can be used to add components

  - ### addMoreComponents

    public static void addMoreComponents(Container cnt, Component[] components, boolean areThereMore)

    Invoke this method to add additional components to the container, if you use addComponent/removeComponent you will get undefined behavior. This is a convenience method saving the need to keep the InfiniteScrollAdapter as a variable

    ###### Parameters

    - `cnt`: container to add the components to

    - `components`: the components to add

    - `areThereMore`: whether additional components exist

  - ### continueFetching

    public static void continueFetching(Container cnt)

    If we previously added components with false for are there more this method can continue the process of fetching. This is useful in case of a networking error. You can end fetching and then restart it based on user interaction see https://github.com/codenameone/CodenameOne/issues/2721

    ###### Parameters

    - `cnt`: the container associated with the infinite scroll adapter

  - ### addMoreComponents

    public void addMoreComponents(Component[] components, boolean areThereMore)

    Invoke this method to add additional components to the container, if you use addComponent/removeComponent you will get undefined behavior.

    ###### Parameters

    - `components`: the components to add

    - `areThereMore`: whether additional components exist

  - ### continueFetching

    public void continueFetching()
    If we previously added components with false for are there more this method can continue the process of fetching. This is useful in case of a networking error. You can end fetching and then restart it based on user interaction see https://github.com/codenameone/CodenameOne/issues/2721

  - ### getComponentLimit

    public int getComponentLimit()

    The component limit defines the number of components that should be within the infinite scroll adapter, if more than component limit is added then the appropriate number of components is removed from the top. This prevents running out of memory or performance overhead with too many components... Notice that -1 is a special case value for no component limit.

    ###### Returns

    the componentLimit

    ###### Deprecated

    this feature has some inherent problems and doesn't work as expected

  - ### setComponentLimit

    public void setComponentLimit(int componentLimit)

    The component limit defines the number of components that should be within the infinite scroll adapter, if more than component limit is added then the appropriate number of components is removed from the top. This prevents running out of memory or performance overhead with too many components... Notice that -1 is a special case value for no component limit.

    ###### Parameters

    - `componentLimit`: the componentLimit to set

    ###### Deprecated

    this feature has some inherent problems and doesn't work as expected

  - ### getInfiniteProgress

    public InfiniteProgress getInfiniteProgress()

    Lets us manipulate the infinite progress object e.g. set the animation image etc.

    ###### Returns

    the infinite progress component underlying this adapter