ScrollView scroll issue

Im having this issue and couldn’t figure out how to solve. In my scrollview when i scroll down it jumps up back to default position. This is my example code:
 

– 

– main.lua

– 


–import the widget library

local widget = require( “widget” )

–Create a group to hold our widgets & images

local widgetGroup = display.newGroup()

– Create toolbar to go at the top of the screen

local titleBar = display.newRect( display.contentCenterX, 0, display.contentWidth, 50 )

titleBar:setFillColor( 0,174/255,239/255)

titleBar.y = display.screenOriginY + titleBar.contentHeight * 0.5

– create embossed text to go on toolbar

local titleText = display.newEmbossedText( “Title”, display.contentCenterX, titleBar.y, native.systemFontBold, 28 )

–Scroll View

– ScrollView listener

local function scrollListener( event )

    local phase = event.phase

    if ( phase == “began” ) then print( “Scroll view was touched” )

    elseif ( phase == “moved” ) then print( “Scroll view was moved” )

    elseif ( phase == “ended” ) then print( “Scroll view was released” )

    end

    – In the event a scroll limit is reached…

    if ( event.limitReached ) then

        if ( event.direction == “up” ) then print( “Reached bottom limit” )

        elseif ( event.direction == “down” ) then print( “Reached top limit” )

        end

    end

    return true

end

– Create the widget

local scrollView = widget.newScrollView

{

    top = 0,

    left = 0,

    width = display.viewableContentWidth + 1,

    height = display.viewableContentHeight + 900,

    scrollHeight = 2000,

    horizontalScrollDisabled = true,

    listener = scrollListener

}

scrollView:toBack()

–Buttons

–Button1

– Function to handle button events

local function handleButtonEvent( event )

    if ( “ended” == event.phase ) then

        print( “Button was pressed and released” )

    end

     local phase = event.phase

    if ( phase == “moved” ) then

        local dy = math.abs( ( event.y - event.yStart ) )

        – If the touch on the button has moved more than 10 pixels,

        – pass focus back to the scroll view so it can continue scrolling

        if ( dy > 10 ) then

            scrollView:takeFocus( event )

        end

    end

    return true

end

local button1 = widget.newButton

{

    width = display.viewableContentWidth,

    height = 80,

    label = “button”,

    onEvent = handleButtonEvent

    

}

– Center the button

button1.x = display.contentCenterX

button1.y = 90

– Change the button’s label text

button1:setLabel( “Button1” )

scrollView:insert(button1)

—Button2

– Function to handle button events

local function handleButton2Event( event )

    if ( “ended” == event.phase ) then

        print( “Button was pressed and released” )

    end

     local phase = event.phase

    if ( phase == “moved” ) then

        local dy = math.abs( ( event.y - event.yStart ) )

        – If the touch on the button has moved more than 10 pixels,

        – pass focus back to the scroll view so it can continue scrolling

        if ( dy > 10 ) then

            scrollView:takeFocus( event )

        end

    end

    return true

end

local button2 = widget.newButton

{

    width = display.viewableContentWidth,

    height = 80,

    label = “button”,

    onEvent = handleButton2Event

    

}

– Center the button

button2.x = display.contentCenterX

button2.y = 160

– Change the button’s label text

button2:setLabel( “Button 2” )

scrollView:insert(button2)

—Button3

– Function to handle button events

local function handleButton3Event( event )

    if ( “ended” == event.phase ) then

        print( “Button was pressed and released” )

    end

     local phase = event.phase

    if ( phase == “moved” ) then

        local dy = math.abs( ( event.y - event.yStart ) )

        – If the touch on the button has moved more than 10 pixels,

        – pass focus back to the scroll view so it can continue scrolling

        if ( dy > 10 ) then

            scrollView:takeFocus( event )

        end

    end

    return true

end

local button3 = widget.newButton

{

    width = display.viewableContentWidth,

    height = 80,

    label = “button”,

    onEvent = handleButton3Event

    

}

– Center the button

button3.x = display.contentCenterX

button3.y = 230

– Change the button’s label text

button3:setLabel( “Button 3” )

scrollView:insert(button3)

—Button4

– Function to handle button events

local function handleButton4Event( event )

    if ( “ended” == event.phase ) then

        print( “Button was pressed and released” )

    end

     local phase = event.phase

    if ( phase == “moved” ) then

        local dy = math.abs( ( event.y - event.yStart ) )

        – If the touch on the button has moved more than 10 pixels,

        – pass focus back to the scroll view so it can continue scrolling

        if ( dy > 10 ) then

            scrollView:takeFocus( event )

        end

    end

    return true

end

local button4 = widget.newButton

{

    width = display.viewableContentWidth,

    height = 80,

    label = “button”,

    onEvent = handleButton4Event

    

}

– Center the button

button4.x = display.contentCenterX

button4.y = 300

– Change the button’s label text

