Qt qml listview下拉刷新和上拉分頁主要根據(jù)contentY來判斷。但要加上頂部下拉指示器、滾動(dòng)條,并封裝成可簡(jiǎn)單調(diào)用的組件,著實(shí)花了我不少精力:)
先給大家展示下效果圖:


【功能】
下拉刷新和上拉分頁邏輯/下拉刷新/上拉更多/滾動(dòng)欄/工具欄半拉顯隱Author: surfsky.cnblogs.comLisence: MIT 請(qǐng)保留此文檔聲明History:init. surfsky.cnblogs.com, 2015-01add initPosition property. 2015-01
【調(diào)用】
控件使用非常簡(jiǎn)單,只要實(shí)現(xiàn) onLoad 和 onLoadMore 事件即可,其他的和標(biāo)準(zhǔn)的ListView差不多。
/**新聞示例下拉刷新上拉分頁滾動(dòng)軸頂部工具欄頂部工具欄自動(dòng)吸附當(dāng)前行高亮Author: surfsky.cnblogs.com 2015-01*/ListViewEx{id: viewwidth: 500height: 800pageSize: 50snapHeader: trueinitPosition: 'header'// 頂部新聞圖片欄headerComponent: Component{PageView{id: pvwidth: view.widthheight: 100clip: trueRectangle{width:pv.width; height:pv.height; color: 'green'}Rectangle{width:pv.width; height:pv.height; color: 'yellow'}Rectangle{width:pv.width; height:pv.height; color: 'blue'}}}// 行UI代理delegate: Text {id: wrapper;width: parent.width;height: 32;font.pointSize: 15;verticalAlignment: Text.AlignVCenter;horizontalAlignment: Text.AlignHCenter;text: content;//color: ListView.view.currentIndex == index ? "white" : "#505050";MouseArea {anchors.fill: parent;onClicked: wrapper.ListView.view.currentIndex = index;}}//-----------------------------------------// 數(shù)據(jù)加載事件//-----------------------------------------onLoad:{for (var i = 0 ; i < pageSize ; ++i)model.append({"index": i, "content": "Item " + i})}onLoadMore:{for (var i = pageSize*page ; i < pageSize*(page+1); ++i)model.append({"index": i, "content": "Item " + i})}}【核心代碼】
實(shí)在太長了,截取ContentY處理部分,其他的下載了看吧
//-------------------------------------// 下拉刷新和上拉分頁邏輯//-------------------------------------onMovementEnded: {//console.log("movementEnded: originY:" + originY + ", contentY:" + contentY + ", reflesh:" + needReflesh + ", more:" + needLoadMore);// 刷新數(shù)據(jù)if (needReflesh){lv.headerItem.goState('load');model.reflesh();needReflesh = false;}// 加載新數(shù)據(jù)else if (needLoadMore){model.loadMore();needLoadMore = false;}else {var h1 = lv.headerItem.loader.height;var h2 = lv.headerItem.indicator.height;// 頭部區(qū)自動(dòng)顯隱(拖動(dòng)過小隱藏頭部,反之顯示)if (snapHeader){if (contentY >= -h1/3 && contentY < 0)moveToFirst();if (contentY >= -h1 && contentY < -h1/3)moveToHeader();}// 刷新區(qū)自動(dòng)顯隱if (contentY >=-(h1+h2) && contentY < -h1)moveToHeader();}}onContentYChanged: {// 下拉刷新判斷邏輯:已經(jīng)到頭了,還下拉一定距離if (contentY < originY){var dy = contentY - originY;if (dy < -10){lv.headerItem.goState('ready');needReflesh = true;}else {if (pressed){//console.log(pressed);//needReflesh = false; // 如何判斷當(dāng)前鼠標(biāo)是否按下?如果是按下狀態(tài)才能取消刷新lv.headerItem.goState('');}}}// 上拉加載判斷邏輯:已經(jīng)到底了,還上拉一定距離if (contentHeight>height && contentY-originY > contentHeight-height){var dy = (contentY-originY) - (contentHeight-height);//console.log("y: " + contentY + ", dy: " + dy);if (dy > 40){needLoadMore = true;//console.log("originY:" + originY + ", contentY:" + contentY + ", height:" + height + ", contentheight:" + contentHeight);}}}以上所述是小編給大家介紹的Qt qml中l(wèi)istview 列表視圖控件(下拉刷新、上拉分頁、滾動(dòng)軸),希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)武林網(wǎng)網(wǎng)站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選