Android Question [Solved] TabStripViewPager Problem - Use same views

pliroforikos

Active Member
Licensed User
Hello, i 'm trying to make a TabStripViewPager with views taken from a class. I wanted like this because is more elegant code and also, i need to use only 1 form and loaded to every tab with different data. So made the code bellow.

In the form there are a label and a CLV.
The problem is that i can see only the last form and nothing else.
I tried to make different .bal files for every form without result.

So i you please have a look to my code to tell me your opinion i'll be grateful.

Class TabStrip:
Sub Class_Globals
    Public xui As XUI
    Public Root As B4XView

    'Public
    Public label1 As Label
    Public panel1 As Panel
    Public CustomListView1 As CustomListView
End Sub


'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(Parent As B4XView)
    Root = Parent
End Sub


Public Sub loadLayout(pos As Int) As String
    Dim p As Panel
    p.Initialize("")
    Root.AddView(p, 0, 10%y, 100%x, 100%y)
    p.LoadLayout("frm"&pos)
    Return("frm"&pos)
'    Return("frm0")
  
End Sub

Public Sub getPanel As B4XView
    Return panel1   
End Sub

Public Sub loadCLV(pos As Int)
    CustomListView1.Clear
    For i = 0 To 30 Step pos+1
        CustomListView1.AddTextItem($"Item #${i}"$, "")
    Next
End Sub


Private Sub CustomListView1_ItemClick (Index As Int, Value As Object)
  
End Sub


B4XMainPage:
#Region Shared Files
#CustomBuildAction: folders ready, %WINDIR%\System32\Robocopy.exe,"..\..\Shared Files" "..\Files"
'Ctrl + click to sync files: ide://run?file=%WINDIR%\System32\Robocopy.exe&args=..\..\Shared+Files&args=..\Files&FilesSync=True
#End Region

'Ctrl + click to export as zip: ide://run?File=%B4X%\Zipper.jar&Args=Project.zip


Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
  
    Private tabstrip1 As TabStrip
    Private myTabs(3) As tabStribs
    Private Panel1 As Panel
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    For i = 0 To myTabs.Length-1
        myTabs(i).Initialize(Root)
    Next
  
    For i = 0 To myTabs.Length-1
        tabstrip1.LoadLayout(myTabs(i).loadLayout(i), "Page"&i)
        myTabs(i).label1.text = "Page "  & i
        myTabs(i).panel1.Color = xui.Color_RGB(150+10*i,100+10*i,10+10*i)
        myTabs(i).loadCLV(i)
    Next
End Sub

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


Sub TabStrip1_PageSelected (Position As Int)
    Log($"Current page: ${Position}"$)
    myTabs(Position).label1.text = "Page "  & Position
    myTabs(Position).panel1.Color = xui.Color_RGB(150+10*Position,100+10*Position,10+10*Position)
    myTabs(Position).loadCLV(Position)
End Sub
 

Attachments

  • Project.zip
    21.1 KB · Views: 149

pliroforikos

Active Member
Licensed User
????????

Whouaou, you just used a form with a panel and passed panel to class to load layouts. Just spend 2 days searching...

I am grateful for your help. I'll try to write it to my project and i'll tell you the results.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I wanted like this because is more elegant code and also, i need to use only 1 form and loaded to every tab with different data.
I don't think you need the class you made to do what you want. If you think it makes it elegant, in my opinion all it does is make it complicated. You do not need all three layouts. All you need is frm0.bal. And of course MainPage.bal that has tabstrip1. Here is you full code you need in your project.
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")   'has Tabstrip1
    For j=0 To 2
        TabStrip1.LoadLayout("frm0", "PAGE "&j)     'has customlistview1
        CustomListView1.AsView.Tag = CustomListView1
        For i = 0 To 30 Step j+1
            CustomListView1.AddTextItem($"Item ${i}"$, i)
        Next    
    Next    
End Sub
 
Upvote 0

pliroforikos

Active Member
Licensed User
One of great mysteries of the world.
"Why something i did so many times without success suddenly works".

That's the code i wrote from the first time. And now it works....
Do you know if there are there any limitations for the views? Can i use all the usual views?

Thank you very much.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Why do we need this?
You really don't need it here, unless you are iterating over the views.
Can i use all the usual views?
Yes. try it with Label1. like this for instance:

Private Label1 as B4XView
B4X:
 For i = 0 To 30 Step j+1
            CustomListView1.AddTextItem($"Item ${i}"$, i)
        Next
        Label1.Text = $"I am a label with text: ${j}"$
        Label1.Color =xui.Color_Red
You will get different values for each tabstrip page.
It will be interesting to see your original code that you said it worked
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I don't think you need the class you made to do what you want. If you think it makes it elegant, in my opinion all it does is make it complicated.
Having multiple classes can be very useful, to manage data (and Views) relating to a single "topic"; otherwise, in this case, you would have to manage everything together and write everything in the B4XMainPage. After all, it is the same concept as the B4XPages.
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…