Nativescript以编程方式滚动到ScrollView的底部

Nativescript以编程方式滚动到ScrollView的底部,nativescript,angular2-nativescript,Nativescript,Angular2 Nativescript,在我的Nativescript Angular应用程序中,我在ScrollView中有一个TextView,定义如下: <ScrollView orientation="vertical" height="70%" width="100%" style="margin-bottom: 1%; background-color:white" (loaded)="onScrollerLoaded($event)"> <TextView id="termin

在我的Nativescript Angular应用程序中,我在ScrollView中有一个TextView,定义如下:

<ScrollView orientation="vertical" height="70%" width="100%" style="margin-bottom: 1%; background-color:white" (loaded)="onScrollerLoaded($event)">
    <TextView 
        id="terminal" [text]="terminalText" editable="false"
        style="color: whitesmoke; background-color: black; font-size: 8%; font-family: monospace" height="100%"     
        (tap)="onTap($event)" (loaded)="onTerminalLoaded($event)">
    </TextView>
</ScrollView>
我已尝试实现以下代码,这些代码将在ScrollView加载后执行:

private scrollIntervalId;
onScrollerLoaded(data: EventData){
    if(!this.scrollIntervalId){
        this.scrollIntervalId = setInterval(()=>{
            this.ngZone.run(() =>
                (data.object as any).scrollToVerticalOffset((data.object as any).scrollableHeight, false)
            )
        }, 10);
    }
}
(此尝试基于给出的解释。)


我只在安卓设备上尝试过,因为我没有访问苹果设备的权限。

您正在将
TextView
设置为固定高度100%,这将与
ScrollView
相同,这就是为什么
scrollableHeight
始终为0的原因。您应该使用
minHeight=“100%”

然后,当您将文本追加到终端文本
this.terminalText+=appendStr
时,可以通过编程方式滚动到末尾

像这样

public appendToTerminal(appendStr){
    this.terminalText += appendStr;
    setTimeout(()=>{
        scrollView.scrollToVerticalOffset(scrollView.scrollableHeight, false);

    },150);
}
这将附加文本,然后获取scrollableHeight,然后滚动到它


以下是工作区演示:

以下函数只能在Angular ngDoCheck()或ngAfterContentChecked()生命周期中使用:

//请参见https://angular.io/guide/lifecycle-hooks
函数goDownScrollView(scrollView:object,animate:boolean=true):布尔{
让neScrollView:ScrollView=getNativeElement(ScrollView),
scrollableHeight:number=neScrollView.scrollableHeight;
log(“neScrollView:,neScrollView”);
log(“neScrollView scrollableHeight:,scrollableHeight”);
如果(可滚动高度>0){
neScrollView.scrollToVerticalOffset(可滚动高度,动画);
返回true;
}否则{
返回false;
}
}
始终获取本机元素的帮助器:

函数getNativeElement(对象:对象):对象{ return(object.hasOwnProperty(“nativeElement”)?object['nativeElement']:object; } 在生命周期的第一个阶段,可滚动高度可能为零(例如,如果您通过HTTP请求向ScrollView添加元素)。这就是为什么您必须在滚动前使用new测试当前内容:

//请参见https://angular.io/api/core/ViewChild
@ViewChild(“内容”)私有页面内容:ElementRef;
公共内容:客体;
private\u previousContent:对象;
...
ngAfterContentChecked():void{
if(this.currentContent!=this.\u previousContent){
让isScrollDown:boolean=godownCrollView(此.\u页面内容);
如果(isScrollDown){
this.\u previousContent=this.currentContent;
}
}
}

在scrollview上加载它是否工作?@bhavinjalodara它工作了尝试过你的方法,仍然不工作。我可能已经找到了原因-我记录了scrollview和它的scrollableHeight的值,虽然scrollview很好,但scrollableHeight始终设置为0,即使我可以手动向下滚动页面。对此有何评论?我尝试将
TextView
嵌入到
StackLayout
中,但也没有成功。你能创建游乐场演示吗。然后我可以看一看。游乐场链接:如果有人仍然将0作为scrollableHeight。我不确定原因,但如果没有setTimeout,它将不起作用。在setTimeout cb函数中处理可滚动的内容解决这就是问题所在。您也可以尝试在NgZone中运行此功能。
public appendToTerminal(appendStr){
    this.terminalText += appendStr;
    setTimeout(()=>{
        scrollView.scrollToVerticalOffset(scrollView.scrollableHeight, false);

    },150);
}