当前位置: 首页 > news >正文

【ccc3.8】虚拟列表

一个简单的虚拟列表,没有任何其他东西。

原理就是向上滚动时,将下面离开屏幕的那一个item塞到上侧来:

主代码仅有两个:ScrollList对应的滚动容器,ScrollListItem对应单项的预制体

当前支持两种:竖向滚动、横线滚动。不支持那种横竖排版的。有需要自己添加

使用案例:https://github.com/EddyLwei/ccc3.8_VirtualList.git

ScrollList:

/**横向排布拖动*/
export const SCROLL_HORIZONTAL: number = 1;
/**竖向排布拖动*/
export const SCROLL_VERTICAL: number = 2;import { _decorator, Node, Prefab, instantiate, ScrollView, UITransform, Vec3, log } from 'cc';
import { ScrollListItem } from './ScrollListItem';const { ccclass, property } = _decorator;@ccclass('ScrollList')
export class ScrollList extends ScrollView {/**item子节点预制体*/@property({ type: Prefab, tooltip: "item子节点预制体" })itemPrefab: Prefab = null;/**单条记录高度*/private _itemSize: number;/**需要多少个记录组件 在可视范围内+2条*/private _numItem: number = 0;private _itemArr: Array<Node> = [];/**开始端下标*/private _itemIndex: number = 0;/**结束端下标*/private _dataIndex: number = 0;/**数据源*/private _dataArr: any[];/**滚动方向*/private _direction: number = 0;/**间隙 0=开始边框,1=结束边框,2=间隙*/private _gapNum: number[];/**子节点刷新绑定事件,或者使用item继承的模式*/public onItemRender: Function;start() {super.start();this.node.on('scrolling', this.scrollCheck, this);}onDestroy() {super.onDestroy();if (this.node) {this.node.off('scrolling', this.scrollCheck, this);}}/**设置数据* @param dataArr : 数据源* @param direction : 滚动方向,默认上下* @param gap : [开始边框距离,结束边框距离,每个之间空隙]*/public setDataList(dataArr: any[], direction: number = SCROLL_VERTICAL, gap?: number[]) {this._dataArr = dataArr;this._direction = direction;this._gapNum = gap;this.createItem();}/**获得数据后开始创建*/private createItem() {let _showSize = this.node.getComponent(UITransform).height;//获得预制体的高度if (!this._itemSize) {let pNode = instantiate(this.itemPrefab);if (this._direction == SCROLL_HORIZONTAL) {this._itemSize = pNode.getComponent(UITransform).contentSize.width;_showSize = this.node.getComponent(UITransform).width;}else {this._itemSize = pNode.getComponent(UITransform).contentSize.height;}pNode.destroy();// log("---_itemSize--", this._itemSize);}//可视范围,对应可以创建多少个实体单例itemthis._numItem = Math.floor(_showSize / this._itemSize) + 2;log(_showSize, "初始化获得数量:", this._numItem)if (this._dataArr.length < this._numItem) {this._numItem = this._dataArr.length;}this._itemArr.length = 0;for (let index = 0; index < this._numItem; index++) {let pNode = instantiate(this.itemPrefab);pNode.parent = this.content;this._itemArr.push(pNode);this.itemRender(pNode, index);}//设置容器大小let contentSize = this._itemSize * this._dataArr.length;//前面距离边框if (this._gapNum && this._gapNum[0]) {contentSize += this._gapNum[0];}//后面距离边框if (this._gapNum && this._gapNum[1]) {contentSize += this._gapNum[1];}//间隙距离if (this._gapNum && this._gapNum[2]) {contentSize += this._gapNum[2] * (this._dataArr.length - 1);}if (this._direction == SCROLL_HORIZONTAL) {this.content.getComponent(UITransform).width = contentSize;}else {this.content.getComponent(UITransform).height = contentSize;}this._itemIndex = this._dataIndex = this._itemArr.length - 1;log("初始化结束:", this._dataIndex, this._itemArr.length)}private scrollCheck() {let nowPos = this.getScrollOffset().y;let topPos = (this._dataIndex + 1 - this._numItem) * this._itemSize;//当前屏幕中靠近最开始的坐标//前面边框if (this._gapNum && this._gapNum[0]) {topPos += this._gapNum[0];}//间隙距离if (this._gapNum && this._gapNum[2]) {topPos += this._gapNum[2] * (this._dataIndex + 1 - this._numItem);}// let topPos = this.countPosByIndex(this._dataIndex + 1 - this._numItem);let size = this._itemSize;if (this._direction == SCROLL_HORIZONTAL) {nowPos = this.getScrollOffset().x;topPos = -topPos;size = -this._itemSize;}//判断向结束端滚动,滚动点和初始点对比if ((this._direction == SCROLL_VERTICAL && nowPos > size + topPos) ||(this._direction == SCROLL_HORIZONTAL && nowPos < size + topPos)) {let newIndex = this._dataIndex + 1;// Log.log(this._dataIndex, "-判断向结束端滚动 1 --将头部item转移到最后---", nowPos, topPos);if (newIndex >= this._dataArr.length) {return; //如果滚动到底部最后一条数据,不再进行写入}this._dataIndex = newIndex;let topItemIndex = this._itemIndex + 1;if (topItemIndex >= this._numItem) {topItemIndex = 0;}let item = this._itemArr[topItemIndex];if (item) {this.itemRender(item, newIndex);// Log.error(topItemIndex, "转移到最后", item.node.position);}this._itemIndex = topItemIndex;}//判断向开始端滚动else if ((this._direction == SCROLL_VERTICAL && nowPos < topPos) ||(this._direction == SCROLL_HORIZONTAL && nowPos > topPos)) {let newIndex = this._dataIndex + 1 - this._numItem - 1;// Log.log(this._dataIndex, "-判断向上滚动 2 -将最后item转移到头部----", newIndex);if (newIndex < 0) {// Log.warn("如果滚动到第一条数据,不再进行写入", newIndex)return; //如果滚动到第一条数据,不再进行写入}this._dataIndex--;// Log.error(this._itemIndex, "将最后item转移到头部", this._dataIndex, newIndex, newIndex * -this._itemSize);let item = this._itemArr[this._itemIndex];if (item) {this.itemRender(item, newIndex);// Log.error(this._itemIndex, "转移头部", item.node.position);}this._itemIndex--;if (this._itemIndex < 0) {this._itemIndex = this._numItem - 1;}}}/**刷新单项*/private itemRender(node: Node, newIndex: number) {//设置有全局得刷新事件if (this.onItemRender) {this.onItemRender(node, newIndex);}//没有全局,使用继承的itemelse {const item = node.getComponent(ScrollListItem)if (item) {item.onItemRender(this._dataArr[newIndex]);}}this.setPos(node, newIndex);}/**设置坐标*/private setPos(node: Node, index: number) {let pos = this.countPosByIndex(index);if (this._direction == SCROLL_HORIZONTAL) {node.setPosition(new Vec3(pos, 0));}else {node.setPosition(new Vec3(0, -pos));}}/**根据下标计算坐标*/private countPosByIndex(index: number): number {let pos = (1 / 2 + index) * this._itemSize;//前面距离边框if (this._gapNum && this._gapNum[0]) {pos += this._gapNum[0];}//间隙距离if (this._gapNum && this._gapNum[2]) {pos += this._gapNum[2] * index;}return pos;}}

ScrollListItem:(其实仅用一个接口的作用)


import { _decorator, Component, } from 'cc';const { ccclass, property } = _decorator;@ccclass('ScrollListItem')
export class ScrollListItem extends Component {/**滚动列表数据变更*/onItemRender(data, ...param: any[]) { }}

使用方式很简单:

import { _decorator, Component, Node } from 'cc';
import { SCROLL_HORIZONTAL, SCROLL_VERTICAL, ScrollList } from './ScrollList';
const { ccclass, property } = _decorator;@ccclass('TestScene')
export class TestScene extends Component {@property({ type: ScrollList, tooltip: "竖行滚动容器" })private vScroll: ScrollList;@property({ type: ScrollList, tooltip: "横向滚动容器" })private hScroll: ScrollList;start() {const dataArr = [];for (let index = 0; index < 50; index++) {dataArr.push(index)}this.hScroll.setDataList(dataArr, SCROLL_HORIZONTAL, [50, 50, 20]);this.vScroll.setDataList(dataArr, SCROLL_VERTICAL, [50, 50, 20]);}}

组件的设置注意:

相关文章:

【ccc3.8】虚拟列表

一个简单的虚拟列表&#xff0c;没有任何其他东西。 原理就是向上滚动时&#xff0c;将下面离开屏幕的那一个item塞到上侧来&#xff1a; 主代码仅有两个&#xff1a;ScrollList对应的滚动容器&#xff0c;ScrollListItem对应单项的预制体 当前支持两种&#xff1a;竖向滚动、…...

【23种设计模式】单一职责原则

个人主页&#xff1a;金鳞踏雨 个人简介&#xff1a;大家好&#xff0c;我是金鳞&#xff0c;一个初出茅庐的Java小白 目前状况&#xff1a;22届普通本科毕业生&#xff0c;几经波折了&#xff0c;现在任职于一家国内大型知名日化公司&#xff0c;从事Java开发工作 我的博客&am…...

DNS入门学习:什么是TTL值?如何设置合适的TTL值?

TTL值是域名解析中的一个重要参数&#xff0c;TTL值设置的合理与否对于域名解析的效率和准确性有着非常重要的影响&#xff0c;因此对于网站管理者而言&#xff0c;了解什么是TTL值以及如何设置合理的TTL值对于做好域名解析管理&#xff0c;确保网站的安全稳定运行至关重要。 …...

ilr normalize isometric log-ratio transformation

visium_heart/st_snRNAseq/05_colocalization/create_niches_ct.R at 5b30c7e497e06688a8448afd8d069d2fa70ebcd2 saezlab/visium_heart (github.com) 更多内容&#xff0c;关注微信&#xff1a;生信小博士 The ILR (Isometric Log-Ratio) transformation is used in the anal…...

el表单的简单查询方法

预期效果 实现表单页面根据groupid 、type 、errortype进行数据过滤 实现 第一步&#xff0c;在页面中添加输入或者是下拉框&#xff0c;并且用相应的v-model进行绑定 <div style"display: flex;flex-direction: row;"><el-input style"width: auto…...

【USRP】通信总的分支有哪些

概述 通信是一个广泛的领域&#xff0c;涵盖了许多不同的技术、应用和专业分支。以下是通信领域的一些主要分支&#xff1a; 有线通信&#xff1a;这涉及到利用物理媒介&#xff08;如电缆、光纤&#xff09;进行通信。 电信&#xff1a;包括电话、电报和传真服务。宽带&#…...

关于服务器网络代理解决方案(1024)

方法一、nginx代理 配置代理服务器 在能够访问外网的服务器上&#xff0c;安装和配置 Nginx。你可以使用包管理器来安装 Nginx&#xff0c;例如&#xff1a; csharpCopy codesudo apt-get install nginx # 对于基于 Debian/Ubuntu 的系统 sudo yum install nginx # 对于基于 C…...

Linux下 /etc/shadow内容详解

/etc/shadow 文件&#xff0c;用于存储 Linux 系统中用户的密码信息&#xff0c;又称为“影子文件”。 前面介绍了 /etc/passwd 文件&#xff0c;由于该文件允许所有用户读取&#xff0c;易导致用户密码泄露&#xff0c;因此 Linux 系统将用户的密码信息从 /etc/passwd 文件中…...

Go学习第二章——变量与数据类型

Go变量与数据类型 1 变量1.1 变量概念1.2 变量的使用步骤1.3 变量的注意事项1.4 ""的使用 2 数据类型介绍3 整数类型3.1 有符号整数类型3.2 无符号整数类型3.3 其他整数类型3.4 整型的使用细节 4 小数类型/浮点型4.1 浮点型的分类4.2 简单使用 5 字符类型5.1 字符类型…...

【剑指Offer】:循环有序列表的插入(涉及链表的知识)

给定循环单调非递减列表中的一个点&#xff0c;写一个函数向这个列表中插入一个新元素 insertVal &#xff0c;使这个列表仍然是循环升序的 给定的可以是这个列表中任意一个顶点的指针&#xff0c;并不一定是这个列表中最小元素的指针 如果有多个满足条件的插入位置&#xff0c…...

【Django 04】Django-DRF(ModelViewSet)

DRF是什么&#xff1f; ModelViewSet 是 Django REST framework 提供的一个视图集类&#xff0c;它封装了常见的模型操作方法。 模型类提供了默认的增删改查功能。 它继承自 GenericViewSet、ListModelMixin、RetrieveModelMixin、CreateModelMixin、UpdateModelMixin、Dest…...

ubuntu命令

一、 防火墙命令 1、安装防火墙 sudo sudo apt-get install ufw2、查看防火墙状态 sudo ufw status# 返回结果 # Status: inactive # 表示没有开启防火墙3、开启防火墙 sudo ufw enable# 返回结果 # Command may disrupt existing ssh connections. Proceed with operation…...

C++学习之强制类型转换

强制类型转换运算符 带着三个疑问阅读&#xff1a; 出现的背景是什么&#xff1f;何时使用&#xff1f;如何使用&#xff1f; MSDN . 强制转换运算符 C中的四种强制类型转换符详解 static_cast (1) 使用场景 在基本数据类型之间转换&#xff0c;如把 int 转换为 char&#…...

在Linux中,可以使用以下命令来查看进程

在Linux中&#xff0c;可以使用以下命令来查看进程&#xff1a; ps 命令&#xff1a;显示当前用户的进程状态。 ps&#xff1a;显示当前终端会话中正在运行的进程。ps aux&#xff1a;显示系统中所有正在运行的进程&#xff0c;包括其他用户的进程。ps -ef&#xff1a;显示系统…...

【算法训练-动态规划 一】【应用DP问题】零钱兑换、爬楼梯、买卖股票的最佳时机I、打家劫舍

废话不多说&#xff0c;喊一句号子鼓励自己&#xff1a;程序员永不失业&#xff0c;程序员走向架构&#xff01;本篇Blog的主题是【动态规划】&#xff0c;使用【数组】这个基本的数据结构来实现&#xff0c;这个高频题的站点是&#xff1a;CodeTop&#xff0c;筛选条件为&…...

2023年中职组“网络安全”赛项云南省竞赛任务书

2023年中职组“网络安全”赛项 云南省竞赛任务书 一、竞赛时间 总计&#xff1a;360分钟 竞赛阶段 竞赛阶段 任务阶段 竞赛任务 竞赛时间 分值 A模块 A-1 登录安全加固 180分钟 200分 A-2 本地安全策略配置 A-3 流量完整性保护 A-4 事件监控 A-5 服务加固…...

Modeling Deep Learning Accelerator Enabled GPUs

Modeling Deep Learning Accelerator Enabled GPUs 发表在 ISPASS 2019 上。文章研究了 NVIDIA 的 Volta 和 Turing 架构中张量核的设计&#xff0c;并提出了 Volta 中张量核的架构模型。 基于 GPGPU-Sim 实现该模型&#xff0c;并且支持 CUTLASS 运行。发现其性能与硬件非常吻…...

《动手学深度学习 Pytorch版》 9.5 机器翻译与数据集

机器翻译&#xff08;machine translation&#xff09;指的是将序列从一种语言自动翻译成另一种语言&#xff0c;基于神经网络的方法通常被称为神经机器翻译&#xff08;neural machine translation&#xff09;。 import os import torch from d2l import torch as d2l9.5.1 …...

网络入门基础

网络入门基础 文章目录 网络入门基础网络的发展协议的概念网络协议初识协议分层层状结构OSI七层模型TCP/IP五层(或四层)模型TCP/IP模型和计算机软硬体系结构的关系 网络传输基本流程同局域网的两台主机通信不同局域网的两台主机通信 网络中的地址管理认识IP地址认识MAC地址 网络…...

Towards a Rigorous Evaluation of Time-series Anomaly Detection(论文翻译)

1 Introduction 随着工业4.0加速系统自动化&#xff0c;系统故障的后果可能会产生重大的社会影响&#xff08;Baheti和Gill 2011; Lee 2008; Lee&#xff0c;Bagheri和Kao 2015&#xff09;。为了防止这种故障&#xff0c;检测系统的异常状态比以往任何时候都更加重要&#xff…...

测试微信模版消息推送

进入“开发接口管理”--“公众平台测试账号”&#xff0c;无需申请公众账号、可在测试账号中体验并测试微信公众平台所有高级接口。 获取access_token: 自定义模版消息&#xff1a; 关注测试号&#xff1a;扫二维码关注测试号。 发送模版消息&#xff1a; import requests da…...

超短脉冲激光自聚焦效应

前言与目录 强激光引起自聚焦效应机理 超短脉冲激光在脆性材料内部加工时引起的自聚焦效应&#xff0c;这是一种非线性光学现象&#xff0c;主要涉及光学克尔效应和材料的非线性光学特性。 自聚焦效应可以产生局部的强光场&#xff0c;对材料产生非线性响应&#xff0c;可能…...

Java如何权衡是使用无序的数组还是有序的数组

在 Java 中,选择有序数组还是无序数组取决于具体场景的性能需求与操作特点。以下是关键权衡因素及决策指南: ⚖️ 核心权衡维度 维度有序数组无序数组查询性能二分查找 O(log n) ✅线性扫描 O(n) ❌插入/删除需移位维护顺序 O(n) ❌直接操作尾部 O(1) ✅内存开销与无序数组相…...

鸿蒙中用HarmonyOS SDK应用服务 HarmonyOS5开发一个医院挂号小程序

一、开发准备 ​​环境搭建​​&#xff1a; 安装DevEco Studio 3.0或更高版本配置HarmonyOS SDK申请开发者账号 ​​项目创建​​&#xff1a; File > New > Create Project > Application (选择"Empty Ability") 二、核心功能实现 1. 医院科室展示 /…...

【SQL学习笔记1】增删改查+多表连接全解析(内附SQL免费在线练习工具)

可以使用Sqliteviz这个网站免费编写sql语句&#xff0c;它能够让用户直接在浏览器内练习SQL的语法&#xff0c;不需要安装任何软件。 链接如下&#xff1a; sqliteviz 注意&#xff1a; 在转写SQL语法时&#xff0c;关键字之间有一个特定的顺序&#xff0c;这个顺序会影响到…...

Spring Cloud Gateway 中自定义验证码接口返回 404 的排查与解决

Spring Cloud Gateway 中自定义验证码接口返回 404 的排查与解决 问题背景 在一个基于 Spring Cloud Gateway WebFlux 构建的微服务项目中&#xff0c;新增了一个本地验证码接口 /code&#xff0c;使用函数式路由&#xff08;RouterFunction&#xff09;和 Hutool 的 Circle…...

A2A JS SDK 完整教程:快速入门指南

目录 什么是 A2A JS SDK?A2A JS 安装与设置A2A JS 核心概念创建你的第一个 A2A JS 代理A2A JS 服务端开发A2A JS 客户端使用A2A JS 高级特性A2A JS 最佳实践A2A JS 故障排除 什么是 A2A JS SDK? A2A JS SDK 是一个专为 JavaScript/TypeScript 开发者设计的强大库&#xff…...

腾讯云V3签名

想要接入腾讯云的Api&#xff0c;必然先按其文档计算出所要求的签名。 之前也调用过腾讯云的接口&#xff0c;但总是卡在签名这一步&#xff0c;最后放弃选择SDK&#xff0c;这次终于自己代码实现。 可能腾讯云翻新了接口文档&#xff0c;现在阅读起来&#xff0c;清晰了很多&…...

jmeter聚合报告中参数详解

sample、average、min、max、90%line、95%line,99%line、Error错误率、吞吐量Thoughput、KB/sec每秒传输的数据量 sample&#xff08;样本数&#xff09; 表示测试中发送的请求数量&#xff0c;即测试执行了多少次请求。 单位&#xff0c;以个或者次数表示。 示例&#xff1a;…...

Caliper 配置文件解析:fisco-bcos.json

config.yaml 文件 config.yaml 是 Caliper 的主配置文件,通常包含以下内容: test:name: fisco-bcos-test # 测试名称description: Performance test of FISCO-BCOS # 测试描述workers:type: local # 工作进程类型number: 5 # 工作进程数量monitor:type: - docker- pro…...