GridView Is not automatically Scrollable

I am trying here to populate a GridView but when number of items increase, and screen not sufficient to cover them all then I need to scroll the GridView as it is not autoScrollable. So I am using ScrollView and inserting GridView in it but its not accepting it.

My code so far is 

[lua]

  display.setStatusBar( display.DefaultStatusBar )

        local widget = require( “widget” )

        local gridView = require(“gridView”)

        local scrollView = widget.newScrollView

        {

            left = 0,

            top = 0,

            width = display.contentWidth,

            height = display.contentHeight,

            id = “onBottom”,

            horizontalScrollDisabled = true,

            verticalScrollDisabled = true,

            listener = scrollListener,

        }

local photoArray = {“assets/pic1.png”,“assets/pic1.png”,“assets/pic2.png”,“assets/pic3.png”,“assets/pic4.png”,“assets/pic5.png”,“assets/pic5.png”}

local photoTextArray = {“pic7”,“pic1”,“pic2”,“pic3”,“pic4”,“pic5  Long Text Test”,“pic6”}

gridView:new(25, 50, photoArray, photoTextArray, 2, 10, 20,150, 150, gridListener)

scrollView:insert( gridView ) – this line giving error

–Process the event when user click on the grid

local function gridListener(index)

print("You select item "…index)

end

[/lua]

Hi ansaf,

What is the gridView you are using? Specifically, where is it coming from? WidgetCandy? Some other library?

Alex

Thats the whole code of my grid.lua, I downloaded it by following an example. I am just trying to insert variable number of images in it and want it to grow and become scrollable if all not fit in one screen dimension.

[lua]

local widget = require(“widget”)

    local gridView = {}

    function gridView:new(gridX, gridY, photoArray, photoTextArray, columnNumber, paddingX, paddingY, photoWidth, photoHeight, gridListener)

        local currentX = gridX

        local currentY = gridY

        function onStarButtonRelease(event)

            local btId = event.target.id

            local index = 0

            – print(btId)

            for i = 1, #photoArray do

                if(btId == “grid”…i) then

                    index = i

                    break

                end

            end

            gridListener(index)

        end            

        function drawGrid()

            for i = 1, #photoArray do

                local fontSize = 24

                                                 --print(i)

                gridView.gridObject = widget.newButton{

                    id = “grid”…i,

                    default = photoArray[i], --For Widget V2.0, change the default to defaultFile

                    left = currentX,

                    top = currentY,        

                    width = photoWidth, height = photoHeight,                    

                    onRelease = onStarButtonRelease

                }        

                local r = 0

                gridView.roundedRect = display.newRoundedRect( currentX, currentY + photoHeight - 35, photoWidth, 35, r )

                gridView.roundedRect:setFillColor( 55, 55, 55, 190 )    

                --Limit the label length

                --Determine the longest length of the label string

                local bestStringLength = photoWidth / (fontSize/2) - 1

                if(string.len(photoTextArray[i]) > bestStringLength) then

                    photoTextArray[i] = string.sub( photoTextArray[i], 0, bestStringLength)

                end

                

                local textPosX = photoWidth/2 - (fontSize/2)*string.len(photoTextArray[i])/2

                gridView.textObject = display.newText( photoTextArray[i], currentX + textPosX, currentY + photoHeight - 33, native.systemFontBold, fontSize )

                gridView.textObject:setTextColor( 255,255,255 )                

                --Update the position of the next item

                currentX = currentX + photoWidth + paddingX

                if(i % columnNumber == 0) then

                    currentX = gridX

                    currentY = currentY + photoHeight + paddingY

                end        

            end

        end

        drawGrid()

    end    

    return gridView

[/lua]

Hi ansaf,

What is the gridView you are using? Specifically, where is it coming from? WidgetCandy? Some other library?

Alex

Thats the whole code of my grid.lua, I downloaded it by following an example. I am just trying to insert variable number of images in it and want it to grow and become scrollable if all not fit in one screen dimension.

[lua]

local widget = require(“widget”)

    local gridView = {}

    function gridView:new(gridX, gridY, photoArray, photoTextArray, columnNumber, paddingX, paddingY, photoWidth, photoHeight, gridListener)

        local currentX = gridX

        local currentY = gridY

        function onStarButtonRelease(event)

            local btId = event.target.id

            local index = 0

            – print(btId)

            for i = 1, #photoArray do

                if(btId == “grid”…i) then

                    index = i

                    break

                end

            end

            gridListener(index)

        end            

        function drawGrid()

            for i = 1, #photoArray do

                local fontSize = 24

                                                 --print(i)

                gridView.gridObject = widget.newButton{

                    id = “grid”…i,

                    default = photoArray[i], --For Widget V2.0, change the default to defaultFile

                    left = currentX,

                    top = currentY,        

                    width = photoWidth, height = photoHeight,                    

                    onRelease = onStarButtonRelease

                }        

                local r = 0

                gridView.roundedRect = display.newRoundedRect( currentX, currentY + photoHeight - 35, photoWidth, 35, r )

                gridView.roundedRect:setFillColor( 55, 55, 55, 190 )    

                --Limit the label length

                --Determine the longest length of the label string

                local bestStringLength = photoWidth / (fontSize/2) - 1

                if(string.len(photoTextArray[i]) > bestStringLength) then

                    photoTextArray[i] = string.sub( photoTextArray[i], 0, bestStringLength)

                end

                

                local textPosX = photoWidth/2 - (fontSize/2)*string.len(photoTextArray[i])/2

                gridView.textObject = display.newText( photoTextArray[i], currentX + textPosX, currentY + photoHeight - 33, native.systemFontBold, fontSize )

                gridView.textObject:setTextColor( 255,255,255 )                

                --Update the position of the next item

                currentX = currentX + photoWidth + paddingX

                if(i % columnNumber == 0) then

                    currentX = gridX

                    currentY = currentY + photoHeight + paddingY

                end        

            end

        end

        drawGrid()

    end    

    return gridView

[/lua]