cocos creator基礎-(二十二)滾動列表高級使用 動態加載數據


動態加載列表

1: 每個記錄是滾動列表里面的一個項,我們將整個列表分為3頁,每頁固定的項的數目;
2: 一個PAGE的項最好超過滾動列表的大小;
3: 課程案例, 創建一個滾動列表, 每一個page為一個頁,共3個頁,每個page有8個項;
3 * 8 = 24個項, 用1-100來模擬數據記錄;
4: 編寫代碼往滾動列表里面加入所需要的項目;

往下加載示意圖


auto scroll細節

1: auto scroll有自己的控制content的位置的機制,
  會導致content的位置與我們加載時候的位置修改沖突,體現在快速滾動后的連續加載;
2: 處理細節:
  (1)在判斷要加載的時候,先判斷當前是否在auto scroll模式, 如果是返回;
  (2)監聽autosocroll結束后拋出的end事件,在來計算加載;
  (3) 當auto scroll滾動到最上頭的時候,會有回彈,那個時候發生了加載,所以
    在要加載的時候,檢測到時autoscroll,關閉掉回彈的效果,等auto scroll end事件發生了以后再打開;
    this.scroll_view.elastic = false;
    this.scroll_view._autoScrolling

/*
假設世界排行榜要100個玩家的數據,我們怎么使用滾動列表來實現?
顯示[1, 100]這個數據
(1)我們將我們滾動列表里面的每個項分成三個頁
(2)每一個頁面我們制定一個數目,例如8個,根據你的scrollview的大小來決定;
(3)總共使用的滾動列表里面的想 PAGE_NUM * 3 = 24個;
(4) 有限的項要顯示 超過它數目的數據記錄?

*/

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //    default: null,      // The default value will be used only when the component attaching
        //                           to a node for the first time
        //    url: cc.Texture2D,  // optional, default is typeof default
        //    serializable: true, // optional, default is true
        //    visible: true,      // optional, default is true
        //    displayName: 'Foo', // optional
        //    readonly: false,    // optional, default is false
        // },
        // ...
        OPT_HEIGHT: 80, // 每項的高度
        PAGE_NUM: 8, // 每頁為8個;
        item_prefab: {
            type: cc.Prefab,
            default: null,
        },

        scroll_view: {
            type: cc.ScrollView,
            default: null,
        },
    },

    // use this for initialization
    onLoad: function () {
        this.value_set = [];
        // 如果你這里是排行榜,那么你就push排行榜的數據;
        for(var i = 1; i <= 100; i ++) {
            this.value_set.push(i);
        }

        this.content = this.scroll_view.content;
        this.opt_item_set = [];
        for(var i = 0; i < this.PAGE_NUM * 3; i ++) {
            var item = cc.instantiate(this.item_prefab);
            this.content.addChild(item);
            this.opt_item_set.push(item);
        }

        this.scroll_view.node.on("scroll-ended", this.on_scroll_ended.bind(this), this);
    },

    start: function() {
        this.start_y = this.content.y;
        this.start_index = 0; // 當前我們24個Item加載的 100項數據里面的起始數據記錄的索引;
        this.load_record(this.start_index);
    },

    // 從這個索引開始,加載數據記錄到我們的滾動列表里面的選項
    load_record: function(start_index) {
        this.start_index = start_index;

        for(var i = 0; i < this.PAGE_NUM * 3; i ++) {
            var label = this.opt_item_set[i].getChildByName("src").getComponent(cc.Label);
            // 顯示我們的記錄;
            label.string = this.value_set[start_index + i];
        }
    },

    on_scroll_ended: function() {
        this.scrollveiw_load_recode();
        this.scroll_view.elastic = true;
    },

    scrollveiw_load_recode: function() {
         // 向下加載了
        if (this.start_index + this.PAGE_NUM * 3 < this.value_set.length &&
            this.content.y >= this.start_y + this.PAGE_NUM * 2 * this.OPT_HEIGHT) { // 動態加載
            
            if (this.scroll_view._autoScrolling) { // 等待這個自動滾動結束以后再做加載
                this.scroll_view.elastic = false; // 暫時關閉回彈效果
                return;
            }

            var down_loaded = this.PAGE_NUM;
            this.start_index += down_loaded;
            if (this.start_index + this.PAGE_NUM * 3 > this.value_set.length) {
                var out_len = this.start_index + this.PAGE_NUM * 3 - this.value_set.length;
                down_loaded -= (out_len);
                this.start_index -= (out_len);
            }
            this.load_record(this.start_index);

            this.content.y -= (down_loaded * this.OPT_HEIGHT);
            return;
        }

        // 向上加載
        if (this.start_index > 0 && this.content.y <= this.start_y) {
            if (this.scroll_view._autoScrolling) { // 等待這個自動滾動結束以后再做加載
                this.scroll_view.elastic = false;
                return;
            }

            var up_loaded = this.PAGE_NUM;
            this.start_index -= up_loaded;
            if (this.start_index < 0) {
                up_loaded += this.start_index;
                this.start_index = 0; 
            }
            this.load_record(this.start_index);
            this.content.y += (up_loaded * this.OPT_HEIGHT);
        }
        // end 
    },
    // called every frame, uncomment this function to activate update callback
    update: function (dt) {
        this.scrollveiw_load_recode();
    },
});

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM