首页 文章

QML ListView,滚动时如何使列表元素不与 Headers 重叠

提问于
浏览
3

我有ListView启用标头委托 . 我有一个 Headers 定位属性设置为“OverlayHeader” . 滚动元素时, Headers 将保持不变 . 但是,元素将与 Headers 重叠 . 有没有办法防止这种情况发生 .

与 Headers 重叠的列表元素的示例 .

import QtQuick 2.5

Rectangle {
    width: 360; height: 600

  ListView {

    width: 350; height: 200
    anchors.centerIn: parent
    id: myList
    model: myModel
    highlight: highlightBar
    clip: true

    snapMode: ListView.SnapToItem

    headerPositioning: ListView.OverlayHeader

    header: Rectangle {
      id: headerItem
      width: myList.width
      height:50

      color: "blue"

      Text {
        text: "HEADER"
        color: "red"
      }
    }

    delegate: Item {
      id: delegateItem
      width: 400; height: 20
      clip: true
      Text { text: name
      }

      MouseArea {
        id: mArea
        anchors.fill: parent
        onClicked: { myList.currentIndex = index; }
      }
    }

  }

  Component {
    id: highlightBar
    Rectangle {
      width: parent.width; height: 20
      color: "#FFFF88"
    }
  }

  ListModel {
      id: myModel
  }

  /* Fill the model with default values on startup */
  Component.onCompleted: {
      for(var i = 0; i < 100; i++) {
          myModel.append({ name: "Big Animal : " + i});
      }
  }
}

1 回答

  • 4

    header's default z value is 1,因此您可以将其设置为更高的值,以确保它超过委托:

    import QtQuick 2.5
    
    Rectangle {
        width: 360
        height: 600
    
        ListView {
    
            width: 350
            height: 200
            anchors.centerIn: parent
            id: myList
            model: myModel
            highlight: highlightBar
            clip: true
    
            snapMode: ListView.SnapToItem
    
            headerPositioning: ListView.OverlayHeader
    
            header: Rectangle {
                id: headerItem
                width: myList.width
                height: 50
                z: 2
    
                color: "blue"
    
                Text {
                    text: "HEADER"
                    color: "red"
                }
            }
    
            delegate: Item {
                id: delegateItem
                width: 400
                height: 20
                Text {
                    text: name
                }
    
                MouseArea {
                    id: mArea
                    anchors.fill: parent
                    onClicked: {
                        myList.currentIndex = index
                    }
                }
            }
        }
    
        Component {
            id: highlightBar
            Rectangle {
                width: parent.width
                height: 20
                color: "#FFFF88"
            }
        }
    
        ListModel {
            id: myModel
        }
    
        /* Fill the model with default values on startup */
        Component.onCompleted: {
            for (var i = 0; i < 100; i++) {
                myModel.append({
                    name: "Big Animal : " + i
                })
            }
        }
    }
    

    请注意clipping view delegates is bad for performance .

相关问题