public final class InfiniteScrollAdapter

  1. Object
  2. InfiniteScrollAdapter

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<Map<String, Object>> data = fetchPropertyData("Leeds");
        if(data == null) {
            InfiniteScrollAdapter.addMoreComponents(hi.getContentPane(), new Component[0], false);
            return;
        }
        MultiButton[] cmps = new MultiButton[data.size()];
        for(int iter = 0 ; iter < data.size() ; iter++) {
            Map<String, Object> 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<Map<String, Object>> 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<Map<String, Object>>)response.get("listings");
    } catch(Exception err) {
        Log.e(err);
        return null;
    }
}

Methods

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.
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.
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.
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.
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.
public void continueFetching()If we previously added components with false for are there more this method can continue the process of fetching.
public int getComponentLimit()Deprecated 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.
public void setComponentLimit(int componentLimit)Deprecated 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.
public InfiniteProgress getInfiniteProgress()Lets us manipulate the infinite progress object e.g. set the animation image etc.

Inherited methods

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 Container
the container to bind, it MUST be empty and must be scrollable on the Y axis
fetchMore Runnable
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 Container
the container to bind, it MUST be empty and must be scrollable on the Y axis
fetchMore Runnable
a callback that will be invoked on the EDT to fetch more data (do not block this method)
fetchOnCreate boolean
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
container to add the components to
components Component[]
the components to add
areThereMore boolean
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 Container
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 Component[]
the components to add
areThereMore boolean
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()
Deprecated. this feature has some inherent problems and doesn’t work as expected
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

setComponentLimit

public void setComponentLimit(int componentLimit)
Deprecated. this feature has some inherent problems and doesn’t work as expected
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 int
the componentLimit to set

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