Fork us on GitHub

Till The End Of The Form

We've had pull to refresh for quite some time which is a real...

Till The End Of The Form

We've had pull to refresh for quite some time which is a really nice feature useful for pulling new updates. We also always had infinite lists using a smart list model approach, however up until now we didn't have a standard implementation of an infinite container with arbitrary components.

In some of the newer web UI's such as Tumblr and Twitter the data is fetched dynamically when you reach a fixed location in the form, this is a simpler approach than the one demonstrated by the list model but in some regards its more practical. A user can't just start jumping around and fetching the entire list, this works better with most REST API's and is pretty powerful on its own.

My initial thought was to create a Container subclass or even add support for this into Container itself, but eventually I came to the conclusion that this is really unnecessary and we can accomplish something like this without modifying the internal code. For this purpose we created the InfiniteScrollAdapter which is a really simple class that binds to a container and gives you the ability to add components to it. The API is remarkably simple you just invoke the static method InfiniteScrollAdapter.createInfiniteScroll() with an empty container then wait for it to invoke the runnable you submit to it.
The runnable will be invoked on the EDT so be sure not to block it (unless you use an AndWait or invokeAndBlock method), in it you can fetch data and once you are done add any set of components you like using the addMoreComponents() method. Notice that you shouldn't just add/remove components on your own since this will mess up the container.

Here is a simple example that adds buttons and sleeps to simulated slow network activity:

Notice: This post was automatically converted using a script from an older blogging system. Some elements might not have come out as intended.... If that is the case please let us know via the comments section below.

Share this Post:

Posted by Shai Almog

Shai is the co-founder of Codename One. He's been a professional programmer for over 25 years. During that time he has worked with dozens of companies including Sun Microsystems.
For more follow Shai on Twitter & github.