iOS Question Masonry View

narek adonts

Well-Known Member
Licensed User
Longtime User
You can achieve this effect using panel. Make an array or a list, and put in it the current height value of columns that you have in your scrollview, for example 2.
Put panel at bottom of each column, and add height into column height

B4X:
Public Sub Masonry
    Dim arr(2) As Int = Array As Int(0,0)
    Dim top As Int
    For i = 0 To 100
        Dim pnl As Panel
        pnl.initialize("")
        scv.panel.addview(pnl,50%x *(i Mod 2),arr( i Mod 2),50%x,Rnd(50dip,100dip))
        arr(i Mod 2) = arr(i Mod 2) + pnl.Height + 8dip 'padding
    Next
    scv.contentheigt = max(arr(0),arr(1))
End Sub
Great piece of Code !
 
Upvote 0

narek adonts

Well-Known Member
Licensed User
Longtime User
You can achieve this effect using panel. Make an array or a list, and put in it the current height value of columns that you have in your scrollview, for example 2.
Put panel at bottom of each column, and add height into column height

B4X:
Public Sub Masonry
    Dim arr(2) As Int = Array As Int(0,0)
    Dim top As Int
    For i = 0 To 100
        Dim pnl As Panel
        pnl.initialize("")
        scv.panel.addview(pnl,50%x *(i Mod 2),arr( i Mod 2),50%x,Rnd(50dip,100dip))
        arr(i Mod 2) = arr(i Mod 2) + pnl.Height + 8dip 'padding
    Next
    scv.contentheigt = max(arr(0),arr(1))
End Sub
Modified your code a little bit to be able to change the number of Columns by parameter.

B4X:
Public Sub Masonry(Columns As Int)
    Dim arr(Columns) As Int
    Dim top As Int
    For i = 0 To 100
        Dim pnl As Panel
        pnl.initialize("")
        pnl.Color=Colors.RGB(Rnd(0,255),Rnd(0,255),Rnd(0,255))
        scv.panel.addview(pnl,(100%x/arr.Length) *(i Mod arr.Length),arr( i Mod arr.Length),100%x/arr.Length,Rnd(50dip,200dip))
        arr(i Mod arr.Length) = arr(i Mod arr.Length) + pnl.Height + 8dip 'padding
    Next
   
    Dim l As List
    l.Initialize2(arr)
    l.Sort(True)
    scv.ContentHeight = l.Get(l.Size-1)
End Sub
 
Upvote 0

ykucuk

Well-Known Member
Licensed User
Longtime User
Modified your code a little bit to be able to change the number of Columns by parameter.

B4X:
Public Sub Masonry(Columns As Int)
    Dim arr(Columns) As Int
    Dim top As Int
    For i = 0 To 100
        Dim pnl As Panel
        pnl.initialize("")
        pnl.Color=Colors.RGB(Rnd(0,255),Rnd(0,255),Rnd(0,255))
        scv.panel.addview(pnl,(100%x/arr.Length) *(i Mod arr.Length),arr( i Mod arr.Length),100%x/arr.Length,Rnd(50dip,200dip))
        arr(i Mod arr.Length) = arr(i Mod arr.Length) + pnl.Height + 8dip 'padding
    Next
 
    Dim l As List
    l.Initialize2(arr)
    l.Sort(True)
    scv.ContentHeight = l.Get(l.Size-1)
End Sub

Hello narek,

Thank you. your code works very well. I would like to ask how can i modify this code for add more records to current masonry. For example users ll click to "more button" for add 10 more records. I tried but unfortunately i didn't succeed. Added 10 records seems not like should be.
 
Upvote 0
Top