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
How can I add more items to a ListView before reaching the end of the list on a BB10 app?
Asked Answered
You can use the onFirstVisibleItemChanged signal to find out how far through the listview you are. Use the firstvisibleitem and the current size of the data model to get your current position in the list. If that position is within the boundaries you set (50% and 75% in my example code) then run code to add more items to the data model.
Don't set an exact number, as the signal may not catch exactly 50% of the way through the list, and you should also still keep a check for when you hit the end of the list as a failsafe in case the user flys past the range you set.
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);
}
}
]}
Great answer thanks! I would never have thought of this by myself –
Mullah
© 2022 - 2024 — McMap. All rights reserved.