Android Question How to duplicate views in code

toby

Well-Known Member
Licensed User
Longtime User
I use customlistviews to show grouped data with horizontal orientation. Currently there ae 14 groups and I use a label and a CustomListView to show each group of data; the views at the bottom are difficult to work with because they're too small in size to be readable. I think there must be a better way since each group of views are identical except the data shown. How do I have one set of views (label and clv) in the Designer and duplicate them in code for other groups instead of having 14 sets? Test project attached.

TIA

layout.jpgdesigner.layout.jpg i
 

Attachments

  • horizontalClvs.zip
    26.5 KB · Views: 92

MicroDrie

Well-Known Member
Licensed User
You can try to simplify the code by placing an equivalent code in a sub and adding a unique tag in that sub. Then the tag can be used with a click to make the correct handling.
 
Upvote 0

toby

Well-Known Member
Licensed User
Longtime User
You can try to simplify the code by placing an equivalent code in a sub and adding a unique tag in that sub. Then the tag can be used with a click to make the correct handling.
Could you show me how by modifying the attached project?
 
Upvote 0

toby

Well-Known Member
Licensed User
Longtime User
You can try to simplify the code by placing an equivalent code in a sub and adding a unique tag in that sub. Then the tag can be used with a click to make the correct handling.
Thanks for reminding me! Now I use same event name for all CLVs and as a result code is simplified significantly. I still would like to know how to duplicate desinger views in code.
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
would like to know how to duplicate desinger views in code


Do you mean something like Erel does in the XCustomListView example?
B4X:
Sub CreateListItem(Text As Map, Width As Int, Height As Int) As Panel
    Dim p As Panel
    p.Initialize("")
    p.SetLayout(0, 0, Width, Height)
    p.LoadLayout("your_item_layout")    ‘<—— your layout with the label and your horizontal xclv
,,,,,
 
Upvote 0

toby

Well-Known Member
Licensed User
Longtime User
There are 14 identical customListiviews and I would like to define just one of them in the designer and duplicate it 13 times at runtime and place them one below another in a scrollview. Currently I define all of them in the designer, resulting in a long layout which is hard to read.
 
Upvote 0

Heuristx

Active Member
Licensed User
Longtime User
Design a layout with just your listview or a panel with stuff, you can duplicate complex layouts, too.
You can have an invisible panel in your layout(panLoad), set to a correct size and do this:

Duplicate your view in code:
  Sub Class_Globals
    ...
    Private lv As B4XView
    ...
  End Sub
 
  'and in a Sub somewhere:
 
    For i = 1 To 13
        panLoad.LoadLayout("MyListView") 
        panListViewsParent.AddView(lv, x, y, ListViewWidth, ListViewHeight)
        'change x
        'change y
    Next
 
Upvote 0
Top