【问题标题】:How to restart a Load on demand in Nativescript?如何在 Nativescript 中重新启动按需加载?
【发布时间】:2020-01-26 05:36:12
【问题描述】:

我有一个 Radlistview,其中数据根据用户在日历中选择的日期而变化。

我正在使用loadOnDemandMode = "Manual",它可以正常工作,直到当前查询用尽。发生这种情况时,我使用listView.notifyLoadOnDemandFinished (true),但如果用户尝试选择另一天,我将无法再次使用“按需加载”。

我再次尝试实例化 ObservableArray,但它不起作用。如果有人知道如何重新启动按需加载,我将不胜感激

我按照THIS 创建代码,我尝试了THIS,但它对我不起作用。我的代码是这样的

<RadListView [items]="dataItems" 
loadOnDemandMode="Manual" 
(loadMoreDataRequested)="onLoadMoreItemsRequested($event)">
export class ListViewFixedSizeManualComponent implements OnInit {
    private _dataItems: ObservableArray<DataItem>;
    private _sourceDataItems: ObservableArray<DataItem>;
    private layout: ListViewLinearLayout;

    constructor(private _changeDetectionRef: ChangeDetectorRef) {
    }

    ngOnInit() {
        this.layout = new ListViewLinearLayout();
        this.layout.scrollDirection = ListViewScrollDirection.Vertical;
        this.initDataItems();
        this._changeDetectionRef.detectChanges();
        this._dataItems = new ObservableArray<DataItem>();
        this.addMoreItemsFromSource(6);
    }

    public get dataItems(): ObservableArray<DataItem> {
        return this._dataItems;
    }


    public addMoreItemsFromSource(chunkSize: number) {
        let newItems = this._sourceDataItems.splice(0, chunkSize);
        this.dataItems.push(newItems);
    }

    public onLoadMoreItemsRequested(args: LoadOnDemandListViewEventData) {
        const that = new WeakRef(this);
        const listView: RadListView = args.object;
        if (this._sourceDataItems.length > 0) {
            setTimeout(function () {
                that.get().addMoreItemsFromSource(2);
                listView.notifyLoadOnDemandFinished();
            }, 1500);
        } else {
            args.returnValue = false;
            listView.notifyLoadOnDemandFinished(true);
        }
    }

    private initDataItems() {
        this._sourceDataItems = new ObservableArray<DataItem>();
        for (let i = 0; i < posts.names.length; i++) {
            if (androidApplication) {
                this._sourceDataItems.push(new DataItem(i, posts.names[i], "This is item description", posts.titles[i], posts.text[i], "res://" + posts.images[i].toLowerCase()));
            }
            else {
                this._sourceDataItems.push(new DataItem(i, posts.names[i], "This is item description", posts.titles[i], posts.text[i], "res://" + posts.images[i]));
            }
        }
    }
}

【问题讨论】:

  • notifyLoadOnDemandFinished(true) 通过将loadOnDemandMode 设置为None 来禁用按需加载。当您更改日期时,您可以将loadOnDemandMode 设置回Manual,这样就可以了。如果您仍有问题,请分享一个 Playground 示例,以便重现问题。

标签: angular mobile nativescript angular2-nativescript radlistview


【解决方案1】:

我解决了从listView.notifyLoadOnDemandFinished()中删除true选项并添加选项ListViewLoadOnDemandMode.None的问题,所以当我更改日期时,只需再次添加ListViewLoadOnDemandMode.Manual

      if (this.getHistoricData.length > 0) {
        setTimeout(function () {
          that.get().addMoreItemsFromSource(10);
          listView.notifyLoadOnDemandFinished();
        }, 1500);
      } else {
        list.listView.loadOnDemandMode = ListViewLoadOnDemandMode.None;
        listView.notifyLoadOnDemandFinished();

参考:nativescript-ui-samples-angular

【讨论】:

    猜你喜欢
    • 2019-07-31
    • 1970-01-01
    • 2017-04-12
    • 2020-01-01
    • 2021-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多