listview - 使用 ListView 和 nativescript-vue 进行无限滚动
问题描述
我想知道是否可以使用 nativescript-vue 在 ListView 上实现无限滚动。
目前,我只看到@itemTap事件,但看不到其他事件与 ListView 和 nativescript-vue 一起使用来实现无限滚动。
<ListView class="list-view" v-for="(post, indexPost) in posts" @itemTap="loadMore">
<v-template>
<StackLayout class="list-element" orientation="vertical">
<post :post="post" />
</StackLayout>
</v-template>
</ListView>
我想听听 ListView 上的滚动。
使用 vue-native,我在 scroll-view 上实现了它:
<scroll-view :on-scroll="(event) => {loadMore(event)}" :scroll-event-throttle="400">
<post v-for="(post, indexPost) in posts" :post="post" />
</scroll-view>
...
loadMore: function (event) {
let paddingToBottom = 0;
paddingToBottom += event.nativeEvent.layoutMeasurement.height;
if (event.nativeEvent.contentOffset.y >= event.nativeEvent.contentSize.height - paddingToBottom) {
this.getData()
}
},
我想用 nativescript-vue 实现相同的...
解决方案
您可以使用 Nativescript 的 RadListView 代替常规的 ListView。这是docs和api的链接。
RadListView
有一个名为的属性loadOnDemand
,可以让您添加一种触发getData
和更新列表视图的方法。
有关此处loadOnDemand
功能的一些附加信息
代码应该与此类似:
<lv:RadListView id="ls" items="{{ dataItems }}" row="0" loadOnDemandMode="Manual" loadMoreDataRequested="{{onLoadMoreItemsRequested}}">
public addMoreItemsFromSource(chunkSize: number, listView: RadListView) {
let newItems = this._sourceDataItems.splice(0, chunkSize);
this.dataItems.push(newItems);
if (listView) {
// Call the optimized function for on-demand loading finished.
// (with 0 because the ObservableArray has already
// notified about the inserted items)
listView.notifyAppendItemsOnDemandFinished(0, this._sourceDataItems.length === 0);
}
}
public onLoadMoreItemsRequested(args: LoadOnDemandListViewEventData) {
const that = new WeakRef(this);
const listView: RadListView = args.object;
if (this._sourceDataItems.length > 0) {
setTimeout(function () {
that.get().addMoreItemsFromSource(20, listView);
}, 0);
args.returnValue = true;
} else {
args.returnValue = false;
listView.notifyAppendItemsOnDemandFinished(0, true);
}
}
推荐阅读
- java - Android MPchart在滚动后获取视图的Y-Position
- apache-spark - 我可以在 Pyspark 中对 pair_rdd 进行 groupByKey 调用吗?
- django - DRF:没有模型的序列化程序验证
- javascript - 使用节点 js 创建一个 css 文件
- javascript - Node JS 会话错误:不推荐使用 express-session
- mysql - SQL有没有办法选择作为给定值开头的所有值?
- face-recognition - 地标检测与人脸识别
- javascript - 如何返回对象名称JS?
- flutter - Flutter:如何在点击 ListTile 后使用 showDialog 提示用户输入,然后使用输入
- flutter - 行内的 FutureBuilder 颤动