Class InfiniteContainer

All Implemented Interfaces:
Animation, Editable, StyleListener, Iterable<Component>

public abstract class InfiniteContainer extends Container

This abstract Container can scroll indefinitely (or at least until we run out of data). This class uses the com.codename1.components.InfiniteScrollAdapter to bring more data and the pull to refresh feature to refresh current displayed data.

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

public void showForm() {
    Form hi = new Form("InfiniteContainer", new BorderLayout());

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

    InfiniteContainer ic = new InfiniteContainer() {
@Override
        public Component[] fetchComponents(int index, int amount) {
            java.util.List> data = fetchPropertyData("Leeds");
            MultiButton[] cmps = new MultiButton[data.size()];
            for(int iter = 0 ; iter  currentListing = data.get(iter);
                if(currentListing == null) {
                    return null;
                }
                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));
            }
            return cmps;
        }
    };
    hi.add(BorderLayout.CENTER, ic);
    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;
    }
}
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;
    }
}
  • Constructor Details

    • InfiniteContainer

      public InfiniteContainer()
      Creates the InfiniteContainer. The InfiniteContainer is created with BoxLayout Y layout.
    • InfiniteContainer

      public InfiniteContainer(int amount)

      Creates the InfiniteContainer. The InfiniteContainer is created with BoxLayout Y layout.

      Parameters
      • amount: the number of items to fetch in each call to fetchComponents
  • Method Details

    • initComponent

      protected void initComponent()
      Description copied from class: Component
      Allows subclasses to bind functionality that relies on fully initialized and "ready for action" component state
      Overrides:
      initComponent in class Component
    • refresh

      public void refresh()
      This refreshes the UI in a similar way to the "pull to refresh" functionality
    • continueFetching

      public void continueFetching()
      If we previously added returned null when fetching components 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
    • isAsync

      protected boolean isAsync()

      Indicates whether int) should be invoked asynchronously off the EDT

      Returns

      this is set to true for compatibility with older versions of the infinite container

    • fetchComponents

      public abstract Component[] fetchComponents(int index, int amount)

      This is an abstract method that should be implemented by the sub classes to fetch the data.

      When #isAsync() is overriden to return true this method is invoked on a background thread. Notice that in this case the method might cause EDT violations warnings, since the subclasses will need to create the Components off the EDT. While these are EDT violations they probably won't cause problems for more code.

      Sub classes should preform their networking/data fetching here.

      Parameters
      • index: the index from which to bring data

      • amount: the size of components to bring

      Returns
      Returns:
      Components array of the returned data, size of the array can be the size of the amount or smaller, if there is no more data to fetch the method should return null.
    • 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 container