Android Question Calculate offset for adding item to ScrollView2D

Robert Valentino

Well-Known Member
Licensed User
Longtime User
When wanting to add another item to my ScrollView2D I use to use the following routine to determine where the next item should be inserted

B4X:
  'NOTE:  PanelHeight is is the height of of the Main Panel shown below

  Private    sStandings_ScrollView  As ScrollView2D

  Dim TotalHeight  As Int = 0

  Dim MainPanel  As Panel

  MainPanel.Initialize("MainPanel") 
  MainPanel.Enabled = True

  For Each v As View In sStandings_ScrollView.Panel.GetAllViewsRecursive         
  If v Is Panel Then
          TotalHeight = TotalHeight + v.Height
         End If
  Next

    sStandings_ScrollView.Panel.AddView(MainPanel, 0, TotalHeight, sStandings_ScrollView.Panel.Width, PanelHeight)

NOW this routine worked fine as long as each entry only had ONE Panel.

Things have changed and I am adding items that have multiple panels in them and ALL the sub panels were be totaled up making the entries off.

So I added a value to my Tag field of the Sub Panels that I do not want to total (because the main panel is the size I want)

B4X:
  Private    sStandings_ScrollView  As ScrollView2D

  Dim TotalHeight  As Int = 0

  Dim MainPanel  As Panel

  MainPanel.Initialize("MainPanel") 
  MainPanel.Enabled = True



  For Each v As View In sStandings_ScrollView.Panel.GetAllViewsRecursive         
  If v Is Panel Then
          Dim vTagCheck As String = v.Tag
           
          If vTagCheck.Contains("~SkipMe~") Then Continue
           
          TotalHeight = TotalHeight + v.Height
         End If
  Next

    sStandings_ScrollView.Panel.AddView(MainPanel, 0, TotalHeight, sStandings_ScrollView.Panel.Width, PanelHeight)

Now this is working just fine. But is there a BETTER way. Without storing information in the tag of panels I want to included?

What I am inserting looks something like this

B4X:
MainPanel [ScrollView Panel as parent]
       SubPanel1(has tag set to "~SkipMe~") [MainPanel as parent]
             Label_101
             Label_102
       SubPanel2(has tag set to "~SkipMe~") [MainPanel as parent]
             Label_201
             Label_202
       SubPanel3(has tag set to "~SkipMe~") [MainPanel as parent]
             Label_301
             Label_302
      SubPanel4(has tag set to "~SkipMe~") [MainPanel as parent]
             Label_401
             Label_402

So my first loop would add in the Main Panels height, then all the Sub Panel's heights making the display take up twice as much screen space then was used

WHAT I am doing works. But just seems messy.

BobVal
 

Robert Valentino

Well-Known Member
Licensed User
Longtime User
I am using the ScrollView2D as a TreeView and depending on if the above entries are toggled open or closed depends on where this item would show.

See attached photo
aStandings-160102150446.jpg


Each Entry above is a Panel with a label to show the text. Basically they are all 50dip when closed when open they will have more 50dip entries. Only the Brackets when open has a Large size. In my recursive draw routine it just calls a Draw Function that calculates where to draw.

Thinking about what you said, I could have the draw function return it's height to the recursive draw routine which would sum them up as going along and pass it on to the next draw routine - Yes... certainly a better way to do it.

I know you might not think you helped much, but just getting me to think of it in a different way makes more sense

My Recursive draw would pass where to draw, each draw routine will return (either the amount they have drawn or where the next routine should draw)

Thanks

BobVal
 
Upvote 0
Top