| 主機參考:VPS測評參考推薦/專注分享VPS服務器優惠信息!若您是商家可以在本站進行投稿,查看詳情!此外我們還提供軟文收錄、PayPal代付、廣告贊助等服務,查看詳情! |
| 我們發布的部分優惠活動文章可能存在時效性,購買時建議在本站搜索商家名稱可查看相關文章充分了解該商家!若非中文頁面可使用Edge瀏覽器同步翻譯!PayPal代付/收錄合作 |
【相關學習推薦:微信小程序教程】
背景
在小程序中,你會遇到很多場景下的一長串交互。 如果頁面渲染過多的wxml,使用node會導致小程序頁面卡頓、白屏。 主要原因有:
1.列表數據量較大,初始化setData和繪制列表wxml需要時間。
2. 渲染了很多wxml節點。 每次setData更新視圖時,都必須創建一個新的虛擬樹,并且與舊樹進行比較是相對耗時的。
3.需要渲染的wxml節點很多,頁面只能容納這么多wxml,占用大量內存。
微信小程序自帶的滾動-視圖沒有針對長列表進行優化。 官方組件recycle-view是一個類似于virtual-list的長列表組件。 現在我們來分析一下虛擬列表的原理,并從頭開始實現一個小程序虛擬-列表。
實現原理
首先你需要了解什么是virtual-list。 這是一個只加載“可見區域”及其附近dom元素的初始化,并且在滾動過程中僅重用dom元素。 滾動列表的前端優化技術,渲染“可見區域”及其附近的 DOM 元素。 與傳統的列表方法相比,它可以實現非常高的初始渲染性能,并且僅在滾動期間保持超輕量級的 DOM 結構。
虛擬列表最重要的概念:
可滾動區域:例如列表容器的高度為600,其內部元素的高度之和為Exceed。 容器的這個區域可以滾動。 這是一個“可滾動區域”。
可見區域:例如一個列表容器,高度為600,上方有一個垂直滾動條。 右側用于滾動。 視覺上可見的內部區域是“可見區域”。
虛擬列表實現的核心是監聽滾動事件,通過滾動距離偏移和渲染來動態調整“可見區域”數據的頂部距離和前/后截距索引值。 滾動元素的總大小,totalSize。 實施步驟如下:
1. 監聽滾動事件Top/scrollLeft,計算“顯示區域”中起始項的索引值startIndex和結束項的索引值endIndex。
2.通過startIndex和endIndex截取長列表中“可見”的數據項。 更新列表。
3. 計算可滾動區域高度和項目偏移量并將其應用于可滾動區域和項目。
1. 列表項寬度/高度和滾動偏移
在虛擬列表中,“可滾動區域”是根據每個列表項的寬度/高度計算的,可能需要自定義。 定義 itemSizeGetter 函數來計算列表項的寬度/高度。
itemSizeGetter(itemSize) { return (index:number) => { if (isFunction(itemSize)) { return itemSize(index); Return isArray(itemSize) : ItemSize };
在滾動過程中,不計算不可見列表項的itemSize。 在這種情況下,使用估計的列表項estimateItemSize。 目的是在計算“可滾動區域”的高度時,計算出未測量的itemSize。 請改用estimatedItemSize。
getSizeAndPositionOfLastMeasuredItem() { return this.lastMeasuredIndex >= 0 ? this.itemSizeAndPositionData[this.lastMeasuredIndex] : { 偏移量: 0, 大小: 0 };t lastMeasuredSizeAndPosition = this.getSizeAndPositionOfLastMeasuredItem(); return (lastMeasuredSizeAndPosition.offset + lastMeasuredSizeAndPosition.size + (this.itemCount - this.lastMeasuredIndex - 1) * this.estimatedItemSize ); 復制代碼
在此處顯示發生了什么是最后計算的列表項的 itemSize 和 offset 直接通過緩存命中。 這是因為每個列表項的兩個參數在檢索時被緩存。
getSizeAndPositionForIndex(index:number) { if (index > this.lastMeasuredIndex) { const lastMeasuredSizeAndPosition = this.getSizeAndPositionOfLastMeasuredItem(); 讓 offset = lastMeasuredSizeAndPosition.offset + lastMeasuredSizeAndPosition.size for (let i = this.lastMeasuredIndex + 1; ; i <= 索引; i++) { const size = this.itemSizeGetter(i); this.itemSizeAndPositionData[i] = { 偏移量, 大小,}; 偏移量 += 大小; this.lastMeasuredIndex = 索引; }復制代碼。 2.根據偏移量找到索引值。
滾動處理時必須傳遞滾動偏移offset。 計算顯示區域中顯示的第一個數據項的索引值。 通常,每個列表項的 itemSize 可以從 0 開始計算。 一旦累加超過偏移量,就可以得到索引值。 但如果數據量太大,頻繁觸發滾動事件,性能會明顯下降。 幸運的是,列表項的滾動距離嚴格按照升序排列,因此我們可以對緩存的數據進行二分查找,將時間復雜度降低到 O(lgN) 。
js代碼如下。
findNearestItem(offset:number) { offset = Math.max(0, offset); const lastMeasuredSizeAndPosition = this.getSizeAndPositionOfLastMeasuredItem(); const lastMeasuredIndex = Math.max(0 , this.lastMeasuredIndex); >= offset) { return this.binarySearch({ high: lastMeasuredIndex, low: 0, offset, }); } else { return this.exponentialSearch({ index: lastMeasuredIndex, offset, }); private binarySearch({ low, high, offset, }: { low: number; high: number; offset: number; }) { let middle = 0 } } currentOffset = 0; while (low <= high) { Medium = low + Math.floor((high - low) / 2); currentOffset = this.getSizeAndPositionForIndex(middle).offset; if (currentOffset === offset) { return middle Masu 。 } else if (currentOffset offset) {high = middle - 1; if (low > 0) { return low - 1; 返回 0 。 復制代碼
對于不緩存計算結果的搜索,首先使用指數搜索來縮小搜索范圍,然后使用二分搜索。
privateexponentialSearch({ 索引, 偏移量, }: { 索引: 數字; 偏移量: 數字; }) { 間隔 = 1; }while (index < this.itemCount && this.getSizeAndPositionForIndex(index).offset < offset ) { index += 間隔; 間隔 *= 2; return this.binarySearch({ high: Math.min(index, this.itemCount [k3 ] 1)、低:Math.floor(index / 2), offset, }); 復制代碼。 3、計算startIndex、endIndex
調整“可視區域”大小、containerSize、滾動偏移、預渲染項數、overscanCount后,我們就可以計算出起始項的索引值startIndex。 以及“可見區域”的結束項的索引值endIndex。 實施步驟如下:
1. 找到距偏移量最近的索引值的距離。 該值為起始項startIndex的索引值。
2.通過startIndex獲取item的偏移量和大小,并調整偏移量。
在3containerSize上加上.offset,得到最終item的maxOffset,從startIndex開始累加,直到超過maxOffset,得到最終item的索引值endIndex。
JS代碼如下。
getVisibleRange({containerSize, offset, overscanCount, }: {containerSize: number; offset: number; overscanCount: number; }): { start?: number; stop?: number } { const maxOffset = offset + 容器大小; const datum = this.getSizeAndPositionForIndex(start); 。 偏移+基準尺寸;停止=讓我們開始。 while (offset < maxOffset && stop < this.itemCount - 1) { stop++; offset += this.getSizeAndPositionForIndex(stop).size; if (overscanCount) { start = Math.max(0, start - overscanCount) ; stop = Math.min(stop + overscanCount, this.itemCount - 1); } return { start, stop, };} 復制代碼 3. 監聽滾動事件并實現虛擬列表滾動
現在可以實現通過偵聽滾動事件并動態更新 startIndex、endIndex、totalSize 和 來實現虛擬列表滾動。 抵消。
js代碼如下。
getItemStyle(index) { const style = this.styleCache[index];e) { return style; const {scrollDirection} = this.data; const { size, offset, } = this.sizeAndPositionManager.getSizeAndPositionForIndex(index); constcumputedStyle = styleToCssString({ 位置: '絕對', 頂部: 0, 左側: 0 , width: '100%', [positionProp[scrollDirection]]: 偏移量, [sizeProp[scrollDirection]]: 大小, }); this.styleCache[index] =cumputedStyle ; },observScroll(offset:number){const{scrollDirection,overscanCount,visibleRange} = this.data; const { start, stop } = this.sizeAndPositionManager.getVisibleRange({ 容器大小: this.data[sizeProp[scrollDirection] ] || 0、偏移、超過canCount, }); const totalSize = this.sizeAndPositionManager.getTotalSize(); if (totalSize !== this.data.totalSize) { this.setData({totalSize }); if (visibleRange.start !== start ||visibleRange .stop !== stop) { const styleItems: string[] = []; if (isNumber(start) && isNumber(stop)) { let Index = start - 1 while (++index <= stop) { styleItems.push(this.getItemStyle(index)); this.triggerEvent('render', { startIndex: start, stopIndex: stop, styleItems, }); this.data.offset = offset; start; this.data.visibleRange.stop = stop; }, 復制代碼
在調用時,我調用了 startIndex,通過渲染事件停止index、styleItems 截取長列表“顯示區域”中的數據,并使用絕對定位將列表項的 itemSize 和偏移量應用到列表中。
代碼如下。
let list = Array .from({ length: 10000 }).map((_,index) =>index);Page({ data: { itemSize:index => 50 * ((index % 3) + 1), styleItems: null, itemCount : list.length, list: [], }, onReady() { this.virtualListRef = this.virtualListRef || this.selectComponent('#virtual-list'); startIndex, stopIndex, styleItems } = e.detail; this.setData({ list: list.slice(startIndex, stopIndex + 1), styleItems, }), loadMore() { setTimeout(() => { const appendList = Array. from({ length: 10 }).map( (_, index) => list.length + index, ); list = list.concat(appendList);list.length, list: this.data.list.concat(appendList), }); },});復制代碼 {{ item + 1 }} {{itemCount}}</view>復制代碼參考資料
在為這個微信小程序創建virtual-list組件的過程中,我們主要參考了幾個優秀的開源虛擬列表實現方案。
react-tiny-virtual-listreact-virtualizedreact-窗口概述
通過上面的講解,我們首先在微信小程序環境下實現虛擬列表,我獲得了一個對原理有更深入的理解。 列表。 但目前尚不適用于瀑布流布局或不可預測的列表項大小等場景。 在快速滾動期間,我會看到渲染速度太慢的白屏。 通過增加“可見區域”之外的預渲染項的數量,overscanCount,可以在一定程度上緩解這個問題。
下載微信應用
微信是一款移動通訊軟件,支持通過手機網絡發送語音消息、視頻、照片和文本。 微信帶來了全新的移動通信體驗,您可以單獨或群組聊天,還可以根據您的地理位置查找附近的人。 有需要的朋友,請快來保存您的下載體驗吧!
這幾篇文章你可能也喜歡:
- 如何在微信系的父母(頁面通信)中實現兒童傳輸參數(微信Mini Program Navigateto傳輸參數)
- 如何操作微信迷你計劃購物中心系統? (使用Mini計劃購物中心的教程)
- 關于如何在Mini程序中創建頁面QR碼的簡要說明(如何為Mini Program Page生成QR碼)
- 簡單分析如何動態創建迷你程序代碼(在微信中更改動態圖像)
- 如何檢查微信系的無線密碼(如何檢查微信小程序的WiFi密碼)
本文由主機參考刊發,轉載請注明:微信小程序中如何實現virtual-list詳解(微信小程序視圖類) http://www.k2270.com/123213.html
主機參考

















評論前必須登錄!
注冊