Android Question New ViewPager Problems

GuyBooth

Active Member
Licensed User
Longtime User
I have been using AHViewPager in one app for several years now without any problems, but when I try to update it to use the new version, I cannot get it to work. I've been having different problems with it. In this example, I load a layout with just the ViewPager on it, and then load two other layouts, one for each page. Problem seems to be that _PageCreated event doesn't fire when the Activity_Create runs and therefore doesn't load the layout files, so when Activity_Resume runs it sees the Edit Text boxes, as Uninitialized.

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

'Activity module Settings - Configuration
Sub Process_Globals
    'These global variables will be declared once when the application starts.
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 edtUserName As EditText
    Private edtUserPassword As EditText
    Private edtMySQLLink As EditText
    Private pcSettings As PageContainer
    Private vpSettings As ViewPager
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer.
    ' For example: Load the Configuration layout
    Dim sErrorMsg As String, sCodeName As String        'ignore
    sCodeName = "TMM_Preferences.A_C"
    Log(sCodeName)
    Activity.LoadLayout("settings_pager")
    pcSettings.Initialize
    ' Add pages
    For i = 0 To 1
        pcSettings.AddPage(i)
    Next
    vpSettings.PageContainer = pcSettings
    vpSettings.GotoPage(0, False)
End Sub

Sub Activity_Resume
    Dim sErrorMsg As String, sCodeName As String        'ignore
    sCodeName = "TMM_Preferences.A_R"
    Log(sCodeName)
    edtMySQLLink.SingleLine = True
    edtUserName.SingleLine = True
    edtUserPassword.SingleLine = True
    edtUserPassword.PasswordMode = True
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    Dim sErrorMsg As String, sCodeName As String    'ignore
    sCodeName = "TMM_Preferences.A_P"
    Log(sCodeName)
End Sub

Sub Activity_KeyPress (KeyCode As Int) As Boolean
    'return true if you want to consume the event
    '"True" will stop it from closing the activity.
    Return False
End Sub

Sub vpSettings_PageCreated (Position As Int, Page As VPage)
    If Not(Page.AlreadyCreated) Then
        Log("New Page " & Position & " Created.")
        If Position = 0 Then Page.Panel.LoadLayout("tmm_preferences_page1_std")
        If Position = 1 Then Page.Panel.LoadLayout("tmm_preferences_page2_std")
    Else
        Log("Page " & Position & " already exists.")
    End If
End Sub

Sub vpSettings_PageDestroyed (Position As Int, Page As VPage)
   
End Sub

Sub vpSettings_PageChanged (Position As Int)
    Log("Settings Page Changed to " & Position)
End Sub

#End Region
Looking for some explanation for this ... the old AHViewPager was working fine, but I want to stay up to date with Corwin's updates.
 

corwin42

Expert
Licensed User
Longtime User
Do all Initialization of the layout in PageCreated event.

It will fire after Activity_Resume with the pages the ViewPager needs. That are normally the current page shown, one page before and one page behind the current page. All other pages are not initialized at this stage.

So in your example the problem is that you can't access Views which are loaded in a layout in PageCreated in Activity_Resume. Put the initialization of your edittext views in the PageCreated event just after loading the layout and it should work.
 
Upvote 0

GuyBooth

Active Member
Licensed User
Longtime User
Why is the Page_Created event not triggered during the Activity_Create sub, where the Page_Add and GotoPage are called?
If I try to initialize them in the Page_Created event I get "view <Edit Text> was added with the designer. You should not initialize it" messages.
Besides, the layouts to be loaded are complex (I cut the number of views on the layout to keep this example readable) and should be created and initialized in the designer. The one in this example is extremely simple - the layouts in my complete app are complex and initializing them outside the designer seems counterintuitive.
 
Upvote 0

corwin42

Expert
Licensed User
Longtime User
Page_Created may get triggered at this times but the event is just added to the message queue and so the B4A framework will raise it after all other events are handled.

With "initialization" I mean loading a layout and setting default view parameters.

You can use the designer for createing the layout.
 
Upvote 0

GuyBooth

Active Member
Licensed User
Longtime User
Ok.
Yes that does work. I'll have to look at my "real" program and see how this affects the rest of the program flow.
Thanks
 
Upvote 0

GuyBooth

Active Member
Licensed User
Longtime User
Ok.
Yes that does work. I'll have to look at my "real" program and see how this affects the rest of the program flow.
Thanks
Well - it worked on a simple two page part of my app. Unfortunately it is not working on a more complex section.
Despite calling for everything to be set up from the Activity_Create sub, I am well through the Activity_Resume sub and the pages still have not been created (because vPager_PageCreated event has not been triggered).
So once again I run into trying to use views that have not yet been initialized.
This isn't working for me, Somehow, I need to be able to force these page setups to complete.
It's too bad. AHViewPager was working well, but it sounds as though it's on its way out (deprecated objects being removed for the new version) and I will have to find another approach.
 
Upvote 0

corwin42

Expert
Licensed User
Longtime User
I don't know your app structure so I can't help anymore here.

For your problem maybe it will help to chck if a page is initialized (layout loaded)? You can get the page from the pagecontainer and check if it is initialized and so a layout is loaded.

You can still use AHViewPager if it works for you. It was one of my most important libraries and I think I will still support it some time.
 
Upvote 0

GuyBooth

Active Member
Licensed User
Longtime User
I don't know your app structure so I can't help anymore here.

For your problem maybe it will help to chck if a page is initialized (layout loaded)? You can get the page from the pagecontainer and check if it is initialized and so a layout is loaded.

You can still use AHViewPager if it works for you. It was one of my most important libraries and I think I will still support it some time.

I think the only way I would be able to make it work would be to have a method that would allow me to force the pages to load. Whatever the trigger is now, it isn't working, at least not consistently. Knowing that the page wasn't initialized when expected doesn't help - I have too much setup that needs it to happen at the right time.
 
Upvote 0
Top