Reputation: 2355
My app features an InfiniteScrollAdapter
because it looks good and is advised in the doc. However, I don't want the "list" to loop back from the beginning when it reaches the end (otherwise the user can think there are many entries although they are just duplicated).
Is the solution to set the value of InfiniteScrollAdapter.setComponentLimit()
to the number of entries ? Or should I use a container in Y BoxLayout
(will the data still be fetched as lazily as with the InfiniteScrollAdapter
?)
Upvotes: 2
Views: 159
Reputation: 7483
My method is to keep track of the lastId
and number of records returned.
Let's take for instance I have a database with 105 records and I want to fetch them 10 at a time:
I will make sure the query pulls the records in ascending or descending orders.
Let's say it's in ascending order, my initial lastId
will be 0 and once records are returned the lastId
will change to the last record id (In this case 10). Then I pass that as a parameter to pull next records where id is greater than the lastId
(10), that's 11 upward.
Coding idea:
InfiniteScrollAdapter.createInfiniteScroll(scroller, new Runnable() {
private int pageNumber = 0;
private int lastId = 0;
@Override
public void run() {
try {
//Go fetch records with lastId as parameter
if (records != null) {
//create components array from records and add them as required
lastId = id value of the last record returned
pageNumber++; //increment this to indicate this is not first run.
//Here we set the boolean value to see if records returned is at least 10. If it is, it means we have more records to pull from the database, else it's the last batch (In our case, record 91 to 95 is less than 10 and it's the last batch)
InfiniteScrollAdapter.addMoreComponents(myContainer, componentsToAdd, records.size() >= 10);
} else {
//show an error message if pageNumber == 1 (First run), otherwise remove the infinite scroll adapter
myContainer.getComponentAt(myContainer.getComponentCount() - 1).remove();
}
} catch (Exception ex) {
//show an error message if pageNumber == 1 (First run), otherwise remove the infinite scroll adapter
myContainer.getComponentAt(myContainer.getComponentCount() - 1).remove();
}
}
}, true);
Upvotes: 3