Xlua开发笔记:ListScrollView

Posted 抚琴一生

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Xlua开发笔记:ListScrollView相关的知识,希望对你有一定的参考价值。

实现一个列表

要求:展示数据列表,最多展示三个

1、unity组件

2、代码实现

function buildListView(dataList)
	local scrollView= GameObject.Find('ListScrollView')
    local listContent = GameObject.Find('ListContent')
    local sizeDelta = listContent:GetComponent('RectTransform').sizeDelta
    -- 列表项高度
    local itemHeight = 30
    -- 参照物
    local gameObj = GameObject.Find('AccountInputField')
    local gameObjPosition = gameObj.transform.localPosition
    local gameObjSizeDelta = gameObj:GetComponent('RectTransform').sizeDelta

    if #dataList < 3 then
        local listY = gameObjPosition.y - gameObjSizeDelta.y / 2 - (itemHeight * #dataList) / 2
        scrollView.transform.localPosition = unityEngine.Vector3(gameObjPosition.x, listY, 0)
        scrollView:GetComponent("RectTransform").sizeDelta = unityEngine.Vector2(scrollView:GetComponent("RectTransform").sizeDelta.x, (#dataList* itemHeight))
    else
        local listY = gameObjPosition.y - gameObjSizeDelta.y / 2 - 50
        scrollView.transform.localPosition = unityEngine.Vector3(gameObjPosition.x, listY, 0)
        scrollView:GetComponent("RectTransform").sizeDelta = unityEngine.Vector2(scrollView:GetComponent("RectTransform").sizeDelta.x, (#dataList* itemHeight))
    end
    listContent:GetComponent("RectTransform").sizeDelta = unityEngine.Vector2(sizeDelta.x, 30*(#dataList))

    local count = #dataList
    if itemBtn == nil then
        itemBtn = GameObject.Find("ItemButton")
    elseif itemBtn.activeSelf == false then
        itemBtn:SetActive(true)
    end
    if itemText == nil then
        itemText = GameObject.Find("ListText")
        itemText:GetComponent("Text").color = toColor(TEXTFIELD_TEXTCOLOR)
    elseif itemText.activeSelf == false then
        itemText:SetActive(true)
    end

    for i=1,count do
        -- 清除原先item
        local item = GameObject.Find("listBtn"..i.."")
        if item then
            GameObject.Destroy(item)
        end
        local itemTxt = GameObject.Find("listTxt"..i.."")
        if itemTxt then
            GameObject.Destroy(itemTxt)
        end
    end
    for i=1,count do
        -- 计算item的y值
        local itemYPoint = (-itemHeight/2-itemHeight*(i-1))

        local txt = GameObject.Instantiate(itemText,listContent.transform)
        txt.name = "listTxt"..i..""
        txt:GetComponent("Text").text = dataList[i]
        txt.transform.localPosition = unityEngine.Vector3(txt.transform.localPosition.x ,itemYPoint,0)


        local item = GameObject.Instantiate(itemBtn,listContent.transform)
        item.name = "listBtn"..i..""
        item.transform.localPosition = unityEngine.Vector3(item.transform.localPosition.x,itemYPoint,0)

        item:GetComponent("Button").onClick:AddListener(function()
            -- 列表item点击事件
            onClicklListItem(dataList,i)
            scrollView:SetActive(false)
        end)
    end

    itemBtn:SetActive(false)
    itemText:SetActive(false)
end

以上是关于Xlua开发笔记:ListScrollView的主要内容,如果未能解决你的问题,请参考以下文章

Xlua开发笔记:ListScrollView

Xlua开发笔记:ListScrollView

Xlua开发笔记:计时器

Xlua开发笔记:计时器

Xlua开发笔记:计时器

Xlua开发笔记:网络图片加载