我需要做什么: 我需要创建一个聊天窗口用一个ListView
在 QML 中存储聊天消息。我设置listView.positionViewAtEnd()
以便跟踪最后的消息。我禁用positionViewAtEnd
当我向上滚动时,我可以阅读过去的消息,而不必在每次收到新消息时跳到最后。
问题:每次向上滚动后我收到一条新消息,它会跳转在列表的开头。为了解决这个问题,我设法存储contentY
列表并每次重置它onCountChanged
处理程序被调用(见下面的代码):
ListView {
id: messagesList
model: contact? contact.messages: []
delegate: delegate
anchors.fill: parent
anchors.bottomMargin: 20
height: parent.height
anchors.margins: 10
property int currentContentY
onMovementEnded: {
currentContentY = contentY
}
onCountChanged: {
contentY = currentContentY
}
onContentYChanged: {
console.log(".....contentY: " + contentY)
}
}
问题是即使我设置了最后一个contentY
在更改模型之前,我有列表仍然有点跳跃 (几个像素,不在结尾或开头)和它并不总是跳。当我转到列表顶部并打印contentY
我得到负值。理论上,contentY
在列表的开头应该是0
.
有人能告诉我出了什么问题吗?或者也许建议另一种解决方案来创建我的消息列表?
比你提前! :)
一种可能的解决方案是将 ListView 插入 Flickable 并禁用交互的ListView 的标志
Flickable {
id: fparent
anchors.fill: parent
anchors.bottomMargin: 20
anchors.margins: 10
interactive: true
clip: true
flickableDirection: Flickable.VerticalFlick
contentHeight: messagesList.height
ListView {
id: messagesList
width: parent.width
height: childrenRect.height
clip: true
model: contact? contact.messages: []
delegate: delegate
interactive: false
onCountChanged: {
fparents.returnToBounds();
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)