【问题标题】:How can I have the first element of a listview always visible when I scroll the listview?当我滚动列表视图时,如何让列表视图的第一个元素始终可见?
【发布时间】:2019-05-24 10:19:12
【问题描述】:

在我的代码中,我有一个包含 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
            }
       }
    }
}

【问题讨论】:

    标签: qt listview qml


    【解决方案1】:

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

    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
                }
           }
        }
    }
    

    【讨论】:

    • 还有ListView.PullBackHeader,以防您想要一个在向前滚动时隐藏而在向后滚动时可见的标题。
    猜你喜欢
    • 2016-12-08
    • 2013-01-21
    • 2017-05-24
    • 1970-01-01
    • 1970-01-01
    • 2017-09-15
    • 2022-06-13
    • 2015-10-21
    • 1970-01-01
    相关资源
    最近更新 更多