【发布时间】:2021-05-08 07:25:09
【问题描述】:
我在 NativeScript 中使用 ScrollView 作为 Chat-Overview。
到目前为止,我在 Scrollview 中使用了这段代码:
<ScrollView #scrollLayout style="height: 80%;margin-top: -200px">
<StackLayout class="chat-body">
<StackLayout *ngFor="let message of messages" orientation="vertical">
<Label *ngIf="message.sender == me" horizontalAlignment="right" class="chat-message-me" [text]="message.msg"></Label>
<Label *ngIf="message.sender == me" horizontalAlignment="right" class="chat-timestamp" [text]="message.time"></Label>
<Label *ngIf="message.sender != me" horizontalAlignment="left" class="chat-message-you" [text]="message.msg"></Label>
<Label *ngIf="message.sender != me" horizontalAlignment="left" class="chat-timestamp" [text]="message.time"></Label>
</StackLayout>
</StackLayout>
</ScrollView>
它起作用了,但是当我向消息数组添加一个新元素时,视图会将完整的数组加载到已经显示的数组之上。
消息数组:
聊天视图:
所以我的解决方案是使用 ListView,如下所示:
<ScrollView #scrollLayout style="height: 80%;margin-top: -200px">
<GridLayout class="chat-body">
<ListView [items]="messages" orientation="vertical">
<ng-template let-item="message">
<Label *ngIf="message.sender == me" horizontalAlignment="right" class="chat-message-me" [text]="message.msg"></Label>
<Label *ngIf="message.sender == me" horizontalAlignment="right" class="chat-timestamp" [text]="message.time"></Label>
<Label *ngIf="message.sender != me" horizontalAlignment="left" class="chat-message-you" [text]="message.msg"></Label>
<Label *ngIf="message.sender != me" horizontalAlignment="left" class="chat-timestamp" [text]="message.time"></Label>
</ng-template>
</ListView>
</GridLayout>
</ScrollView>
但我得到这个错误:
System.err: An uncaught Exception occurred on "main" thread.
System.err: Calling js method getView failed
System.err: Error: No suitable views found in list template! Nesting level: 0
System.err:
【问题讨论】:
标签: listview nativescript scrollview