B4J Code Snippet ListView scroll changed event for lazyload

This is useful to lazy load a listview, as customlistview costs more memory. This is the reference webpage: https://www.superglobals.net/javafx-listview-lazy-loading/

B4X:
Sub addScrollChangedEvent
    Dim jo As JavaObject
    jo=ListView1
    Dim nodes() As Object
    nodes=jo.RunMethodJO("lookupAll",Array(".scroll-bar")).RunMethod("toArray",Null)
    Dim ListViewScrollBar As JavaObject
    For Each sb As Object In nodes
        Dim sbJO As JavaObject
        sbJO=sb
        Dim orientation As String=sbJO.RunMethod("getOrientation",Null)
        If orientation="VERTICAL" Then
            ListViewScrollBar=sbJO
        End If
    Next
    Log(ListViewScrollBar)

    Dim r As Reflector
    r.Target=ListViewScrollBar
    r.AddChangeListener("ListView1_ScrollPosition","valueProperty")
End Sub

Sub ListView1_ScrollPosition_Changed(OldVal As Object,NewVal As Object)
    Log(OldVal)
    Log(NewVal)
End Sub
 
Last edited:

xulihang

Active Member
Licensed User
Longtime User
This code can get the visible range. The reference page: https://stackoverflow.com/questions/30457708/visible-items-of-listview

B4X:
Sub getVisibleRange As Int()
    Dim jo As JavaObject
    jo=ListView1
    Dim VirtualFlow As JavaObject
    VirtualFlow=jo.RunMethodJO("getSkin",Null).RunMethodJO("getChildren",Null).RunMethodJO("get",Array(0))
    Dim lastVisibleCell As JavaObject
    lastVisibleCell=VirtualFlow.RunMethodJO("getLastVisibleCell",Null)
    Dim firstVisibleCell As JavaObject
    firstVisibleCell=VirtualFlow.RunMethodJO("getFirstVisibleCell",Null)
    Dim range(2) As Int
    range(0)=firstVisibleCell.RunMethod("getIndex",Null)
    range(1)=lastVisibleCell.RunMethod("getIndex",Null)
    Return range
End Sub
 
Top