iOS Question Scrollview does not show up in first time run, but only second time

susu

Well-Known Member
Licensed User
Longtime User
Hi,

I'm trying to make a simple app with 3 pages.
Page1 with a button to call Page2 that hold a scroll view with 100 items. Click on each item will open Page3 that shows the selected item.

If I put code to load 100 items in Page2_appear event, the scroll view will be reload everytime I back from Page 3. Imagine if there's 300 items and the user scrolled to the end to select item 299, later he want to select item 298, he need to scroll again to the end of scrollview. It's waste of time and really annoying.

I write the code as below, I think its logical is right:

Page 1:
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public App As Application
    Public NavControl As NavigationController
    Private Page1 As Page
    Private bt As Button
End Sub

Private Sub Application_Start (Nav As NavigationController)
    NavControl = Nav
    Page1.Initialize("Page1")
    Page1.Title = "Page 1"
    Page1.RootPanel.Color = Colors.White
    NavControl.ShowPage(Page1)
End Sub

Private Sub Page1_Resize(Width As Int, Height As Int)
    bt.InitializeCustom("bt", Colors.Blue, Colors.Black)
    bt.Text = " Click Me "
    bt.CustomLabel.SetBorder(1, Colors.Black, 2)
    bt.CustomLabel.Font = Font.CreateNew(30)
    Page1.RootPanel.AddView(bt, 0, 100dip, 200dip, 80dip)
End Sub

Sub bt_Click
    ScrollViewModule.RemoveSV
    ScrollViewModule.Show
    ScrollViewModule.CreateScrollView
End Sub

Page 2 (ScrollViewModule):
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Private pg As Page
    Private sv As ScrollView
    Private btname As Button
    Public SelectedItem As Int
End Sub

Public Sub Show
    If pg.IsInitialized = False Then
        pg.Initialize("pg")
        pg.Title = "Scroll View"
        pg.RootPanel.Color = Colors.White
    End If
    Main.NavControl.ShowPage(pg)
End Sub

Public Sub CreateScrollView
        sv.Initialize("sv", pg.RootPanel.Width, 0)
        pg.RootPanel.AddView(sv, 0, 10dip, pg.RootPanel.Width, pg.RootPanel.Height-60dip)
        For i=0 To 100
            btname.InitializeCustom("btname", Colors.RGB(163,102,40), Colors.Black)
            btname.CustomLabel.SetBorder(1, Colors.Black, 2)
            btname.CustomLabel.Font = Font.CreateNew(25)
            btname.Text = "Item #" & i
            btname.Tag = i
            sv.Panel.AddView(btname, sv.width/2 - 100dip/2, i*80dip, 150dip, 80dip)
        Next
        sv.ContentWidth = pg.RootPanel.Width
        sv.ContentHeight = i * 80dip
End Sub

Private Sub btname_Click
    Dim lbt As Button
    lbt = Sender
    SelectedItem = lbt.Tag
    ResultModule.RemoveButton
    ResultModule.Show
End Sub


Public Sub RemoveSV
    If sv.IsInitialized Then
        sv.RemoveViewFromParent
    End If
End Sub

Page 3 (ResultModule):
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Private pg As Page
    Private bt As Button
End Sub

Public Sub Show
    If pg.IsInitialized = False Then
        pg.Initialize("pg")
        pg.Title = "Result"
        pg.RootPanel.Color = Colors.White
    End If
    Main.NavControl.ShowPage(pg)
    bt.InitializeCustom("bt", Colors.Blue, Colors.Black)
    bt.Text = "You Selected Item #" & ScrollViewModule.SelectedItem
    bt.CustomLabel.Font = Font.CreateNew(24)
    pg.RootPanel.AddView(bt, 50dip, 100dip, 250dip, 80dip)
End Sub

Public Sub RemoveButton
    If bt.IsInitialized Then
        bt.RemoveViewFromParent
    End If
End Sub

Question 1: Why the scrollview never load when Page2 run first time? If I back to Page1 and call Page2 again, the scrollview will show up.

Question 2: Is there better way to avoid loading the scrollview again?

Please find attachment for the code.

Thank you guys.
 

Attachments

  • scrollviewtest.zip
    2.4 KB · Views: 227

Eric Baker

Member
Licensed User
Longtime User
Answer 1:
It is being created, but the first time you call CreateScrollView, pg.RootPanel.Width is 0 (zero), because pg hasn't fired its pg_resize event yet.

In B4I, it seems, the only place where you should set the size of something seems to be in the "resize" event.

Answer 2:
I don't like how you are calling (manipulating) the ScrollViewModule from Main. You should only have ScrollViewModule.Show and let that module handle itself (self-contained) Try handling it in the resize event. You might not even have to destroy and recreate it if you already have it filled and the content doesn't change. I don't think you'll even lose position then.

(Only been using b4i for a week, take any advice with a grain of salt)
 
Upvote 0

susu

Well-Known Member
Licensed User
Longtime User
I moved code form Sub CreateScrollView to Sub pg_Resize, delete ScrollViewModule.RemoveSV and ScrollViewModule.CreateScrollView of bt_Click event. It worked.
Now I want to add new button in Page1 that call Page2 with 100 new items (from Item#200 to 300). New scrollview will never show up because it created with Item#0 to 100.

Do you have any idea?
 
Upvote 0
Top