custom-long-list.uvue 2.1 KB
Newer Older
1
<template>
2 3
  <view style="flex: 1;background-color: aliceblue;">
    <page-head :title="title"></page-head>
W
wanganxp 已提交
4 5
    <view class="tips">list-view组件虽然在UI层有recycle机制,但长列表的vnode太多也会造成初始化卡顿。本组件仅创建部分vnode,而未使用list-view,也就是UI层其实是短列表。
    此示例中仅渲染滚动容器上下5屏的内容。适用于仅使用一个for循环创建所有列表项的场景。</view>
6 7 8 9 10 11 12 13 14 15 16
    <custom-list-view style="flex: 1;" :list="list" @scrolltoupper="scrolltoupper" @scroll="scroll">
      <template v-slot:default="{items}">
        <custom-list-item class="item" v-for="item in (items as Item[])" :item="item" :key="item.id">
          <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>
        </custom-list-item>
      </template>
    </custom-list-view>
  </view>
17 18 19 20 21
</template>

<script>
  type Item = {
    id : number
22 23
    name : string
    info : string
24 25 26 27 28 29 30 31 32 33
  }
  import CustomListView from "./custom-list-view"
  import CustomListItem from "./custom-list-item"
  export default {
    components: {
      CustomListView,
      CustomListItem
    },
    data() {
      return {
34
        title: '自行实现长列表组件',
35 36 37 38
        list: [] as Item[]
      }
    },
    created() {
39
      for (let i = 0; i < 2000; i++) {
40 41
        this.list.push({
          id: i,
42 43
          name: `Wifi_` + i,
          info: `信号强度: -${(Math.floor(Math.random() * 60) + 40)} db, 安全性: WPA/WPA2/WPA3-Personal`
44 45 46 47 48 49 50
        } as Item)
      }
    },
    methods: {
      scrolltoupper() {
        console.log('scroll top upper')
      },
51 52
      scroll() {
        // console.log('scroll')
53 54 55 56 57 58
      }
    }
  }
</script>

<style>
59 60 61 62 63 64 65 66 67 68
  .tips {
    margin: 10px;
    border-radius: 5px;
    padding: 10px;
    background-color: white;
  }

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

70 71 72 73 74
  .item-wrapper {
    padding: 10px;
    border-radius: 5px;
    background-color: white;
  }
W
wanganxp 已提交
75
</style>