custom-long-list.uvue 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <template>
  2. <view style="flex: 1;background-color: aliceblue;">
  3. <page-head :title="title"></page-head>
  4. <view class="tips">list-view组件虽然在UI层有recycle机制,但长列表的vnode太多也会造成初始化卡顿。本组件仅创建部分vnode,而未使用list-view,也就是UI层其实是短列表。
  5. 此示例中仅渲染滚动容器上下5屏的内容。适用于仅使用一个for循环创建所有列表项的场景。文档详见插件市场:https://ext.dcloud.net.cn/plugin?id=17385</view>
  6. <uni-recycle-view style="flex: 1;" :list="list" @scrolltoupper="scrolltoupper" @scroll="scroll">
  7. <template v-slot:default="{items}">
  8. <!-- scroll-view内:key只绑定数据内的id时在微信小程序端有Bug,表现为滚动位置跳动,初步判断为微信scroll-view的Bug。 -->
  9. <uni-recycle-item class="item" v-for="(item, index) in (items as Item[])" :item="item" :key="index + '_' + item.id">
  10. <view class="item-wrapper">
  11. <view class="name"><text style="font-size: 14px;">{{item.name}}</text></view>
  12. <view class="info"><text style="font-size: 12px; color: #999999;">{{item.info}}</text></view>
  13. </view>
  14. </uni-recycle-item>
  15. </template>
  16. </uni-recycle-view>
  17. </view>
  18. </template>
  19. <script>
  20. type Item = {
  21. id : number
  22. name : string
  23. info : string
  24. }
  25. export default {
  26. data() {
  27. return {
  28. title: '自定义虚拟列表组件uni-recycle-view',
  29. list: [] as Item[]
  30. }
  31. },
  32. created() {
  33. for (let i = 0; i < 2000; i++) {
  34. this.list.push({
  35. id: i,
  36. name: `Wifi_` + i,
  37. info: `信号强度: -${(Math.floor(Math.random() * 60) + 40)} db, 安全性: WPA/WPA2/WPA3-Personal`
  38. } as Item)
  39. }
  40. },
  41. methods: {
  42. scrolltoupper() {
  43. console.log('scroll top upper')
  44. },
  45. scroll() {
  46. // console.log('scroll')
  47. }
  48. }
  49. }
  50. </script>
  51. <style>
  52. .tips {
  53. margin: 10px;
  54. border-radius: 5px;
  55. padding: 10px;
  56. background-color: white;
  57. }
  58. .item {
  59. padding: 5px 10px;
  60. }
  61. .item-wrapper {
  62. padding: 10px;
  63. border-radius: 5px;
  64. background-color: white;
  65. }
  66. </style>