Android Question [Solved] B4XDrawer and B4xPages in class

pliroforikos

Active Member
Licensed User
Hello

I'm trying to make a class to use a B4XDrawer in several B4XPages.
I've made a small project but it's not working.

After 2-3 clicks in menu it stacks for some time and then continues for a click and stucks again.
Please can anyone check this?

Thank you

The class:
Sub Class_Globals
    Private Root As B4XView 'ignore
    Private xui As XUI

    'Drawer Menu
    Private drMen As B4XDrawer
    Private HamburgerIcon As B4XBitmap
   
    Private MenuItems As ListView
    Private Menus As List
    Private stringModuleName  As String
    Dim cs As CSBuilder
End Sub


'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(page As String, Parent As B4XView)
    stringModuleName = page
    drMen.Initialize(Me, "Drawer", Parent, 200dip)
   
    Menus.Initialize
    Menus.AddAll (Array As String("Page1", "Page2", "Page3"))
   
    drMen.LeftPanel.LoadLayout("drawerMenu")
    drMen.CenterPanel.LoadLayout(page)
    HamburgerIcon = xui.LoadBitmapResize(File.DirAssets, "hamburger.png", 32dip, 32dip, True)

'    Place items one by one with icons
'    MenuItems.SingleLineLayout.Label.TextSize = 20
'    MenuItems.AddSingleLine( cs.Initialize.Typeface(Typeface.MATERIALICONS).Size(22).VerticalAlign(3dip).Append(Chr(0xE145)).PopAll.Append("  Page 1") )
'    MenuItems.AddSingleLine( cs.Initialize.Typeface(Typeface.MATERIALICONS).Size(22).VerticalAlign(3dip).Append(Chr(0xF080)).PopAll.Append("  Page 2") )
'    MenuItems.AddSingleLine( cs.Initialize.Typeface(Typeface.MATERIALICONS).Size(22).VerticalAlign(3dip).Append(Chr(0xF013)).PopAll.Append("  Page 3") )

'    Use list with menu items
    For i = 0 To Menus.Size -1
        MenuItems.AddSingleLine(Menus.Get(i))
    Next

End Sub


Public Sub pageAppear
    #if B4A
    Sleep(0)
    B4XPages.GetManager.ActionBar.RunMethod("setDisplayHomeAsUpEnabled", Array(True))
    Dim bd As BitmapDrawable
    bd.Initialize(HamburgerIcon)
    B4XPages.GetManager.ActionBar.RunMethod("setHomeAsUpIndicator", Array(bd))
    #End If
End Sub


Public Sub closePage As Boolean
    #if B4A
    If Main.ActionBarHomeClicked Then
        drMen.LeftOpen = Not(drMen.LeftOpen)
        Return False
    End If
    'back key
    If drMen.LeftOpen Then
        drMen.LeftOpen = False
        Return False
    End If
    #end if
    Return True
End Sub


Public Sub resize(Width As Int, Height As Int)
    drMen.Resize(Width, Height)
End Sub


'App menu
Public Sub MenuItems_ItemClick (Position As Int, Value As Object)
'    Dim p As Panel = Sender
    drMen.LeftOpen = Not(drMen.LeftOpen)
    Select Value
        Case "Page1"
            If stringModuleName <> "Page1"  Then
                Log("page1 clicked")
                B4XPages.ShowPage("mainpage")
'                B4XPages.ShowPageandRemovePreviousPages("MainPage")
            End If
        Case "Page2"
            If stringModuleName <> "Page2"  Then
                Log("page2 clicked")
                B4XPages.ShowPage("Page2")
'                B4XPages.ShowPageandRemovePreviousPages("Page2")
            End If
        Case "Page3"
            If stringModuleName <> "Page3"  Then
                Log("page3 clicked")
                B4XPages.ShowPage("Page3")
'                B4XPages.ShowPageandRemovePreviousPages("Page3")
            End If
    End Select
End Sub

MainPage:
'Ctrl + click to export as zip: ide://run?=&File=%B4X%\Zipper.jar&Args=Project.zip
Sub Class_Globals
    Private Root As B4XView 'ignore
    Private xui As XUI 'ignore
    Public Page2 As B4XPage2
    Public Page3 As B4XPage3
   
    Private drawer As menuDrawer
End Sub

'You can add more parameters here.
Public Sub Initialize

End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    'load the layout to Root
   
    drawer.Initialize("frmPage1", Root)
    B4XPages.SetTitle(Me, "Page1")
   
    Page2.Initialize
    B4XPages.AddPage("Page2", Page2)
    Page3.Initialize
    B4XPages.AddPage("Page3", Page3)
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub B4XPage_Appear
    drawer.pageAppear
End Sub

Private Sub B4XPage_CloseRequest As ResumableSub
    Return drawer.closePage
End Sub

Private Sub B4XPage_Disappear
    #if B4A
    B4XPages.GetManager.ActionBar.RunMethod("setHomeAsUpIndicator", Array(0))
    #end if
End Sub


Private Sub B4XPage_Resize (Width As Int, Height As Int)
    drawer.resize(Width, Height)
End Sub

Page 2 and 3 is similar to 1
 

Attachments

  • Project.zip
    197.5 KB · Views: 170
Last edited:

pliroforikos

Active Member
Licensed User
It happens because the drawer adds a special panel behind the root panel.
If you need to add the drawer to multiple pages then it is better to add it in the Main activity. It will be available on all pages.
Goodmorning Erel and thanks for the reply. In this thread you are saying that using it in main
It is not cross platform and isn't flexible.

A better approach is to implement all the drawer related code in a class and create a class instance in each of the pages.

Especialy if i need 1 or 2 pages without drawer can i use it in main?
Thank you
 
Upvote 0

pliroforikos

Active Member
Licensed User
Thank you very much Erel.
Here is my final code in case someone wants to use it.

3 pages example with:
  • customListView as menu items
  • icons taken from materialdesignicons-webfont.ttf
  • Logo at the top

drawer.png
 

Attachments

  • Project.zip
    464.4 KB · Views: 201
Last edited:
Upvote 0

mmieher

Active Member
Licensed User
Longtime User
Thank you very much Erel.
Here is my final code in case someone wants to use it.

3 pages example with:
  • customListView as menu items
  • icons taken from materialdesignicons-webfont.ttf
  • Logo at the top

View attachment 126186

THIS POST should be one of the first things one sees when searching "B4XPages Menu" in the forum.

I have been looking (incorrectly I guess) for over a year to find a simple example of the B4XDrawer that includes a menu. One menu.

Thank you so much!

Now to clean up and sort out the five versions of the "Three Pages Example" I have.
 
Upvote 0
Top