diff --git a/pages.json b/pages.json index 61fbb3d373fd822138164e7f5f813324d5051b73..1f2e70b8baee15010ec2c21f7d3ba7e6190bbf85 100644 --- a/pages.json +++ b/pages.json @@ -1274,7 +1274,7 @@ { "path" : "pages/template/custom-long-list/custom-long-list", "style" : { - "navigationBarTitleText" : "", + "navigationBarTitleText" : "自行实现长列表组件", "enablePullDownRefresh" : false } } diff --git a/pages/template/custom-long-list/custom-list-item.uvue b/pages/template/custom-long-list/custom-list-item.uvue index 1bb145b414305a25c46a9eab52ce37ec0d020ce2..b95c44ef4d79fa07101836cc39fb932b3f888fa6 100644 --- a/pages/template/custom-long-list/custom-list-item.uvue +++ b/pages/template/custom-long-list/custom-list-item.uvue @@ -14,18 +14,13 @@ } }, inject: { - cachedSize: { - type: Map as PropType>, - default: () : Map => { - return new Map() - } + setCachedSize: { + type: Function as PropType<(item : any, size : number) => void> }, }, mounted() { - nextTick(() => { - uni.createSelectorQuery().in(this).select('.custom-list-item-box').boundingClientRect().exec((ret) => { - (this.cachedSize as Map).set(this.item, (ret[0] as NodeInfo).height!) - }) + uni.createSelectorQuery().in(this).select('.custom-list-item-box').boundingClientRect().exec((ret) => { + this.setCachedSize(this.item, (ret[0] as NodeInfo).height!) }) } } @@ -33,4 +28,4 @@ + \ No newline at end of file diff --git a/pages/template/custom-long-list/custom-list-view.uvue b/pages/template/custom-long-list/custom-list-view.uvue index 731189f63a2078b52fbeecb02fd920e867a7aeec..40677eb24e182aadd26296a2726a26e3d3c8aab4 100644 --- a/pages/template/custom-long-list/custom-list-view.uvue +++ b/pages/template/custom-long-list/custom-list-view.uvue @@ -34,6 +34,9 @@ }) }, deep: true + }, + defaultItemSize() { + this.rearrange(this.lastScrollTop) } }, data() { @@ -47,11 +50,18 @@ initialized: false, hasDefaultSize: false, defaultItemSize: 40, + lastScrollTop: 0, }; }, provide() { return { - cachedSize: this.cachedSize + setCachedSize: (item : any, size : number) => { + if (!this.hasDefaultSize) { + this.defaultItemSize = size + this.hasDefaultSize = true + } + this.cachedSize.set(item, size) + } } }, created() { @@ -72,6 +82,7 @@ return } const scrollTop = e.detail.scrollTop + this.lastScrollTop = 0 if (scrollTop < this.offsetThreshold[1] || scrollTop > this.offsetThreshold[2]) { this.rearrange(scrollTop) } @@ -90,10 +101,6 @@ const cachedItemSize = this.cachedSize.get(item) if (cachedItemSize != null) { itemSize = cachedItemSize - if (!this.hasDefaultSize) { - this.defaultItemSize = itemSize - this.hasDefaultSize = true - } } tempTotalHeight += itemSize if (tempTotalHeight >= this.offsetThreshold[0] && tempTotalHeight <= this.offsetThreshold[3]) { @@ -112,4 +119,4 @@ + \ No newline at end of file diff --git a/pages/template/custom-long-list/custom-long-list.uvue b/pages/template/custom-long-list/custom-long-list.uvue index 0117508d1d6dc4e490d0fcc34d6ddb8652c39d8e..638bc9c1ce3e5132a8ebb3325d97ca2ff59a7e30 100644 --- a/pages/template/custom-long-list/custom-long-list.uvue +++ b/pages/template/custom-long-list/custom-long-list.uvue @@ -1,19 +1,25 @@ + .item-wrapper { + padding: 10px; + border-radius: 5px; + background-color: white; + } + \ No newline at end of file