Android Question Load Layout with AHViewPager and DSTabLayout

Alexander Stolte

Expert
Licensed User
Longtime User
Hello guys,

I use this library: https://www.b4x.com/android/forum/threads/designsupport-additional-material-design-components.58893/

This is the completet code:

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Public MyColors() As Int = Array As Int(Colors.RGB(74,74,82), Colors.RGB(132,121,121), Colors.RGB(152,99,122), Colors.RGB(162,116,92))
    Public ColorIndex As Int
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Private VP As AHViewPager
    Private PC As AHPageContainer
    Private TabLayout As DSTabLayout
    Private ToolBar As ACToolBarLight

    Dim p As Panel
    Dim p1 As Panel

    Dim pnl_content001 As Panel

    Private clv1 As CustomListView
    Dim btn_addcontent As ImageView
    Dim btn_vote As ImageView

'    Dim TabStrip1 As TabStrip
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Activity.LoadLayout("frm_main")
    PC.Initialize


   
    p.Initialize("")
    p1.Initialize("")
    p.Color = Colors.RGB(Rnd(0,256), Rnd(0,256), Rnd(0,256))
    'TabStrip1.LoadLayout("frm_content", "Chatrooms")
    'TabStrip1.LoadLayout("frm_settings", "+20000")

    p.RemoveView
    p.LoadLayout("frm_content")
    p1.RemoveView
    p1.LoadLayout("frm_settings")


    PC.AddPage(p, "Location")
    PC.AddPage(p1, "Settings")



    VP.PageContainer = PC


    Dim ac As AppCompat
    TabLayout.Color = ac.GetThemeAttribute("colorPrimary")

    TabLayout.SetViewPager(VP)

    TabLayout.SetTabText(0, "Location")
    TabLayout.SetTabText(1, "Settings")

    For i = 1 To 10
        clv1.Add(CreateListItem($"Item #${i}"$, clv1.AsView.Width, 160dip), 160dip, $"Item #${i}"$)
    Next

End Sub

Sub CreateListItem(Text As String, Width As Int, Height As Int) As Panel
    Dim pan As Panel
    pan.Initialize("")
    'we need to add the panel to a parent to set its dimensions. It will be removed after the layout is loaded.
    Activity.AddView(pan, 0, 0, Width, Height)
    pan.LoadLayout("frm_compo")
    pan.RemoveView
    'label1 and button1 will point to the last added views.

    'p.Color = MyColors(ColorIndex)
    pnl_content001.Color = MyColors(ColorIndex)
    ColorIndex = (ColorIndex + 1) Mod MyColors.Length

    Return pan


End Sub

why i dont see the cutomlistview with his entrys?

The completet project can you Download here: https://1drv.ms/u/s!AupZtTpO80tCgpYgvzPsLIhAImFfMg

thanks for help! :)
 

corwin42

Expert
Licensed User
Longtime User
Upvote 0

corwin42

Expert
Licensed User
Longtime User
Ah, I see.

I guess the problem is because you load a layout to the page panels just before they are added to the PageContainer. Don't you get a warning message in the logs about this?

Try to first add the panels to the Container and then load a layout.

The recommended place where to load the layout to the pages is the PageCreated event.
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
No change, still empty:

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Activity.LoadLayout("frm_main")
    PC.Initialize
   

       
    p.Initialize("")
    p1.Initialize("")
    p.Color = Colors.RGB(Rnd(0,256), Rnd(0,256), Rnd(0,256))
    'TabStrip1.LoadLayout("frm_content", "Chatrooms")
    'TabStrip1.LoadLayout("frm_settings", "+20000")
   
   
   
   
    PC.AddPage(p, "Location")
    PC.AddPage(p1, "Settings")
   
   
   
    VP.PageContainer = PC
   
   
    Dim ac As AppCompat
    TabLayout.Color = ac.GetThemeAttribute("colorPrimary")
   
    TabLayout.SetViewPager(VP)
   
    TabLayout.SetTabText(0, "Location")
    TabLayout.SetTabText(1, "Settings")
   
   
   
    p.RemoveView
    p.LoadLayout("frm_content")
    p1.RemoveView
    p1.LoadLayout("frm_settings")
   
    For i = 1 To 10
        clv1.Add(CreateListItem($"Item #${i}"$, clv1.AsView.Width, 160dip), 160dip, $"Item #${i}"$)
    Next

