Android Question CustomListView with floating titles & Event VisibleRangeChanged

MarcoRome

Expert
Licensed User
Longtime User
Hi, this can help a lot, if you want to split the lists (
https://www.b4x.com/android/forum/threads/xui-customlistview-with-floating-titles.87935/#content )

With this code work without problem

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

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 TitleHeight As Int = 50dip
    Private DividerHeight As Int = 5dip
    Private clv As CustomListView
    Private pnlTitle As B4XView
    Private xui As XUI
    Private lblTitle As Label
    Type TitleData (Title As String)
    Type CardData (Text As String, Numero As Int)
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
    pnlTitle.Visible = False
    pnlTitle.SetLayoutAnimated(0, clv.AsView.Left, clv.AsView.Top, clv.AsView.Width, TitleHeight + DividerHeight)
    pnlTitle.LoadLayout("CellTitle")
    AddTitle("Title #AAA")
    For i = 1 To 10
        'clv.AddTextItem($"A) Item #${i}"$, "")
        Dim cd As CardData
        cd.Initialize
        cd.Text = $"A) Item #${i}"$
        cd.Numero = 10
        Dim p As B4XView = xui.CreatePanel("")
        p.Color = Colors.ARGB(255, 255,254,240)
        p.SetLayoutAnimated(0, 0, 0, clv.AsView.Width, 180dip)
                
        clv.Add(p, cd)
        If i = 5 Then
            AddTitle("Title #A2111")
        End If
    Next
    AddTitle("Title #BBB")
    For i = 1 To 10
        'clv.AddTextItem($"A) Item #${i}"$, "")
        Dim cd As CardData
        cd.Initialize
        cd.Text = $"A) Item #${i}"$
        cd.Numero = 10
        Dim p As B4XView = xui.CreatePanel("")
        p.Color = Colors.ARGB(255, 255,254,240)
        p.SetLayoutAnimated(0, 0, 0, clv.AsView.Width, 180dip)
                
        clv.Add(p, cd)
        If i = 5 Then
            AddTitle("Title #A23333")
        End If
    Next
    clv_ScrollChanged(0)
End Sub

Sub clv_ScrollChanged (Offset As Int)
    Dim TopIndex As Int = clv.FindIndexFromOffset(Offset + TitleHeight + DividerHeight * 2)
    If clv.GetValue(TopIndex) Is TitleData Then
        pnlTitle.Visible = False
    Else
        For i = TopIndex - 1 To 0 Step - 1
            If clv.GetValue(i) Is TitleData Then
                Dim td As TitleData = clv.GetValue(i)
                pnlTitle.GetView(0).Text = td.Title
                pnlTitle.Visible = True
                Exit
            End If
        Next
    End If
End Sub

Sub AddTitle (Title As String)
    Dim p As B4XView = xui.CreatePanel("")
    p.SetLayoutAnimated(0, 0, 0, clv.AsView.Width, TitleHeight)
    p.LoadLayout("CellTitle")
    lblTitle.Text = Title
    Dim td As TitleData
    td.Title = Title
    clv.Add(p, td)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

but if i have 1000 record and use Event VisibleRangeChanged


B4X:
Sub clv_VisibleRangeChanged (FirstIndex As Int, LastIndex As Int)
    Dim ExtraSize As Int = 20
    For i = 0 To clv.Size - 1
        Dim p As B4XView = clv.GetPanel(i)
        If i > FirstIndex - ExtraSize And i < LastIndex + ExtraSize Then
            'visible+
            If p.NumberOfViews = 0 Then
                Dim cd As CardData = clv.GetValue(i)
                .....

Dont work. Anyone already tried ?
Than you very much
 
Top