---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by momo.
--- DateTime: 2019-12-11 18:31
---


---数据
dataSource = {
    items = {
        {
            icon = "http://s.momocdn.com/w/u/others/2019/01/16/1547610372024-01.png",
            text = "11111"
        },
        {
            icon = "http://s.momocdn.com/w/u/others/2019/01/16/1547610372064-02.png",
            text = "22222"
        },
        {
            icon = "http://s.momocdn.com/w/u/others/2019/01/16/1547610372063-3.png",
            text = "33333"
        },
        {
            icon = "http://s.momocdn.com/w/u/others/2019/01/16/1547610372137-4.png",
            text = "44444"
        },
        {
            icon = "http://s.momocdn.com/w/u/others/2019/01/16/1547610372063-5.png",
            text = "55555"
        }
    }
}

--- 创建banner
local function createBanner(viewPager, tabLayout)
    ---FirstViewPager轮播使用的ViewPager
    local FirstViewPager = ViewPager()
                            :width(MeasurementType.MATCH_PARENT)
                            :height(100)
                            :autoScroll(true)
                            :frameInterval(5)
                            :showIndicator(true)
                            :recurrence(true)
                            ----配置一下FirstViewPager的点击监听
                            :setPageClickListener(function (position)
                                Toast("Click position: "..tostring(position),1)
                                ---点击FirstViewPager对应的position页 就跳转到viewPager的对应position页
                                viewPager:scrollToPage(position,true)
                                ---清除viewPager对应position的提示
                                tabLayout:setTapBadgeNumAtIndex(0,position)
                            end)
    ---FirstViewPager的adapter
    local firstadapter = ViewPagerAdapter()
                            :getCount(function ()
                                return #dataSource.items
                            end)
                            :initCell(function (cell)
                                cell.bgImage = ImageView()
                                                :contentMode(ContentMode.SCALE_ASPECT_FIT)
                                                :width(100)
                                                :height(100)
                                cell.contentView:cornerRadius(10)
                                                :clipToBounds(true)
                                                :bgColor(Color(255, 0, 0, 0.5))
                                                :addView(cell.bgImage)
                            end)
                            :fillCellData(function (cell, position)
                                local items = dataSource.items[position]
                                cell.bgImage:image(items.icon)
                            end)

    FirstViewPager:adapter(firstadapter)

    return FirstViewPager
end

local function createOutViewPager(tabLayout)
    ---滑动分页的ViewPager
    local viewPager = ViewPager()
                        :width(MeasurementType.MATCH_PARENT)
                        :height(MeasurementType.MATCH_PARENT)
                        :marginTop(50)
                        :showIndicator(false)
                        :setPageClickListener(function (position)  --设置点击指定页的回调
                            Toast("Click position: " .. tostring(position), 1)
                        end)

    local adapter = ViewPagerAdapter()
                        :getCount(function()  --设置cell数量回调
                            return #dataSource.items
                        end)
                        :reuseId(function (position)
                            if(position == 1)then
                                return "first_page"
                            else
                                return "cellId"
                            end
                        end)
                        :initCellByReuseId("first_page",function (cell)  --设置初始化cell的回调
                            cell.bgImage = ImageView()
                                            :width(200)
                                            :height(200)
                                            :marginTop(200)
                                            :marginLeft(80)
                                            :cornerRadius(10)
                                            :contentMode(ContentMode.SCALE_ASPECT_FIT)

                            cell.contentView:bgColor(Color(255, 0, 0, 0.5))
                                            :addView(cell.bgImage)
                                            :addView(createBanner(viewPager, tabLayout))
                        end)
                        :fillCellDataByReuseId("first_page",function (cell, position)  --设置初始化cell数据的回调
                            local items = dataSource.items[position]
                            cell.bgImage:image(items.icon)
                        end)
                        :initCellByReuseId("cellId",function (cell)  --设置初始化cell的回调
                            cell.bgImage = ImageView()
                                            :width(200)
                                            :height(200)
                                            :marginTop(200)
                                            :marginLeft(80)
                                            :cornerRadius(10)
                                            :contentMode(ContentMode.SCALE_ASPECT_FIT)
                                            :bgColor(Color(255, 0, 0, 0.5))

                            cell.contentView:bgColor(Color(255, 0, 0, 0.5))
                                            :addView(cell.bgImage)
                        end)
                        :fillCellDataByReuseId("cellId",function (cell, position)  --设置初始化cell数据的回调
                            local items = dataSource.items[position]
                            cell.bgImage:image(items.icon)
                        end)

    viewPager:adapter(adapter)
    return viewPager
end

local function createTabSegment()
    local array = Array()
    for _,v in ipairs(dataSource.items) do
        array:add(v.text)
    end
    ---初始化一个TabSegmentView
    local tabLayout = TabSegmentView(Rect(0,0,window:width(),50), array)
                        :selectScale(1.6)  --选中时缩放比例
                        :normalFontSize(15)  --默认字体大小
                        :setTapBadgeNumAtIndex(30, 3)  --设置标注数
                        :setTabSelectedListener(function (index)  --设置tab选中的索引
                            Toast("select "..index,1)
                        end)
    return tabLayout
end
local tabLayout = createTabSegment()
local viewPager = createOutViewPager(tabLayout)
tabLayout:relatedToViewPager(viewPager)  --实现联动效果
window:addView(viewPager)
window:addView(tabLayout)
