热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

ListView学习2--使用ListView做上下翻页

ListView想要做到上下翻页的效果需要使用如下参数positionViewAtIndex(intindex,PositionModemode)P
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是通过定位一个元素,将其放置在一个指定的位置。
不多说,直接上代码: 下面贴上一段上下翻页的效果代码: 在下面代码中创建上一页和下一页按钮 property int num :0     property int messageNumber : 9 Item{         id:upAndDownPage         width: ticketMessageImage.width         height: ticketMessageImage.height         anchors{horizontalCenter: checkMessageBoxImage.horizontalCenter;bottom: checkMessageBoxImage.bottom;bottomMargin: 15}
        Row{             spacing: 100             anchors{horizontalCenter: upAndDownPage.horizontalCenter;verticalCenter: upAndDownPage.verticalCenter} //一页只放10条,也只能显示10条,这样就不会产生滑动,当选择下一页的时候,将数据再以10条为单位放入listmodel中,进行显示             Item{                 id:upTurnPage                 width: upTurnPageText.width                 height:upTurnPageText.height //                color: "#30FFFF"                 Text {                     id: upTurnPageText                     text: qsTr("上一页")                     font.pixelSize: 30                     color: "white"                 }                 MouseArea{                     anchors.fill: upTurnPage                     //16                     onClicked:{                         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:downTurnPage                 width: downTurnPageText.width                 height:downTurnPageText.height                 color: "#30FFFF"                 Text {                     id: downTurnPageText                     text: qsTr("下一页")                     color: "white"                     font.pixelSize: 30                 }                 MouseArea{                     anchors.fill: downTurnPage                     onClicked:{                         //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:checkHistoralRecordTitleItem         width: ticketMessageImage.width         anchors{top:historicalRecordImage.bottom;topMargin:10; horizontalCenter: checkMessageBoxImage.horizontalCenter}         height:35 //        color:"#33FFFF"
        Item {             id: tradeTimeItem             width:parent.width*0.25             height:24             Text{                 id:tradeTimeText                 color: "yellow"                 text:"交易时间"                 font.pixelSize: 24                 anchors{verticalCenter: tradeTimeItem.verticalCenter;horizontalCenter: tradeTimeItem.horizontalCenter}             }         }
        Item {             id: terminalNumberItem             width:parent.width*0.25             height:24             anchors{left:tradeTimeItem.right;verticalCenter: tradeTimeItem.verticalCenter }             Text{                 id:terminalNumberItemText                 color: "yellow"                 text:"终端编号"                 font.pixelSize: 24                 anchors{verticalCenter: terminalNumberItem.verticalCenter;horizontalCenter: terminalNumberItem.horizontalCenter}             }
        }
        Item {             id: tradeTypeItem             width:parent.width*0.25             height:24             anchors{left:terminalNumberItem.right;verticalCenter: tradeTimeItem.verticalCenter }             Text{                 id:tradeTypeItemText                 color: "yellow"                 text:"类型"                 font.pixelSize: 24                 anchors{verticalCenter: tradeTypeItem.verticalCenter;horizontalCenter: tradeTypeItem.horizontalCenter}             }
        }
        Item {             id: tradeMoneyItem             width:parent.width*0.25             height:24             anchors{left:tradeTypeItem.right;verticalCenter: tradeTimeItem.verticalCenter }             Text{                 id:tradeMoneyItemText                 color: "yellow"                 text:"金额"                 font.pixelSize: 24                 anchors{verticalCenter: tradeMoneyItem.verticalCenter;horizontalCenter: tradeMoneyItem.horizontalCenter}             }
        }     }

    Item{         id:historicalRecordItem         width:historicalRecordImage.width         height:300         anchors{top:checkHistoralRecordTitleItem.bottom;topMargin:10; horizontalCenter: checkMessageBoxImage.horizontalCenter}         Column{             ListView{                 id:recordList                 width: historicalRecordItem.width                 height: historicalRecordItem.height                 spacing:10                 clip:true                 model:tradeRecordList                 delegate:HMBOMCheckViewHistorialRecordContextMode{}             }         }     }

在本地创建ListModel用于测试 ListModel{         id:tradeRecordList         ListElement{tradeTimeRecord:"00/10/13 08:00:59";tradeTerminalRecord:"XXXXXXXXXXXX";             tradeTypeRecord:"XXXXXXXXXXXXXX";tradeMoneyRecord:"20元"}         ListElement{tradeTimeRecord:"01/10/13 08:00:59";tradeTerminalRecord:"XXXXXXXXXXXX";             tradeTypeRecord:"XXXXXXXXXXXXXX";tradeMoneyRecord:"20元"}         ListElement{tradeTimeRecord:"02/10/13 08:00:59";tradeTerminalRecord:"XXXXXXXXXXXX";             tradeTypeRecord:"XXXXXXXXXXXXXX";tradeMoneyRecord:"20元"}         ListElement{tradeTimeRecord:"03/10/13 08:00:59";tradeTerminalRecord:"XXXXXXXXXXXX";             tradeTypeRecord:"XXXXXXXXXXXXXX";tradeMoneyRecord:"20元"}         ListElement{tradeTimeRecord:"04/10/13 08:00:59";tradeTerminalRecord:"XXXXXXXXXXXX";             tradeTypeRecord:"XXXXXXXXXXXXXX";tradeMoneyRecord:"20元"}         ListElement{tradeTimeRecord:"05/10/13 08:00:59";tradeTerminalRecord:"XXXXXXXXXXXX";             tradeTypeRecord:"XXXXXXXXXXXXXX";tradeMoneyRecord:"20元"}         ListElement{tradeTimeRecord:"06/10/13 08:00:59";tradeTerminalRecord:"XXXXXXXXXXXX";             tradeTypeRecord:"XXXXXXXXXXXXXX";tradeMoneyRecord:"20元"}         ListElement{tradeTimeRecord:"07/10/13 08:00:59";tradeTerminalRecord:"XXXXXXXXXXXX";             tradeTypeRecord:"XXXXXXXXXXXXXX";tradeMoneyRecord:"20元"}         ListElement{tradeTimeRecord:"08/10/13 08:00:59";tradeTerminalRecord:"XXXXXXXXXXXX";             tradeTypeRecord:"XXXXXXXXXXXXXX";tradeMoneyRecord:"20元"}         ListElement{tradeTimeRecord:"09/10/13 08:00:59";tradeTerminalRecord:"XXXXXXXXXXXX";             tradeTypeRecord:"XXXXXXXXXXXXXX";tradeMoneyRecord:"20元"}         ListElement{tradeTimeRecord:"10/10/13 08:00:59";tradeTerminalRecord:"XXXXXXXXXXXX";             tradeTypeRecord:"XXXXXXXXXXXXXX";tradeMoneyRecord:"20元"}         ListElement{tradeTimeRecord:"11/10/13 08:00:59";tradeTerminalRecord:"XXXXXXXXXXXX";             tradeTypeRecord:"XXXXXXXXXXXXXX";tradeMoneyRecord:"20元"}         ListElement{tradeTimeRecord:"12/10/13 08:00:59";tradeTerminalRecord:"XXXXXXXXXXXX";             tradeTypeRecord:"XXXXXXXXXXXXXX";tradeMoneyRecord:"20元"}         ListElement{tradeTimeRecord:"13/10/13 08:00:59";tradeTerminalRecord:"XXXXXXXXXXXX";             tradeTypeRecord:"XXXXXXXXXXXXXX";tradeMoneyRecord:"20元"}         ListElement{tradeTimeRecord:"14/10/13 08:00:59";tradeTerminalRecord:"XXXXXXXXXXXX";             tradeTypeRecord:"XXXXXXXXXXXXXX";tradeMoneyRecord:"20元"}         ListElement{tradeTimeRecord:"15/10/13 08:00:59";tradeTerminalRecord:"XXXXXXXXXXXX";             tradeTypeRecord:"XXXXXXXXXXXXXX";tradeMoneyRecord:"20元"}         ListElement{tradeTimeRecord:"16/10/13 08:00:59";tradeTerminalRecord:"XXXXXXXXXXXX";             tradeTypeRecord:"XXXXXXXXXXXXXX";tradeMoneyRecord:"20元"}         ListElement{tradeTimeRecord:"17/10/13 08:00:59";tradeTerminalRecord:"XXXXXXXXXXXX";             tradeTypeRecord:"XXXXXXXXXXXXXX";tradeMoneyRecord:"20元"}         ListElement{tradeTimeRecord:"18/10/13 08:00:59";tradeTerminalRecord:"XXXXXXXXXXXX";             tradeTypeRecord:"XXXXXXXXXXXXXX";tradeMoneyRecord:"20元"}         ListElement{tradeTimeRecord:"19/10/13 08:00:59";tradeTerminalRecord:"XXXXXXXXXXXX";             tradeTypeRecord:"XXXXXXXXXXXXXX";tradeMoneyRecord:"20元"}         ListElement{tradeTimeRecord:"20/10/13 08:00:59";tradeTerminalRecord:"XXXXXXXXXXXX";             tradeTypeRecord:"XXXXXXXXXXXXXX";tradeMoneyRecord:"20元"}     }
运行效果如下:
推荐阅读
  • 深入解析Android自定义View面试题
    本文探讨了Android Launcher开发中自定义View的重要性,并通过一道经典的面试题,帮助开发者更好地理解自定义View的实现细节。文章不仅涵盖了基础知识,还提供了实际操作建议。 ... [详细]
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • 导航栏样式练习:项目实例解析
    本文详细介绍了如何创建一个具有动态效果的导航栏,包括HTML、CSS和JavaScript代码的实现,并附有详细的说明和效果图。 ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • Android 渐变圆环加载控件实现
    本文介绍了如何在 Android 中创建一个自定义的渐变圆环加载控件,该控件已在多个知名应用中使用。我们将详细探讨其工作原理和实现方法。 ... [详细]
  • 本文探讨了如何在给定整数N的情况下,找到两个不同的整数a和b,使得它们的和最大,并且满足特定的数学条件。 ... [详细]
  • 本文详细介绍了Java中org.neo4j.helpers.collection.Iterators.single()方法的功能、使用场景及代码示例,帮助开发者更好地理解和应用该方法。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • 本文介绍如何使用Objective-C结合dispatch库进行并发编程,以提高素数计数任务的效率。通过对比纯C代码与引入并发机制后的代码,展示dispatch库的强大功能。 ... [详细]
  • 在使用 DataGridView 时,如果在当前单元格中输入内容但光标未移开,点击保存按钮后,输入的内容可能无法保存。只有当光标离开单元格后,才能成功保存数据。本文将探讨如何通过调用 DataGridView 的内置方法解决此问题。 ... [详细]
  • RecyclerView初步学习(一)
    RecyclerView初步学习(一)ReCyclerView提供了一种插件式的编程模式,除了提供ViewHolder缓存模式,还可以自定义动画,分割符,布局样式,相比于传统的ListVi ... [详细]
  • 扫描线三巨头 hdu1928hdu 1255  hdu 1542 [POJ 1151]
    学习链接:http:blog.csdn.netlwt36articledetails48908031学习扫描线主要学习的是一种扫描的思想,后期可以求解很 ... [详细]
  • 本文介绍如何使用 NSTimer 实现倒计时功能,详细讲解了初始化方法、参数配置以及具体实现步骤。通过示例代码展示如何创建和管理定时器,确保在指定时间间隔内执行特定任务。 ... [详细]
  • 本文详细介绍了Java中org.w3c.dom.Text类的splitText()方法,通过多个代码示例展示了其实际应用。该方法用于将文本节点在指定位置拆分为两个节点,并保持在文档树中。 ... [详细]
  • 本教程涵盖OpenGL基础操作及直线光栅化技术,包括点的绘制、简单图形绘制、直线绘制以及DDA和中点画线算法。通过逐步实践,帮助读者掌握OpenGL的基本使用方法。 ... [详细]
author-avatar
sdauilk_299
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有