Android Question is it possible to load the customlistview from bottom to top?

jose luis gudino

Active Member
Licensed User
Longtime User
I am making a chat and I would like to load the customlistview that contains the conversation from the most recent to the oldest, loading from the bottom to the top, is it possible
thanks in advance
 

William Lancee

Well-Known Member
Licensed User
Longtime User
The intentions of the OP are somewhat ambiguous. Should the most recent be at the top or the bottom?

@PaulMeuris

Insert is good, if most recent is at the top then insert at 0 is the solution.
Otherwise insert at end is essentially the same as adding an item.
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
Here is an example of adding items to the top. For expediency I used B4J in B4XPages, but the essential code would be the same for B4A in non-pages.

B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")        'Just a screen/form-filling CLV
   
    Dim simulationText As String = $"
Me: Hello
You: What's up?
Me: Not much, and you?
You: Just passing time
Me: Let's paint the town
You: I've got the keys to the car. I'll be right there
    "$
    Dim exchange As List: exchange.Initialize
    Dim v() As String = Regex.Split(CRLF, simulationText)
    For Each s As String In v
        If s.Trim.Length > 0 Then exchange.Add(s.trim)
    Next
   
    For Each s As String In exchange
        addMessage(s)
        Sleep(2000)
    Next
End Sub

Private Sub addMessage(s As String)
    Dim w() As String = Regex.Split("\: ", s)
    Dim h As Float = 100    'compute the height of message instead (B4A StringUtils)
    Dim pnl As B4XView = xui.CreatePanel("")
    pnl.SetLayoutAnimated(0, 0, 0, Root.Width - 20, h)
    Dim name As Label: name.Initialize("")
    Dim namex As B4XView = name
    namex.Text = w(0)
    pnl.AddView(namex, 0, 0, pnl.Width, 20)
    Dim message As Label: message.Initialize("")
    Dim messagex As B4XView = message
    If w(0) = "Me" Then
        namex.SetTextAlignment("CENTER", "LEFT")
        messagex.SetTextAlignment("CENTER", "LEFT")
        namex.TextColor = xui.Color_Blue
        messagex.TextColor = xui.Color_Blue
        messagex.Text = "   " & w(1)
    Else
        namex.SetTextAlignment("CENTER", "RIGHT")
        messagex.SetTextAlignment("CENTER", "RIGHT")
        namex.TextColor = xui.Color_Black
        messagex.TextColor = xui.Color_Black
        messagex.Text = w(1) & "   "
    End If
    pnl.AddView(messagex, 0, namex.Height, pnl.Width, 30)
    pnl.height = namex.Height + messagex.height
    CustomListView1.InsertAt(0, pnl, s)
End Sub
 

Attachments

  • myChat.zip
    8.9 KB · Views: 72
Upvote 0
Top