| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- const { ccclass, property } = cc._decorator;
- /**
- * 翻页组件类
- * - 简单版 无动态加载功能
- * - 已知bug,请注意规避:开启回弹后,首尾界面回弹没有考虑 setCurrentPageIndex 导致在setCurrentPageIndex设置之后的位置开始回弹。解决方法:关闭回弹
- * @author 薛鸿潇
- */
- @ccclass
- export default class PageView extends cc.Component {
- /** 翻页组件 */
- private comp_page_view: cc.PageView = null;
- /** item 容器 */
- private arr_node_item: Array<cc.Node> = [];
- onLoad() {
- this.comp_page_view = this.node.getComponent(cc.PageView);
- this.node.on('ui-init', this.init, this);// 初始化列表
- this.node.on('ui-clearItem', this.clearItem, this);// 清除页面
- this.node.on('ui-backCurPage', this.backCurPage, this);// 返回到当前Index对应的界面【滑动后禁用滑动功能,再打开滑动功能,翻页组件不会自动返回,调用此接口】
- }
- start() {
- }
- /**
- * 初始化列表
- * @param list_data 数据列表
- * @param page_item item预制体
- */
- private init(list_data: Array<any>, page_item: cc.Prefab, init_index: number = 0) {
- const data_count = list_data.length;
- if (!data_count) {
- // 刷新时,数据列表是空的
- const node_count = this.arr_node_item.length;
- for (let i = 0; i < node_count; i++) {
- this.comp_page_view.removePageAtIndex(i);
- this.arr_node_item.splice(i, 1);
- }
- return;
- }
- for (let i = 0; i < data_count; i++) {
- let node_item = this.arr_node_item[i];
- if (!node_item) {
- node_item = cc.instantiate(page_item);
- this.arr_node_item.push(node_item);
- }
- // node_item.parent = this.content;
- this.comp_page_view.addPage(node_item);
- node_item.getComponent(node_item.name).setItemData(list_data[i]);
- }
- this.comp_page_view.setCurrentPageIndex(init_index);
- // 清理多出来的item
- const node_count = this.arr_node_item.length;
- if (data_count > node_count) {
- for (let i = data_count; i < node_count; i++) {
- this.comp_page_view.removePageAtIndex(i);
- this.arr_node_item.splice(i, 1);
- }
- }
- }
- /**
- * 返回当前页
- */
- private backCurPage() {
- let index = this.comp_page_view.getCurrentPageIndex();
- this.comp_page_view.setCurrentPageIndex(index);
- }
-
- /** 清理子节点
- * - need_destroy 销毁
- */
- private clearItem(need_destroy: boolean = false) {
- // this.comp_page_view.removeAllPages();
- // const n_count = this.arr_node_item.length;
- // for (let i = 0; i < n_count; i++) {
- // if (need_destroy) {
- // let node_end = this.arr_node_item.pop();
- // node_end.destroy();
- // } else {
- // this.arr_node_item[i].removeFromParent();
- // }
- // }
- }
- // update (dt) {}
- }
|