End Sub

The recommended place where to load the layout to the pages is the PageCreated event.

Can you please make an example for it? :)
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
Is there another way to load the activity? Because with the panels does not want to work, since these can not be updated

B4X:
Sub VP_PageCreated (Position As Int, Page As Object)
    p.RemoveView
    p.LoadLayout("frm_content")
    p1.RemoveView
    p1.LoadLayout("frm_settings")
End Sub

It is logical why this does not work:
B4X:
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
~w:1004,main,46
main_activity_create (B4A line: 78)
clv1.Add(CreateListItem($"Item #${i}"$, clv1.AsV
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object anywheresoftware.b4a.AbsObjectWrapper.getObject()' on a null object reference
    at anywheresoftware.b4a.samples.customlistview.customlistview._asview(customlistview.java:57)
    at com.tabhost.main._activity_create(main.java:425)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:179)
    at com.tabhost.main.afterFirstLayout(main.java:102)
    at com.tabhost.main.access$000(main.java:17)
    at com.tabhost.main$WaitForLayout.run(main.java:80)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
It works:

B4X:
Sub VP_PageCreated (Position As Int, Page As Object)
    p.LoadLayout("frm_content")
    p1.LoadLayout("frm_settings")
        For i = 1 To 10
        clv1.Add(CreateListItem($"Item #${i}"$, clv1.AsView.Width, 160dip), 160dip, $"Item #${i}"$)
    Next
End Sub
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
@corwin42

I have the same problem. I have 3 Tabs, 3 Layouts and 3 Panels to load this 3 Layouts.

My problem is, I need to add the 3 tabs at the same time. But "VP_PageCreated" is calling every time if i scroll to the next tab and the result is, that the layouts are removed.

and this Code is not working, just for the first tab. This is a stripped down version of the project code:

B4X:
Sub Globals
    Private TabLayout As DSTabLayout
    Private VP As AHViewPager
    Private PC As AHPageContainer
   
    Dim pnl_likemain As Panel
    Dim pnl_comm As Panel
    Dim pnl_newmain As Panel
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("frm_main")

PC.Initialize

pnl_comm.Initialize("")
        pnl_newmain.Initialize("")
        pnl_likemain.Initialize("")

    PC.AddPageAt(pnl_comm,"",0)
PC.AddPageAt(pnl_newmain,"",1)
    PC.AddPageAt(pnl_likemain,"",2)

VP.PageContainer = PC
   
    

TabLayout.Color = Colors.Black
   
    TabLayout.SetViewPager(VP)
   
    TabLayout.SetTabText(0,"Comments")
    TabLayout.SetTabText(1,"Newest")
    TabLayout.SetTabText(2,"Likes")

Wait For VP_PageCreated

pnl_comm.LoadLayout("frm_maincontent_mostcomment")

    pnl_newmain.LoadLayout("frm_maincontent")
    
    pnl_likemain.LoadLayout("frm_maincontent_mostlikes")


'after that comes the routine to connect to the database records and the data then fetches, so I need the 3 tabs concurrently.

End Sub

Sub VP_PageCreated (Position As Int, Page As Object)

End Sub

And this is the result:

The first tab is normal, but tab 2 and 3 are empty and white.

what am I doing wrong?
 
Upvote 0

corwin42

Expert
Licensed User
Longtime User
Waiting for PageCreated event is wrong since this event is fired for each page.

In your code when the first PageCreated event is fired the Activity_Create Sub is resumed. The first page is created and the LoadLayout works. The other two pages are not created and the LoadLayouts will not work as expected.
 
Upvote 0
Top