Weird bug when adding dynamically content to newScrollView

Hi ,

Im trying to create an app using Corona, its fun and beautiful!

However, i have faced strange bugs when using newScrollView

Currently what the app is doing is pulling images (with other stuff like title and etc) from server and adding it dynamically to scrollView. Whenever the user hits the bottom of the scrollView, the app tries to bring new batch of images and append them to the end of the scrollView.

The problem is when it appends the new batch of images, the scrollView scrolls immediately to the top! The position of the scrollView doesn’t stay the same.

I really really tried many tricks to solve this issue! like after 1 second auto change the position after appending and many other tricks. Nothing worked!

My corona build is 2100 Mac version.

Regards,

Nasser

Hi Nassar,

Please try to isolate your code and add it to this thread, so that you may receive more help on this from staff or community members.

Also, remember to post your code within “lua” tags for readability.

[lua] --code [/lua]  

Thanks,

Brent Sorrentino

I have more than thousands of line of codes, but i tried to isolate what is relative here.

The logic is:

When the scollView reaches the end, append new images by downloading them first and adding to them information like title and buttons, then appending it to the scollView by  

            newsScrollView:insert(newsContainer) 

[lua] local function newsScrollListener( event )     -- In the event a scroll limit is reached...     if ( event.limitReached ) then         if ( event.direction == "up" ) then                           if( tonumber(nextPageNews) \> 0 ) then                  showNews()             end                      end     end     return true end -- Called when the scene's view does not exist: function scene:createScene( event )          newsDisplayGroup = self.view          newsScrollView = widget.newScrollView{          id = "newsScrollView",          left           = 0,          top            = 50 + statusBarPad,          width          = display.contentWidth,          height         = display.contentHeight  - (50 + statusBarPad) ,          backgroundColor = { 255, 255, 255 },    -- bgColor =\> backgroundColor                   scrollWidth    = display.contentWidth,         scrollHeight   = display.contentHeight ,          --hideBackground = true,          friction = 0.98,          listener = newsScrollListener,      }                newsDisplayGroup:insert( newsScrollView )                end function showNews()     for key, value in pairs(news) do         network.download( imageURL , "GET",  (function(event)                                                     local newsContainer = display.newContainer( newsBlockWidth, newsBlockWidth )             newsContainer.x = (newsBlockWidth  /2)  + (  newsBlockWidth  \* (newsInRow - rowIndex)   )              newsContainer.y =   (newsBlockWidth  /2) + (  newsYShift  \* newsBlockWidth )                        --[[Here is preparing each display object that will be contained in the newsContainer , like title ,image ,buttons , etc]]                                                    newsContainer:insert(downloadedImage)             newsContainer:insert( blackRect )             newsContainer:insert(halfReadSign)             newsContainer:insert(fullyReadSign)                  newsContainer:insert( moreRect )             newsContainer:insert( timeagoRect )             newsContainer:insert( newsCaption )             newsContainer:insert( newsSummary )             newsContainer:insert( moreText )             newsContainer:insert( timeagoString )                          newsScrollView:insert(newsContainer)                                                     end ) ,params, imageFileName ,system.TemporaryDirectory)                           end --  end for loop                                                       end  [/lua]

If more info is needed would be glad to give more.

Edit: [lua][/lua] Tag didn’t seem to work i don’t know why

Cant get much info out of your codes. But I would suggest that you look at this function, where you can scroll to any position in the scrollView. I use them to remember the ‘last scroll position’

http://docs.coronalabs.com/api/type/ScrollViewWidget/scrollTo.html

http://docs.coronalabs.com/api/type/ScrollViewWidget/scrollToPosition.html

Hi Nassar,

Please try to isolate your code and add it to this thread, so that you may receive more help on this from staff or community members.

Also, remember to post your code within “lua” tags for readability.

[lua] --code [/lua]  

Thanks,

Brent Sorrentino

I have more than thousands of line of codes, but i tried to isolate what is relative here.

The logic is:

When the scollView reaches the end, append new images by downloading them first and adding to them information like title and buttons, then appending it to the scollView by  

            newsScrollView:insert(newsContainer) 

[lua] local function newsScrollListener( event )     -- In the event a scroll limit is reached...     if ( event.limitReached ) then         if ( event.direction == "up" ) then                           if( tonumber(nextPageNews) \> 0 ) then                  showNews()             end                      end     end     return true end -- Called when the scene's view does not exist: function scene:createScene( event )          newsDisplayGroup = self.view          newsScrollView = widget.newScrollView{          id = "newsScrollView",          left           = 0,          top            = 50 + statusBarPad,          width          = display.contentWidth,          height         = display.contentHeight  - (50 + statusBarPad) ,          backgroundColor = { 255, 255, 255 },    -- bgColor =\> backgroundColor                   scrollWidth    = display.contentWidth,         scrollHeight   = display.contentHeight ,          --hideBackground = true,          friction = 0.98,          listener = newsScrollListener,      }                newsDisplayGroup:insert( newsScrollView )                end function showNews()     for key, value in pairs(news) do         network.download( imageURL , "GET",  (function(event)                                                     local newsContainer = display.newContainer( newsBlockWidth, newsBlockWidth )             newsContainer.x = (newsBlockWidth  /2)  + (  newsBlockWidth  \* (newsInRow - rowIndex)   )              newsContainer.y =   (newsBlockWidth  /2) + (  newsYShift  \* newsBlockWidth )                        --[[Here is preparing each display object that will be contained in the newsContainer , like title ,image ,buttons , etc]]                                                    newsContainer:insert(downloadedImage)             newsContainer:insert( blackRect )             newsContainer:insert(halfReadSign)             newsContainer:insert(fullyReadSign)                  newsContainer:insert( moreRect )             newsContainer:insert( timeagoRect )             newsContainer:insert( newsCaption )             newsContainer:insert( newsSummary )             newsContainer:insert( moreText )             newsContainer:insert( timeagoString )                          newsScrollView:insert(newsContainer)                                                     end ) ,params, imageFileName ,system.TemporaryDirectory)                           end --  end for loop                                                       end  [/lua]

If more info is needed would be glad to give more.

Edit: [lua][/lua] Tag didn’t seem to work i don’t know why

Cant get much info out of your codes. But I would suggest that you look at this function, where you can scroll to any position in the scrollView. I use them to remember the ‘last scroll position’

http://docs.coronalabs.com/api/type/ScrollViewWidget/scrollTo.html

http://docs.coronalabs.com/api/type/ScrollViewWidget/scrollToPosition.html