Android Question Custom View in Custom View?

agraham

Expert
Licensed User
Is it possible to have a Custom view located on another Custom View?

I am trying to use ScrollView2D on a Custom View and get it loaded by LoadLayout, and am getting a null reference error setting the ScrollView2D width in the ScrollView2D DesignerCreateView method.

Things work fine if I do it all programmatically, Initialize the Custom View, add it to the Activity then call InitTable. I just can't get it to work from a layout.

Am I doing things in the wrong order? I don't know the order in which things are called when LoadLayout loads a Custom View in a Custom View.

B4X:
 ' Table2D Custom View Module
Private Sub Class_Globals
    Private SV As ScrollView2D
    Private TablePanel As Panel
    '...
End Sub

Public Sub DesignerCreateView (Base As Panel, Lbl As Label, Props As Map)
    TablePanel = Base
     '...
     InitTable
End Sub

Public Sub Initialize (CallbackModule As Object, EventName As String)
    Callback = CallbackModule
    Event = EventName
End Sub

Public Sub InitTable
   SV.Initialize(TablePanel.Width, TablePanel.Height - RowHeight, "SV")
   TablePanel.AddView(SV, 0, RowHeight , TablePanel.Width, TablePanel.Height - RowHeight)
End Sub

' Used by the Main Module
Sub Globals
   Dim Table1 As Table2D
   '...
End Sub

Sub Activity_Create(FirstTime As Boolean)
  LoadLayout("Layout1")
   Table1.ResizeTable(5)
   For i = 1 To 500   
       Table1.AddRow(...)
   Next
   '...
End Sub
 
Last edited:

klaus

Expert
Licensed User
Sorry, but I don't understand what exactly you want to load with LoadLayout?
I don't see any LoadLayout in your code.
 
Last edited:

agraham

Expert
Licensed User
Oops! You are right. I copied and simplified the code to better show what was going on but accidentally omitted that line - Rectified!
 

agraham

Expert
Licensed User
Ok - forget this, something weird is happening and it is probably me :( I can get a ScrollView2D on my Custom View but I can't set it up without getting errors. Needs more work!
 

agraham

Expert
Licensed User
More or less working now. :) A classic combination of ignorance, stupidity and a non-obvious bug combining to obscure what was really happening!
 

Erel

Administrator
Staff member
Licensed User
Many custom views are made of other custom views.
Do note that LoadLayout calls cannot be nested. This code will fail:
B4X:
Public Sub DesignerCreateView (Base As Panel, Lbl As Label, Props As Map)
 Base.LoadLayout(...)
End Sub
The solution is to add Sleep(0) before:
B4X:
Public Sub DesignerCreateView (Base As Panel, Lbl As Label, Props As Map)
 'do many things here
 Sleep(0)
 Base.LoadLayout(...)
End Sub
 
Top