button4:setLabel( “Button 4” )

scrollView:insert(button4)

—Button5

– Function to handle button events

local function handleButton5Event( event )

    if ( “ended” == event.phase ) then

        print( “Button was pressed and released” )

    end

     local phase = event.phase

    if ( phase == “moved” ) then

        local dy = math.abs( ( event.y - event.yStart ) )

        – If the touch on the button has moved more than 10 pixels,

        – pass focus back to the scroll view so it can continue scrolling

        if ( dy > 10 ) then

            scrollView:takeFocus( event )

        end

    end

    return true

end

local button5 = widget.newButton

{

    width = display.viewableContentWidth,

    height = 80,

    label = “button”,

    onEvent = handleButton5Event

    

}

– Center the button

button5.x = display.contentCenterX

button5.y = 370

– Change the button’s label text

button5:setLabel( “Button 5” )

scrollView:insert(button5)

—Button6

– Function to handle button events

local function handleButton6Event( event )

    if ( “ended” == event.phase ) then

        print( “Button was pressed and released” )

    end

     local phase = event.phase

    if ( phase == “moved” ) then

        local dy = math.abs( ( event.y - event.yStart ) )

        – If the touch on the button has moved more than 10 pixels,

        – pass focus back to the scroll view so it can continue scrolling

        if ( dy > 10 ) then

            scrollView:takeFocus( event )

        end

    end

    return true

end

local button6 = widget.newButton

{

    width = display.viewableContentWidth,

    height = 80,

   label = “button”,

    onEvent = handleButton6Event

    

}

– Center the button

button6.x = display.contentCenterX

button6.y = 440

– Change the button’s label text

button6:setLabel( “Button 6” )

scrollView:insert(button6)

–Button7

– Function to handle button events

local function handleButton7Event( event )

    if ( “ended” == event.phase ) then

        print( “Button was pressed and released” )

    end

     local phase = event.phase

    if ( phase == “moved” ) then

        local dy = math.abs( ( event.y - event.yStart ) )

        – If the touch on the button has moved more than 10 pixels,

        – pass focus back to the scroll view so it can continue scrolling

        if ( dy > 10 ) then

            scrollView:takeFocus( event )

        end

    end

    return true

end

local button7 = widget.newButton

{

    width = display.viewableContentWidth,

    height = 80,

    label = “button”,

    onEvent = handleButton7Event

    

}

– Center the button

button7.x = display.contentCenterX

button7.y = 510

– Change the button’s label text

button7:setLabel( “Button 7” )

scrollView:insert(button7)

I did a bit of research and found that the scrollHeight value is not being set.

local MY_SCROLLVIEW = widget.newScrollView
{
    top=0,
    left=0,
    width = w+1,
    height = h+600,
    scrollWidth = w,
    scrollHeight = 2000,
    horizontalScrollDisabled = true,
–    listener = scrollListener
}
print(MY_SCROLLVIEW.height,MY_SCROLLVIEW.scrollHeight)

The height is set but the scrollHeight is nil…

Any thoughts?

K

@ahmet6yilmaz,

Which device (or Simulator view) are you testing in? In your code, you set the height to be the “viewableContentHeight”+900, so if that value is over 1100, your scrollHeight of 2000 is already less than the height of your widget. Instead, try setting the height to a value that will fit on the screen, and adjusting the scrollHeight from there.

@ksteinberg,

Simply print()-ing the “.scrollHeight” value and getting “nil” doesn’t necessarily mean that it’s not being assigned. When you create a widget, the parameters that you supply are not all necessarily assigned to the actual widget object. Some of them are passed internally to sub-objects/tables of the widget, so simply printing them won’t always reveal the value you’ve set for them.

Hope this helps,

Brent

I did a bit of research and found that the scrollHeight value is not being set.

local MY_SCROLLVIEW = widget.newScrollView
{
    top=0,
    left=0,
    width = w+1,
    height = h+600,
    scrollWidth = w,
    scrollHeight = 2000,
    horizontalScrollDisabled = true,
–    listener = scrollListener
}
print(MY_SCROLLVIEW.height,MY_SCROLLVIEW.scrollHeight)

The height is set but the scrollHeight is nil…

Any thoughts?

K

@ahmet6yilmaz,

Which device (or Simulator view) are you testing in? In your code, you set the height to be the “viewableContentHeight”+900, so if that value is over 1100, your scrollHeight of 2000 is already less than the height of your widget. Instead, try setting the height to a value that will fit on the screen, and adjusting the scrollHeight from there.

@ksteinberg,

Simply print()-ing the “.scrollHeight” value and getting “nil” doesn’t necessarily mean that it’s not being assigned. When you create a widget, the parameters that you supply are not all necessarily assigned to the actual widget object. Some of them are passed internally to sub-objects/tables of the widget, so simply printing them won’t always reveal the value you’ve set for them.

Hope this helps,

Brent