Skip to content

ViewPagerDemo.llua

xu.jingyu edited this page Aug 11, 2020 · 2 revisions

copy下面代码,运行试试

model(homeData)

mCell(item) {
    VStack()
    .widthPercent(100)
    .heightPercent(100)
    .mainAxis(MainAxis.CENTER)
    .crossAxis(CrossAxis.CENTER).subs(

        ImageView()
        .width(50)
        .height(50)
        .bgColor(Color(0xaaaaaa))
        .cornerRadius(30)
        ,
        Label(item.title)
        .bgColor(Color(0xffff00))
        ,
        Label(item.name)
        .bgColor(Color(0xffff00))
    )
}
mCell2(item) {
    ImageView("https://hbimg.huabanimg.com/7c41bc5871d74c9036932ca9bba76de363727be113b6fd-NApej6_fw658")
    .widthPercent(100)
    .heightPercent(100)
    .contentMode(ContentMode.SCALE_ASPECT_FILL)
}
mLabel(str) {
    Label(str).padding(20, 20, 20, 20)
    .bgColor(Color(200, 200, 100, 1))
    .left(10)
}
---
--- UI
ui {
    --- layout views
    ViewPager()
    .ID(vp)
    .bindData(homeData.list)
    .spacing(5)
    .bgColor(Color(200, 200, 200, 1))
    .height(150)
    .setScrollEnable(true)
    .bindCell(function(item)
        if item.row % 2 == 0 then
            return mCell(item).bgColor(Color(0xffffff))--.cellSize(Size(window.width(), 200))
        else
            return mCell2(item)--.cellSize(Size(window.width(), 200))--.mainAxis()
        end
    end)
    .onSelected(function(index)
        print("onSelected-->" .. index)
    end)
    .onChangeSelected(function(to)
        print("onChangeSelected-->" .. to)
    end)
    .setTabScrollingListener(function(percent, fromIndex, toIndex)
        print("setTabScrollingListener-->" .. percent, fromIndex, toIndex)
    end)
    .setPageClickListener(function(index)
        print("setPageClickListener-->" .. index)
    end)
}

---
--- preview
local function preview()
    homeData.list = {
        { title = "111", name = "哈哈哈1" },
        { title = "222", name = "哈哈哈2" },
        { title = "333", name = "哈哈哈3" },
        { title = "444", name = "哈哈哈4" },
        { title = "555", name = "哈哈哈5" }
    }
end
Clone this wiki locally