[英]Center QML ListView highlighted item
In the picture Test
, Test 1
and Test 2
are in the ListView
. 在图片
Test
, Test 1
和Test 2
在ListView
。 In this case Test
element is highlighted. 在这种情况下,
Test
元素会突出显示。 How can I modify view behavior to ensure that current (highlighted) item stays always in the middle of the list? 如何修改视图行为以确保当前(突出显示的)项始终位于列表的中间? What I want to achieve is represented in the following picture:
我希望实现的目标如下图所示:
You just need highlightRangeMode
with preferredHighlightBegin
and preferredHighlightEnd
. 你只需要使用
preferredHighlightBegin
和preferredHighlightEnd
highlightRangeMode
。 From the documentation: 从文档:
These properties affect the position of the current item when the list is scrolled.
滚动列表时,这些属性会影响当前项的位置。 For example, if the currently selected item should stay in the middle of the list when the view is scrolled, set the preferredHighlightBegin and preferredHighlightEnd values to the top and bottom coordinates of where the middle item would be .
例如,如果当滚动视图时当前所选项目应保留在列表中间,请将preferredHighlightBegin和preferredHighlightEnd值设置为中间项目所在位置的顶部和底部坐标 。 If the currentItem is changed programmatically, the list will automatically scroll so that the current item is in the middle of the view.
如果以编程方式更改currentItem,列表将自动滚动,以便当前项位于视图的中间。 Furthermore, the behavior of the current item index will occur whether or not a highlight exists.
此外,无论是否存在突出显示,都将发生当前项索引的行为。
Here is a full example of an horizontal ListView
with the current item positioned at the center. 以下是水平
ListView
的完整示例,当前项目位于中心。
import QtQuick 2.4
import QtQuick.Window 2.2
Window {
width: 300
height: 150
visible: true
ListView {
anchors.fill: parent
spacing: 5
model: 20
delegate:
Rectangle {
width: 30
color: ListView.view.currentIndex === index ? "red" : "steelblue"
height: ListView.view.height
Text {
anchors.centerIn: parent
text: index
font.pixelSize: 20
}
}
orientation: Qt.Horizontal
preferredHighlightBegin: 150 - 15
preferredHighlightEnd: 150 + 15
highlightRangeMode: ListView.StrictlyEnforceRange
}
}
您可以查看ListView的positionViewAtIndex
方法,看看是否有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.