cocos2dx-lua ListView上拉加载功能

目录



1. 引言

  过完端午节,先来分享一段。有缘的小伙伴,自觉带走;没有缘分的以后还有机会,哈哈。。。好了,下面直接上代码!

2. 代码

2.1 进入场景

function GoodsLayer:onEnter()
    --查询兑换历史纪录
   self:requestGoodList(self.curPage)
end

2.2 请求商品列表

function GoodsLayer:requestGoodList(pageIndex)
    local url = string.format("http://"..ServerHttpHost.."/appInterface.ashx?data={\"CmdID\":num,\"ToKen\":\"%s\",\"PageIndex\":%d,\"PageSize\":%d}", token, pageIndex or 1, PageSize or 50)
    G_HttpRequest(url, function(result)
            print("————请求商品列表", result.MessageDic)
            if result.CmdID == num then
                self.totalPage = math.ceil(result.RecordCount/PageSize)
                print("商品列表消息内容:", result.GoodList)
                local items = {}
                for k,v in ipairs(result.GoodsLayer) do
                    local item = {}
                    item.name = v.ShopName

                    table.insert(items, item)
                end

                self:refreshGoodList(items)
            end
        end)
end

2.3 刷新商品列表及上拉请求加载

function GoodsLayer:refreshGoodList(itemList)
    --self.listView:removeAllItems()
    -- self.listView:setBackGroundColorType(1)
    -- self.listView:setBackGroundColor(cc.c3b(0,123,0))
    -- local function listViewEvent(sender, eventType)
    --     -- 事件类型为点击结束
    --     print("eventType=",eventType)
    --     if eventType == ccui.ListViewEventType.ONSELECTEDITEM_END then
    --         print("select child index = ",sender:getCurSelectedIndex())
    --     end
    -- end

    -- -- 设置ListView的监听事件
    -- self.listView:addEventListener(listViewEvent)

    -- 滚动事件方法回调  
    local function scrollViewEvent(sender, eventType)  
        -- 滚动到底部  
        if eventType == ccui.ScrollviewEventType.scrollToBottom then

        elseif eventType == ccui.ScrollviewEventType.scrolling then
            --local percent = self:getCurPercent()
            print("SCROLLVIEW_EVENT_SCROLLING", percent)

            --self.exchangeprogress:setValue(percent*100)

        elseif eventType == ccui.ScrollviewEventType.bounceBottom then
            print("请求更新")
            self.curPage = self.curPage +1
            if self.curPage <= self.totalPage then                
                self:requestGoodList(self.curPage)
            end

        end
    end
    self.listView:addScrollViewEventListener(scrollViewEvent)

    for k, v in ipairs(itemList) do
        local item = ccui.Layout:create()
        item:setContentSize(cc.size(620, 30))
        self.listView:pushBackCustomItem(item)

        local txt = v.time .. "  商品名字:".. v.name .."  价格:".. v.price 
        print("-----addItem", k, txt)
        local content = ccui.Text:create(txt, "font/JIANZHUNYUAN.ttf", 18);
        content:setColor(cc.c3b(0xFF,0xFF,0xFF));
        content:setAnchorPoint(cc.p(0,0));
        content:setPosition(cc.p(0,0));

        local status = ccui.Text:create("txt", "font/JIANZHUNYUAN.ttf", 18);
        if v. status == 0 then
            status:setString("  -->销售中")
            status:setColor(cc.c3b(0xFF,0x0,0x0));
            status:setAnchorPoint(cc.p(0,0));
            status:setPositionX(content:getContentSize().width);
        else
            status:setString("  -->已售罄")
            status:setColor(cc.c3b(0x00,0xFF,0x00));
            status:setAnchorPoint(cc.p(0,0));
            status:setPositionX(content:getContentSize().width);
        end
        status:addTo(content)

        content:addTo(item, k)
    end

end

2.4. 补充知识

-- 0-3滑动到上下左右触发,4滑动一直触发,5-8惯性滑动到上下左右触发
ccui.ScrollviewEventType = {
    scrollToTop =  0,
    scrollToBottom =  1,
    scrollToLeft = 2,
    scrollToRight = 3,
    scrolling = 4,
    bounceTop = 5,
    bounceBottom = 6,
    bounceLeft = 7,
    bounceRight = 8,
}
SCROLLVIEW_EVENT_SCROLL_TO_TOP         = ccui.ScrollviewEventType.scrollToTop
SCROLLVIEW_EVENT_SCROLL_TO_BOTTOM      = ccui.ScrollviewEventType.scrollToBottom
SCROLLVIEW_EVENT_SCROLL_TO_LEFT        = ccui.ScrollviewEventType.scrollToLeft
SCROLLVIEW_EVENT_SCROLL_TO_RIGHT       = ccui.ScrollviewEventType.scrollToRight
SCROLLVIEW_EVENT_SCROLLING             = ccui.ScrollviewEventType.scrolling
SCROLLVIEW_EVENT_BOUNCE_TOP            = ccui.ScrollviewEventType.bounceTop
SCROLLVIEW_EVENT_BOUNCE_BOTTOM         = ccui.ScrollviewEventType.bounceBottom
SCROLLVIEW_EVENT_BOUNCE_LEFT           = ccui.ScrollviewEventType.bounceLeft
SCROLLVIEW_EVENT_BOUNCE_RIGHT          = ccui.ScrollviewEventType.bounceRight

3. 总结

  前面的代码和注释已经很详细了,这里就不做进一步解释了。


The End
  好了,今天的分享就到这里,如有不足之处,还望大家及时指正,随时欢迎大家留言评论和探讨交流!!!

猜你喜欢

转载自blog.csdn.net/lxt610/article/details/80732965