ListView想要做到上下翻页的效果需要使用如下参数 positionViewAtIndex(int index,PositionMode mode)Positions the view such that the index is at the position specified by mode:
ListView.Beginning - position item at the top (or left for horizontal orientation) of the view.
ListView.Center - position item in the center of the view.
ListView.End - position item at bottom (or right for horizontal orientation) of the view.
ListView.Visible - if any part of the item is visible then take no action, otherwise bring the item into view.
ListView.Contain - ensure the entire item is visible. If the item is larger than the view the item is positioned at the top (or left for horizontal orientation) of the view.
ListView.SnapPosition - position the item at preferredHighlightBegin. This mode is only valid if highlightRangeMode is StrictlyEnforceRange or snapping is enabled via snapMode.
If positioning the view at index would cause empty space to be displayed at the beginning or end of the view, the view will be positioned at the boundary.It is not recommended to use contentX or contentY to position the view at a particular index. This is unreliable since removing items from the start of the list does not cause all other items to be repositioned, and because the actual start of the view can vary based on the size of the delegates. The correct way to bring an item into view is with positionViewAtIndex.Note: methods should only be called after the Component has completed. To position the view at startup, this method should be called by Component.onCompleted. For example, to position the view at the end: Component.onCompleted: positionViewAtIndex(count - 1, ListView.Beginning)这个methods是通过定位一个元素,将其放置在一个指定的位置。 不多说,直接上代码:下面贴上一段上下翻页的效果代码:在下面代码中创建上一页和下一页按钮propertyintnum:0propertyintmessageNumber:9Item{id:upAndDownPagewidth:ticketMessageImage.widthheight:ticketMessageImage.heightanchors{horizontalCenter:checkMessageBoxImage.horizontalCenter;bottom:checkMessageBoxImage.bottom;bottomMargin:15} Row{spacing:100anchors{horizontalCenter:upAndDownPage.horizontalCenter;verticalCenter:upAndDownPage.verticalCenter}//一页只放10条,也只能显示10条,这样就不会产生滑动,当选择下一页的时候,将数据再以10条为单位放入listmodel中,进行显示Item{id:upTurnPagewidth:upTurnPageText.widthheight:upTurnPageText.height//color:"#30FFFF"Text{id:upTurnPageTexttext:qsTr("上一页")font.pixelSize:30color:"white"}MouseArea{anchors.fill:upTurnPage//16onClicked:{num=num-messageNumber;if(num<=0){num=0;}recordList.positionViewAtIndex(recordList.count-recordList.count+num,ListView.Beginning);console.log(recordList.count-recordList.count+num)}}} Rectangle{id:downTurnPagewidth:downTurnPageText.widthheight:downTurnPageText.heightcolor:"#30FFFF"Text{id:downTurnPageTexttext:qsTr("下一页")color:"white"font.pixelSize:30}MouseArea{anchors.fill:downTurnPageonClicked:{//5为一个界面显示多少条信息,可用变量num=num+messageNumber;if(num>=recordList.count-messageNumber){num=recordList.count-messageNumber;}recordList.positionViewAtIndex(recordList.count-recordList.count+num,ListView.Beginning);console.log(recordList.count-recordList.count+num)}}}}}} 在下面的代码中创建ListView视图Item{id:checkHistoralRecordTitleItemwidth:ticketMessageImage.widthanchors{top:historicalRecordImage.bottom;topMargin:10;horizontalCenter:checkMessageBoxImage.horizontalCenter}height:35//color:"#33FFFF" Item{id:tradeTimeItemwidth:parent.width*0.25height:24Text{id:tradeTimeTextcolor:"yellow"text:"交易时间"font.pixelSize:24anchors{verticalCenter:tradeTimeItem.verticalCenter;horizontalCenter:tradeTimeItem.horizontalCenter}}} Item{id:terminalNumberItemwidth:parent.width*0.25height:24anchors{left:tradeTimeItem.right;verticalCenter:tradeTimeItem.verticalCenter}Text{id:terminalNumberItemTextcolor:"yellow"text:"终端编号"font.pixelSize:24anchors{verticalCenter:terminalNumberItem.verticalCenter;horizontalCenter:terminalNumberItem.horizontalCenter}} } Item{id:tradeTypeItemwidth:parent.width*0.25height:24anchors{left:terminalNumberItem.right;verticalCenter:tradeTimeItem.verticalCenter}Text{id:tradeTypeItemTextcolor:"yellow"text:"类型"font.pixelSize:24anchors{verticalCenter:tradeTypeItem.verticalCenter;horizontalCenter:tradeTypeItem.horizontalCenter}} } Item{id:tradeMoneyItemwidth:parent.width*0.25height:24anchors{left:tradeTypeItem.right;verticalCenter:tradeTimeItem.verticalCenter}Text{id:tradeMoneyItemTextcolor:"yellow"text:"金额"font.pixelSize:24anchors{verticalCenter:tradeMoneyItem.verticalCenter;horizontalCenter:tradeMoneyItem.horizontalCenter}} }} Item{id:historicalRecordItemwidth:historicalRecordImage.widthheight:300anchors{top:checkHistoralRecordTitleItem.bottom;topMargin:10;horizontalCenter:checkMessageBoxImage.horizontalCenter}Column{ListView{id:recordListwidth:historicalRecordItem.widthheight:historicalRecordItem.heightspacing:10clip:truemodel:tradeRecordListdelegate:HMBOMCheckViewHistorialRecordContextMode{}}}} 在本地创建ListModel用于测试ListModel{id:tradeRecordListListElement{tradeTimeRecord:"00/10/1308:00:59";tradeTerminalRecord:"XXXXXXXXXXXX";tradeTypeRecord:"XXXXXXXXXXXXXX";tradeMoneyRecord:"20元"}ListElement{tradeTimeRecord:"01/10/1308:00:59";tradeTerminalRecord:"XXXXXXXXXXXX";tradeTypeRecord:"XXXXXXXXXXXXXX";tradeMoneyRecord:"20元"}ListElement{tradeTimeRecord:"02/10/1308:00:59";tradeTerminalRecord:"XXXXXXXXXXXX";tradeTypeRecord:"XXXXXXXXXXXXXX";tradeMoneyRecord:"20元"}ListElement{tradeTimeRecord:"03/10/1308:00:59";tradeTerminalRecord:"XXXXXXXXXXXX";tradeTypeRecord:"XXXXXXXXXXXXXX";tradeMoneyRecord:"20元"}ListElement{tradeTimeRecord:"04/10/1308:00:59";tradeTerminalRecord:"XXXXXXXXXXXX";tradeTypeRecord:"XXXXXXXXXXXXXX";tradeMoneyRecord:"20元"}ListElement{tradeTimeRecord:"05/10/1308:00:59";tradeTerminalRecord:"XXXXXXXXXXXX";tradeTypeRecord:"XXXXXXXXXXXXXX";tradeMoneyRecord:"20元"}ListElement{tradeTimeRecord:"06/10/1308:00:59";tradeTerminalRecord:"XXXXXXXXXXXX";tradeTypeRecord:"XXXXXXXXXXXXXX";tradeMoneyRecord:"20元"}ListElement{tradeTimeRecord:"07/10/1308:00:59";tradeTerminalRecord:"XXXXXXXXXXXX";tradeTypeRecord:"XXXXXXXXXXXXXX";tradeMoneyRecord:"20元"}ListElement{tradeTimeRecord:"08/10/1308:00:59";tradeTerminalRecord:"XXXXXXXXXXXX";tradeTypeRecord:"XXXXXXXXXXXXXX";tradeMoneyRecord:"20元"}ListElement{tradeTimeRecord:"09/10/1308:00:59";tradeTerminalRecord:"XXXXXXXXXXXX";tradeTypeRecord:"XXXXXXXXXXXXXX";tradeMoneyRecord:"20元"}ListElement{tradeTimeRecord:"10/10/1308:00:59";tradeTerminalRecord:"XXXXXXXXXXXX";tradeTypeRecord:"XXXXXXXXXXXXXX";tradeMoneyRecord:"20元"}ListElement{tradeTimeRecord:"11/10/1308:00:59";tradeTerminalRecord:"XXXXXXXXXXXX";tradeTypeRecord:"XXXXXXXXXXXXXX";tradeMoneyRecord:"20元"}ListElement{tradeTimeRecord:"12/10/1308:00:59";tradeTerminalRecord:"XXXXXXXXXXXX";tradeTypeRecord:"XXXXXXXXXXXXXX";tradeMoneyRecord:"20元"}ListElement{tradeTimeRecord:"13/10/1308:00:59";tradeTerminalRecord:"XXXXXXXXXXXX";tradeTypeRecord:"XXXXXXXXXXXXXX";tradeMoneyRecord:"20元"}ListElement{tradeTimeRecord:"14/10/1308:00:59";tradeTerminalRecord:"XXXXXXXXXXXX";tradeTypeRecord:"XXXXXXXXXXXXXX";tradeMoneyRecord:"20元"}ListElement{tradeTimeRecord:"15/10/1308:00:59";tradeTerminalRecord:"XXXXXXXXXXXX";tradeTypeRecord:"XXXXXXXXXXXXXX";tradeMoneyRecord:"20元"}ListElement{tradeTimeRecord:"16/10/1308:00:59";tradeTerminalRecord:"XXXXXXXXXXXX";tradeTypeRecord:"XXXXXXXXXXXXXX";tradeMoneyRecord:"20元"}ListElement{tradeTimeRecord:"17/10/1308:00:59";tradeTerminalRecord:"XXXXXXXXXXXX";tradeTypeRecord:"XXXXXXXXXXXXXX";tradeMoneyRecord:"20元"}ListElement{tradeTimeRecord:"18/10/1308:00:59";tradeTerminalRecord:"XXXXXXXXXXXX";tradeTypeRecord:"XXXXXXXXXXXXXX";tradeMoneyRecord:"20元"}ListElement{tradeTimeRecord:"19/10/1308:00:59";tradeTerminalRecord:"XXXXXXXXXXXX";tradeTypeRecord:"XXXXXXXXXXXXXX";tradeMoneyRecord:"20元"}ListElement{tradeTimeRecord:"20/10/1308:00:59";tradeTerminalRecord:"XXXXXXXXXXXX";tradeTypeRecord:"XXXXXXXXXXXXXX";tradeMoneyRecord:"20元"}} 运行效果如下: