我使用QML ScrollView
和ListView
。 ListView
由headerDelegate
和delegate
组成:
ScrollView {
id: scrollView
Layout.fillWidth: true
Layout.fillHeight: true
width: parent.width
height: parent.height
ListView{
id: listView
width: parent.width
height: parent.height
spacing: 10
header: headerDelegate
anchors.fill: parent
property bool isFolded: false
model: MyModel
delegate: mainDelegate
}
}
每次ListView
滚动到顶部时,会显示第一个mainDelegate
而不是隐藏的headerDelegate
。headerDelegate
如何强制滚动显示A=array();
A[0]=>name = "John";
A[0]=>lastname = "Blabla";
A[0]=>genre = "Male";
A[1]=>name = "Cheryl";
A[1]=>lastname = "Blabla";
A[1]=>genre = "Female";
?
答案 0 :(得分:1)
此处的问题是,ScrollView
期望孩子的contentY
相等0
,但ListView
将模型的第一项定位在contentY = 0
并放置标题之前的项目。如果ListView.header
为50px
高,则其位于ListView.contentY = -50
。
对我有用的解决方案是发出ListView.contentYChanged()
信号。它会导致ScrollView
更新。如果这解决了您的问题,请告诉我。
ScrollView {
id: scrollView
Layout.fillWidth: true
Layout.fillHeight: true
width: parent.width
height: parent.height
ListView{
id: listView
width: parent.width
height: parent.height
spacing: 10
header: headerDelegate
anchors.fill: parent
property bool isFolded: false
model: listModel
delegate: mainDelegate
onContentYChanged: console.log(contentY)
Component.onCompleted: {
contentYChanged()
}
}
}
答案 1 :(得分:1)
我手动更改了listView.contentY。在最后加载标题后(我得到了事件)我设置了listView.contentY = 0 - headerDelegateView.height并且它可以工作。 @Filip Hazubski感谢您提供良好的解决方案!
示例代码
Rectangle
{
id: headerDelegateView
WebEngineView{
...
onLoadingChanged: {
if (loadRequest.status === WebEngineView.LoadSucceededStatus) {
... // here I calculate web page height by java script and set to headerDelegateView.height
listView.contentY = 0 - headerDelegateView.height
}
ScrollView {
...
ListView{
id: listView
...
}
}
}