Qt 滚动listview时,如何使listview的第一个元素始终可见?

Qt 滚动listview时,如何使listview的第一个元素始终可见?,qt,listview,qml,Qt,Listview,Qml,在我的代码中,我有一个包含16个元素的ListView,我希望当我滚动ListView时,第一个元素必须始终可见。我怎么能做到 ApplicationWindow { id: window visible: true width: 640 height: 480 ListView { id:listview anchors.fill: parent model: 16 verticalLa

在我的代码中,我有一个包含16个元素的ListView,我希望当我滚动ListView时,第一个元素必须始终可见。我怎么能做到

ApplicationWindow {
    id: window
    visible: true
    width: 640
    height: 480


    ListView {
        id:listview
        anchors.fill: parent
        model: 16
        verticalLayoutDirection: ListView.BottomToTop
        delegate: Rectangle {
            height: listview.height/5
            width: listview.width
            color: Qt.rgba(Math.random(),Math.random(),Math.random(),1)
            Text {
               text: index
               anchors.fill: parent
               horizontalAlignment: Text.AlignHCenter
               verticalAlignment: Text.AlignVCenter
               color: "white"
               font.pixelSize: 35
            }
       }
    }
}

您可以使用标题。您的代码将如下所示:

ApplicationWindow {
    id: window
    visible: true
    width: 640
    height: 480


    ListView {
        id:listview
        anchors.fill: parent
        model: 15
        verticalLayoutDirection: ListView.BottomToTop

        headerPositioning: ListView.OverlayHeader

        header: Rectangle {
            height: listview.height/5
            width: listview.width
            color: Qt.rgba(Math.random(),Math.random(),Math.random(),1)
            z:2
            Text {
               text: "0"
               anchors.fill: parent
               horizontalAlignment: Text.AlignHCenter
               verticalAlignment: Text.AlignVCenter
               color: "white"
               font.pixelSize: 35
            }
        }

        delegate: Rectangle {
            height: listview.height/5
            width: listview.width
            color: Qt.rgba(Math.random(),Math.random(),Math.random(),1)
            Text {
               text: index+1
               anchors.fill: parent
               horizontalAlignment: Text.AlignHCenter
               verticalAlignment: Text.AlignVCenter
               color: "white"
               font.pixelSize: 35
            }
       }
    }
}
还有一种情况是,你想要一个标题,它在向前滚动时隐藏,在向后滚动时可见。