程序员scholar 程序员scholar
首页
  • Java 基础

    • JavaSE
    • JavaIO
    • JavaAPI速查
  • Java 高级

    • JUC
    • JVM
    • Java新特性
    • 设计模式
  • Web 开发

    • Servlet
    • Java网络编程
  • Web 标准

    • HTML
    • CSS
    • JavaScript
  • 前端框架

    • Vue2
    • Vue3
    • Vue3 + TS
    • 微信小程序
    • uni-app
  • 工具与库

    • jQuery
    • Ajax
    • Axios
    • Webpack
    • Vuex
    • WebSocket
    • 第三方登录
  • 后端与语言扩展

    • ES6
    • Typescript
    • node.js
  • Element-UI
  • Apache ECharts
  • 数据结构
  • HTTP协议
  • HTTPS协议
  • 计算机网络
  • Linux常用命令
  • Windows常用命令
  • SQL数据库

    • MySQL
    • MySQL速查
  • NoSQL数据库

    • Redis
    • ElasticSearch
  • 数据库

    • MyBatis
    • MyBatis-Plus
  • 消息中间件

    • RabbitMQ
  • 服务器

    • Nginx
  • Spring框架

    • Spring6
    • SpringMVC
    • SpringBoot
    • SpringSecurity
  • SpringCould微服务

    • SpringCloud基础
    • 微服务之DDD架构思想
  • 日常必备

    • 开发常用工具包
    • Hutoll工具包
    • IDEA常用配置
    • 开发笔记
    • 日常记录
    • 项目部署
    • 网站导航
    • 产品学习
    • 英语学习
  • 代码管理

    • Maven
    • Git教程
    • Git小乌龟教程
  • 运维工具

    • Docker
    • Jenkins
    • Kubernetes
  • 算法笔记

    • 算法思想
    • 刷题笔记
  • 面试问题常见

    • 十大经典排序算法
    • 面试常见问题集锦
关于
GitHub (opens new window)
首页
  • Java 基础

    • JavaSE
    • JavaIO
    • JavaAPI速查
  • Java 高级

    • JUC
    • JVM
    • Java新特性
    • 设计模式
  • Web 开发

    • Servlet
    • Java网络编程
  • Web 标准

    • HTML
    • CSS
    • JavaScript
  • 前端框架

    • Vue2
    • Vue3
    • Vue3 + TS
    • 微信小程序
    • uni-app
  • 工具与库

    • jQuery
    • Ajax
    • Axios
    • Webpack
    • Vuex
    • WebSocket
    • 第三方登录
  • 后端与语言扩展

    • ES6
    • Typescript
    • node.js
  • Element-UI
  • Apache ECharts
  • 数据结构
  • HTTP协议
  • HTTPS协议
  • 计算机网络
  • Linux常用命令
  • Windows常用命令
  • SQL数据库

    • MySQL
    • MySQL速查
  • NoSQL数据库

    • Redis
    • ElasticSearch
  • 数据库

    • MyBatis
    • MyBatis-Plus
  • 消息中间件

    • RabbitMQ
  • 服务器

    • Nginx
  • Spring框架

    • Spring6
    • SpringMVC
    • SpringBoot
    • SpringSecurity
  • SpringCould微服务

    • SpringCloud基础
    • 微服务之DDD架构思想
  • 日常必备

    • 开发常用工具包
    • Hutoll工具包
    • IDEA常用配置
    • 开发笔记
    • 日常记录
    • 项目部署
    • 网站导航
    • 产品学习
    • 英语学习
  • 代码管理

    • Maven
    • Git教程
    • Git小乌龟教程
  • 运维工具

    • Docker
    • Jenkins
    • Kubernetes
  • 算法笔记

    • 算法思想
    • 刷题笔记
  • 面试问题常见

    • 十大经典排序算法
    • 面试常见问题集锦
关于
GitHub (opens new window)
npm

