【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】虚拟列表
一个简单的虚拟列表,没有任何其他东西。 原理就是向上滚动时,将下面离开屏幕的那一个item塞到上侧来: 主代码仅有两个:ScrollList对应的滚动容器,ScrollListItem对应单项的预制体 当前支持两种:竖向滚动、…...
【23种设计模式】单一职责原则
个人主页:金鳞踏雨 个人简介:大家好,我是金鳞,一个初出茅庐的Java小白 目前状况:22届普通本科毕业生,几经波折了,现在任职于一家国内大型知名日化公司,从事Java开发工作 我的博客&am…...
DNS入门学习:什么是TTL值?如何设置合适的TTL值?
TTL值是域名解析中的一个重要参数,TTL值设置的合理与否对于域名解析的效率和准确性有着非常重要的影响,因此对于网站管理者而言,了解什么是TTL值以及如何设置合理的TTL值对于做好域名解析管理,确保网站的安全稳定运行至关重要。 …...
ilr normalize isometric log-ratio transformation
visium_heart/st_snRNAseq/05_colocalization/create_niches_ct.R at 5b30c7e497e06688a8448afd8d069d2fa70ebcd2 saezlab/visium_heart (github.com) 更多内容,关注微信:生信小博士 The ILR (Isometric Log-Ratio) transformation is used in the anal…...
el表单的简单查询方法
预期效果 实现表单页面根据groupid 、type 、errortype进行数据过滤 实现 第一步,在页面中添加输入或者是下拉框,并且用相应的v-model进行绑定 <div style"display: flex;flex-direction: row;"><el-input style"width: auto…...
【USRP】通信总的分支有哪些
概述 通信是一个广泛的领域,涵盖了许多不同的技术、应用和专业分支。以下是通信领域的一些主要分支: 有线通信:这涉及到利用物理媒介(如电缆、光纤)进行通信。 电信:包括电话、电报和传真服务。宽带&#…...
关于服务器网络代理解决方案(1024)
方法一、nginx代理 配置代理服务器 在能够访问外网的服务器上,安装和配置 Nginx。你可以使用包管理器来安装 Nginx,例如: csharpCopy codesudo apt-get install nginx # 对于基于 Debian/Ubuntu 的系统 sudo yum install nginx # 对于基于 C…...
Linux下 /etc/shadow内容详解
/etc/shadow 文件,用于存储 Linux 系统中用户的密码信息,又称为“影子文件”。 前面介绍了 /etc/passwd 文件,由于该文件允许所有用户读取,易导致用户密码泄露,因此 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】:循环有序列表的插入(涉及链表的知识)
给定循环单调非递减列表中的一个点,写一个函数向这个列表中插入一个新元素 insertVal ,使这个列表仍然是循环升序的 给定的可以是这个列表中任意一个顶点的指针,并不一定是这个列表中最小元素的指针 如果有多个满足条件的插入位置,…...
【Django 04】Django-DRF(ModelViewSet)
DRF是什么? ModelViewSet 是 Django REST framework 提供的一个视图集类,它封装了常见的模型操作方法。 模型类提供了默认的增删改查功能。 它继承自 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++学习之强制类型转换
强制类型转换运算符 带着三个疑问阅读: 出现的背景是什么?何时使用?如何使用? MSDN . 强制转换运算符 C中的四种强制类型转换符详解 static_cast (1) 使用场景 在基本数据类型之间转换,如把 int 转换为 char&#…...
在Linux中,可以使用以下命令来查看进程
在Linux中,可以使用以下命令来查看进程: ps 命令:显示当前用户的进程状态。 ps:显示当前终端会话中正在运行的进程。ps aux:显示系统中所有正在运行的进程,包括其他用户的进程。ps -ef:显示系统…...
【算法训练-动态规划 一】【应用DP问题】零钱兑换、爬楼梯、买卖股票的最佳时机I、打家劫舍
废话不多说,喊一句号子鼓励自己:程序员永不失业,程序员走向架构!本篇Blog的主题是【动态规划】,使用【数组】这个基本的数据结构来实现,这个高频题的站点是:CodeTop,筛选条件为&…...
2023年中职组“网络安全”赛项云南省竞赛任务书
2023年中职组“网络安全”赛项 云南省竞赛任务书 一、竞赛时间 总计: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 架构中张量核的设计,并提出了 Volta 中张量核的架构模型。 基于 GPGPU-Sim 实现该模型,并且支持 CUTLASS 运行。发现其性能与硬件非常吻…...
《动手学深度学习 Pytorch版》 9.5 机器翻译与数据集
机器翻译(machine translation)指的是将序列从一种语言自动翻译成另一种语言,基于神经网络的方法通常被称为神经机器翻译(neural machine translation)。 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加速系统自动化,系统故障的后果可能会产生重大的社会影响(Baheti和Gill 2011; Lee 2008; Lee,Bagheri和Kao 2015)。为了防止这种故障,检测系统的异常状态比以往任何时候都更加重要ÿ…...
DBAPI如何优雅的获取单条数据
API如何优雅的获取单条数据 案例一 对于查询类API,查询的是单条数据,比如根据主键ID查询用户信息,sql如下: select id, name, age from user where id #{id}API默认返回的数据格式是多条的,如下: {&qu…...
回溯算法学习
一、电话号码的字母组合 import java.util.ArrayList; import java.util.List;import javax.management.loading.PrivateClassLoader;public class letterCombinations {private static final String[] KEYPAD {"", //0"", //1"abc", //2"…...
使用LangGraph和LangSmith构建多智能体人工智能系统
现在,通过组合几个较小的子智能体来创建一个强大的人工智能智能体正成为一种趋势。但这也带来了一些挑战,比如减少幻觉、管理对话流程、在测试期间留意智能体的工作方式、允许人工介入以及评估其性能。你需要进行大量的反复试验。 在这篇博客〔原作者&a…...
windows系统MySQL安装文档
概览:本文讨论了MySQL的安装、使用过程中涉及的解压、配置、初始化、注册服务、启动、修改密码、登录、退出以及卸载等相关内容,为学习者提供全面的操作指导。关键要点包括: 解压 :下载完成后解压压缩包,得到MySQL 8.…...
在 Spring Boot 中使用 JSP
jsp? 好多年没用了。重新整一下 还费了点时间,记录一下。 项目结构: pom: <?xml version"1.0" encoding"UTF-8"?> <project xmlns"http://maven.apache.org/POM/4.0.0" xmlns:xsi"http://ww…...
渗透实战PortSwigger靶场:lab13存储型DOM XSS详解
进来是需要留言的,先用做简单的 html 标签测试 发现面的</h1>不见了 数据包中找到了一个loadCommentsWithVulnerableEscapeHtml.js 他是把用户输入的<>进行 html 编码,输入的<>当成字符串处理回显到页面中,看来只是把用户输…...
Modbus RTU与Modbus TCP详解指南
目录 1. Modbus协议基础 1.1 什么是Modbus? 1.2 Modbus协议历史 1.3 Modbus协议族 1.4 Modbus通信模型 🎭 主从架构 🔄 请求响应模式 2. Modbus RTU详解 2.1 RTU是什么? 2.2 RTU物理层 🔌 连接方式 ⚡ 通信参数 2.3 RTU数据帧格式 📦 帧结构详解 🔍…...
第八部分:阶段项目 6:构建 React 前端应用
现在,是时候将你学到的 React 基础知识付诸实践,构建一个简单的前端应用来模拟与后端 API 的交互了。在这个阶段,你可以先使用模拟数据,或者如果你的后端 API(阶段项目 5)已经搭建好,可以直接连…...
背包问题双雄:01 背包与完全背包详解(Java 实现)
一、背包问题概述 背包问题是动态规划领域的经典问题,其核心在于如何在有限容量的背包中选择物品,使得总价值最大化。根据物品选择规则的不同,主要分为两类: 01 背包:每件物品最多选 1 次(选或不选&#…...
vxe-table vue 表格复选框多选数据,实现快捷键 Shift 批量选择功能
vxe-table vue 表格复选框多选数据,实现快捷键 Shift 批量选择功能 查看官网:https://vxetable.cn 效果 代码 通过 checkbox-config.isShift 启用批量选中,启用后按住快捷键和鼠标批量选取 <template><div><vxe-grid v-bind"gri…...
