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

时间:2017-03-02 06:13:16

标签: codenameone

我的应用程序具有InfiniteScrollAdapter,因为它看起来不错,并在doc中得到建议。但是,我不希望“列表”在它到达结尾时从头开始循环(否则用户可以认为有很多条目,尽管它们只是重复)。

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

1 个答案:

答案 0 :(得分:3)

我的方法是跟踪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);