(进入注册为作者充电)

  • 原生微信小程序

  • uniapp多端开发

    • 快速入门

    • 内置组件

      • 视图容器

      • 基础内容

      • 表单组件

      • 路由页面跳转

      • 媒体组件

      • 地图

      • 画布

      • 网页嵌入

      • 页面属性配置节点

      • nvue组件

        • 条形码/二维码组件
        • 基础列表组件
        • 列表单元格组件
          • 1. 什么是 cell 组件?
          • 2. cell 组件的常用属性
            • 2.1 控制 cell 的插入与删除动画
            • 2.2 保持滚动位置
            • 2.3 控制 cell 的回收机制
          • 3. cell` 配置案例
        • 虚拟列表组件
        • 瀑布流组件
        • 下拉刷新组件
      • 小程序组件

    • 扩展组件

  • 小程序开发
  • uniapp多端开发
  • 内置组件
  • nvue组件
scholar
2024-10-21
目录

列表单元格组件

# 列表单元格组件

cell 组件是 App 端 nvue 专用的列表项组件,主要用于 list、recycler 和 waterfall 组件中。它的重要作用在于帮助原生引擎进行资源的高效管理和回收,从而提升长列表的渲染性能。

# 1. 什么是 cell 组件?

cell 组件是一个必须嵌套在 list、recycler 或 waterfall 组件中的基础组件,用于定义列表中的单个列表项。cell 支持添加任意类型的子组件作为内容,但应避免在 cell 内添加滚动容器。通过使用 cell,可以提升长列表渲染的性能,同时减少内存占用。

使用场景

  • 列表项定义:用于定义列表中的单个条目或元素,作为 list、recycler 或 waterfall 组件的子组件。
  • 高性能长列表展示:在处理大量数据时,通过 cell 的回收机制提升渲染性能。
  • 自定义列表内容:在 cell 内嵌入各种组件,自由定义列表项的内容布局。

# 2. cell 组件的常用属性

cell 组件提供了一些用于控制列表项行为的属性,如是否保留滚动位置、插入和删除动画等。以下是常用属性的详细说明及使用示例:

属性名 类型 默认值 说明
keep-scroll-position Boolean false 控制插入单元格后是否保持最后一个滑动位置
insert-animation String none 插入 cell 时的动画效果,支持 none 和 default
delete-animation String none 删除 cell 时的动画效果,支持 none 和 default
recycle Boolean true 控制 cell 及其子视图在滚动时是否回收,建议保持为 true 以节省内存
render-reverse-position Boolean false 定义开始渲染的位置,需搭配 list 的 render-reverse 属性共同使用

# 2.1 控制 cell 的插入与删除动画

  • 说明:通过 insert-animation 和 delete-animation 属性设置 cell 的插入与删除时的动画效果。
  • 类型:String
  • 默认值:none
<template>
  <list>
    <!-- 使用默认插入和删除动画 -->
    <cell 
      v-for="(item, index) in dataList" 
      :key="item.id"
      insert-animation="default" 
      delete-animation="default"
    >
      <text>{{ item.name }}</text>
    </cell>
  </list>
</template>

<script>
import { ref } from 'vue'

export default {
  setup() {
    const dataList = ref([
      { id: "1", name: 'Item A' },
      { id: "2", name: 'Item B' },
      { id: "3", name: 'Item C' }
    ]);

    return { dataList };
  }
}
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

# 2.2 保持滚动位置

  • 说明:通过 keep-scroll-position 属性,控制当新 cell 插入时是否保持列表的滚动位置不变。
  • 类型:Boolean
  • 默认值:false
<template>
  <list>
    <!-- 设置保持滚动位置 -->
    <cell 
      v-for="(item, index) in dataList" 
      :key="item.id"
      :keep-scroll-position="true"
    >
      <text>{{ item.name }}</text>
    </cell>
  </list>
</template>

<script>
import { ref } from 'vue'

export default {
  setup() {
    const dataList = ref([
      { id: "1", name: 'Item A' },
      { id: "2", name: 'Item B' },
      { id: "3", name: 'Item C' }
    ]);

    return { dataList };
  }
}
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

# 2.3 控制 cell 的回收机制

  • 说明:通过 recycle 属性控制 cell 及其子视图在滚动时是否被回收。建议保持为 true,以确保滚动时的内存占用较低。
  • 类型:Boolean
  • 默认值:true
<template>
  <list>
    <!-- 控制回收机制,默认回收 -->
    <cell 
      v-for="(item, index) in dataList" 
      :key="item.id"
      :recycle="true"
    >
      <text>{{ item.name }}</text>
    </cell>
  </list>
</template>

<script>
import { ref } from 'vue'

export default {
  setup() {
    const dataList = ref([
      { id: "1", name: 'Item A' },
      { id: "2", name: 'Item B' },
      { id: "3", name: 'Item C' }
    ]);

    return { dataList };
  }
}
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

# 3. cell` 配置案例

以下案例展示了如何在 list 中使用 cell 组件,结合插入动画、删除动画、回收机制和滚动位置控制等功能,完成列表项的高效渲染。

<template>
  <list>
    <!-- 列表项,包含插入和删除动画,支持回收 -->
    <cell 
      v-for="(item, index) in dataList" 
      :key="item.id"
      insert-animation="default"
      delete-animation="default"
      :keep-scroll-position="true"
      :recycle="true"
    >
      <text>{{ item.name }}</text>
    </cell>
  </list>
</template>

<script>
import { ref } from 'vue'

export default {
  setup() {
    const dataList = ref([
      { id: "1", name: 'Item A' },
      { id: "2", name: 'Item B' },
      { id: "3", name: 'Item C' }
    ]);

    return { dataList };
  }
}
</script>

<style>
/* 样式配置,控制 cell 布局 */
.text {
  font-size: 16px;
  padding: 10px;
}
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
编辑此页 (opens new window)
上次更新: 2025/02/01, 02:18:15
基础列表组件
虚拟列表组件

← 基础列表组件 虚拟列表组件→

Theme by Vdoing | Copyright © 2019-2025 程序员scholar
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式