我可以强制 Codename One 的 InfiniteScrollAdapter 不循环吗

Can I force Codename One's InfiniteScrollAdapter not to loop

我的应用有一个 InfiniteScrollAdapter,因为它看起来不错并且在 doc 中被推荐。但是,我不希望 "list" 在到达结尾时从头开始循环(否则用户可以认为有很多条目,尽管它们只是重复的)。

解决方案是将InfiniteScrollAdapter.setComponentLimit()的值设置为条目数吗?或者我应该在 Y BoxLayout 中使用一个容器(数据仍然会像 InfiniteScrollAdapter 那样延迟获取吗?)

我的方法是跟踪 lastId 和返回的记录数。

假设我有一个包含 105 条记录的数据库,我想一次获取 10 条记录:

我会确保查询按升序或降序提取记录。

假设它按升序排列,我的初始 lastId 将为 0,一旦返回记录,lastId 将更改为最后一个记录 ID(在本例中为 10)。然后我将其作为参数传递以提取 id 大于 lastId (10) 的下一条记录,即 11 向上。

编码思路:

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