Nativescript + Angular ScrollView滚动到最终侦听器

时间:2019-02-01 15:46:54

标签: angular nativescript angular2-nativescript nativescript-angular

每当ScrollView滚动到最底端时,是否有任何回调可用于识别? 在Nativescript文档中,仅提到了scroll事件,但它仅提供有关当前滚动的X或Y线的信息。由于我的ScrollView身高是动态的,因此不确定此事件是否对我有帮助。

2 个答案:

答案 0 :(得分:5)

您仍然可以收听scroll事件,并查看当前verticalOffset是否等于location中最后一个组件的ScrollView

类似

 // Attached to layoutChange event of your container element inside scroll view
 onLayoutChanged(event: EventData) {
    const containerLyt = <StackLayout>event.object,
        length = containerLyt.getChildrenCount(),
        lastItem = containerLyt.getChildAt(length - 1);

    this.lastItemY = lastItem.getLocationRelativeTo(containerLyt).y;
}

onScroll(event: EventData) {
    const scrollView = <ScrollView>event.object,
        verticalOffset = scrollView.getActualSize().height + scrollView.verticalOffset;

    if (verticalOffset >= this.lastItemY) {
        console.log("Reached end of scroll");
    }
}

Playground Example

答案 1 :(得分:0)

通过比较scrollView.scrollableHeight和ScrollEventData.scrollY来简单地检测ScrollView的结尾,例如:

let scrollv : ScrollView = <ScrollView>this.scrollView.nativeElement;
scrollv.on(ScrollView.scrollEvent, function (args: ScrollEventData) { 
        if(scrollv.scrollableHeight === args.scrollY){
            console.log("load more items here !!! ");
        } 
 });