6

I have a listview that starts off with 50 items, and I want to add more as the user scrolls through the list, but before they reach the end of the list. I've been using the atEnd property to know when I'm at the end of the list, and adding more items at that point, but I'd rather start adding new items when I'm 50% - 75% of the way through the list, so that the user is less likely to be left waiting for more data

4

1 回答 1

7

您可以使用onFirstVisibleItemChanged信号找出您在列表视图中的距离。使用 firstvisibleitem 和数据模型的当前大小来获取您在列表中的当前位置。如果该位置在您设置的边界内(在我的示例代码中为 50% 和 75%),则运行代码以向数据模型添加更多项目。

不要设置确切的数字,因为信号可能无法准确捕获列表中 50% 的路径,并且您还应该检查何时到达列表末尾作为故障保险,以防用户飞过去您设置的范围。

ListView {
dataModel: listDataModel
objectName: "listView"
attachedObjects: [

    ListScrollStateHandler {
        property int position
        id: scrollStateHandler
        onScrollingChanged: {

            if (atEnd) {
                //code to run if you hit the end of the list
            }
        }
        onFirstVisibleItemChanged: {
            if (position < firstVisibleItem) {
                console.log("Scrolling Down")
                var size = listDataModel.size();
                var percent = firstVisibleItem / size * 100;
                console.log("Percent: " + percent + "%");
                if (50 < percent < 75) {
                    //do stuff
                }
            } else if (position > firstVisibleItem) {
                console.log("Scrolling Up");

            }
            position = parseInt(firstVisibleItem);
        }
    }
]}
于 2013-08-13T18:49:44.683 回答