custom-long-list.uvue 2.0 KB
Newer Older
1
<template>
2 3
  <view style="flex: 1;background-color: aliceblue;">
    <page-head :title="title"></page-head>
W
wanganxp 已提交
4
    <view class="tips">list-view组件虽然在UI层有recycle机制,但长列表的vnode太多也会造成初始化卡顿。本组件仅创建部分vnode,而未使用list-view,也就是UI层其实是短列表。
W
wanganxp 已提交
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
        <uni-recycle-item class="item" v-for="item in (items as Item[])" :item="item" :key="item.id">
9 10 11 12
          <view class="item-wrapper">
            <view class="name"><text style="font-size: 14px;">{{item.name}}</text></view>
            <view class="info"><text style="font-size: 12px; color: #999999;">{{item.info}}</text></view>
          </view>
13
        </uni-recycle-item>
14
      </template>
15
    </uni-recycle-view>
16
  </view>
17 18 19 20 21
</template>

<script>
  type Item = {
    id : number
22 23
    name : string
    info : string
24 25 26 27
  }
  export default {
    data() {
      return {
W
wanganxp 已提交
28
        title: '自定义虚拟列表组件uni-recycle-view',
29 30 31 32
        list: [] as Item[]
      }
    },
    created() {
33
      for (let i = 0; i < 2000; i++) {
34 35
        this.list.push({
          id: i,
36 37
          name: `Wifi_` + i,
          info: `信号强度: -${(Math.floor(Math.random() * 60) + 40)} db, 安全性: WPA/WPA2/WPA3-Personal`
38 39 40 41 42 43 44
        } as Item)
      }
    },
    methods: {
      scrolltoupper() {
        console.log('scroll top upper')
      },
45 46
      scroll() {
        // console.log('scroll')
47 48 49 50 51 52
      }
    }
  }
</script>

<style>
53 54 55 56 57 58 59 60 61 62
  .tips {
    margin: 10px;
    border-radius: 5px;
    padding: 10px;
    background-color: white;
  }

  .item {
    padding: 5px 10px;
  }
63

64 65 66 67 68
  .item-wrapper {
    padding: 10px;
    border-radius: 5px;
    background-color: white;
  }
W
wanganxp 已提交
69
</style>