B4A Library xListview

xCLV now supports adding huge numbers of items smoothly using Preoptimized class , you should use it instead :
I'd like to share my first B4A library,

Using xListview you can add tens of thousands of items instantly with an extremely low memory footprint.

How it works
instead of creating an empty panel for each item to load the layout inside it when needed, which takes time to create the panels and consumes more memory if used for a large number of items
xListview on the other hand uses only 1 panel for each visible item and automatically destroys it when it goes offscreen.
So if we have a list that contains 100,000 items it will actually have around 15 panels only.


Requirements
Reflection library






test on a very old android device



Usage

B4X:
Sub Globals
    Private xLV As xListView
    Private lblTitle As Label
    Private lblTime As Label
End Sub
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("L1")
    xLV.selectionColor = Colors.Cyan
    xLV.OffscreenItems=5 'Default = 5, change it before adding items
    xLV.SetScrollbarColor(Colors.Blue)
    xLV.HideScrollBar(False) ' Set True to hide Scrollbar
End Sub
Sub xLV_PanelRequested(Index As Int)
    'Load the panel here , the panel will be automatically removed when it's not visible (don't remove it manually)
    Dim P As Panel
    P.Initialize("")
    P.SetLayout(0,0,xLV.AsView.Width,180dip)
    P.LoadLayout("ItemLayout") 'Disable animation from the Visual Designer for more smooth scrolling
    lblTitle.Text= "Item Number : " & Index
    xLV.LoadPanel(P,Index)
End Sub
Sub Button1_Click
    Dim B As Button=Sender
    B.Text="Clicked"
End Sub
Sub xLV_ItemClick (Index As Int, Value As Object)
    ToastMessageShow("Item " & Index & " Clicked",False)
End Sub
Sub xLV_ItemLongClick (Index As Int, Value As Object)
    ToastMessageShow("Item " & Index & " Long Clicked",False)
End Sub
Sub btnAddItems_Click
    Dim ms As Long = DateTime.Now
    For i = 1 To 10000
        xLV.NewItem(180dip,i)
    Next
    xLV.Reload ' you must call reload after adding new items
    lblTime.Text = "Load Time : " & (DateTime.Now - ms) & " ms    -   Total Items : " & xLV.Size
End Sub


v0.9
+ Resize item
+ DisableSelectionColor

v0.95
+ Scrolling performance improved
 

Attachments

  • example.zip
    41.6 KB · Views: 813
  • xListview0.95.zip
    9.1 KB · Views: 799
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is true that xCLV (currently) creates a panel for each item. With lazy loading you can create long lists.
One of the reasons that I don't think that it is a big issue is that a user will never scroll 100k items. This is not practical.


Edit: while there is never a good reason to add 100k items to a UI list, you can also do it with xCustomListView using PreoptimizedCLV: https://www.b4x.com/android/forum/t...loading-extension-for-xcustomlistview.115289/
 
Last edited:

LucaMs

Expert
Licensed User
Longtime User
did you try to add 100,000 items with clv ?
clv is not suitable for such large numbers of items
I thought it was based on xCustomListView, since, at least with B4A, the normal ListView doesn't have Panels (or, perhaps better, doesn't expose them).

[or maybe your library isn't even based on ListView but written from scratch]
 

KZero

Active Member
Licensed User
Longtime User
It is true that xCLV (currently) creates a panel for each item. With lazy loading you can create long lists.
One of the reasons that I don't think that it is a big issue is that a user will never scroll 100k items. This is not practical.

This library is made for those who find 250ms too slow to add only 1000 item to the list.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
This library is made for those who find 250ms too slow to add only 1000 item to the list.
1. Don't get me wrong. It is good that there are more choices for developers and each developer will use whatever they find to be better for their requirements.
2. When I test it with xCLV adding 1000 items takes around 110ms on the first time and repeating it takes around 70ms. This is unnoticeable. It will be slower on slower devices, though 250ms is not really slow.

Edit: it takes 3ms to create 1000 items with xCLV and PreoptimizedCLV: https://www.b4x.com/android/forum/t...loading-extension-for-xcustomlistview.115289/
 
Last edited:

KZero

Active Member
Licensed User
Longtime User
well,
few hundreds of milliseconds or even a few seconds may not matter if you are using a button to load the list
but when the list is loaded on the activity start it really matters
 
Last edited:

Mahares

Expert
Licensed User
Longtime User
Great work. You should change your name from KZero to KMillion:
B4X:
Sub btnAddItems_Click
    Dim ms As Long = DateTime.Now
    For i = 1 To 10000
        xLV.NewItem(180dip,i)
    Next
    xLV.Reload ' you must call reload after adding new items     
    lblTime.Text = "Load Time : " & (DateTime.Now - ms) & " ms    -   Total Items : " & xLV.Size 
    xLV.JumpToItem(xLV.Size-1)    '<--Did not jump to last item and error  when I added this line, but no crash 
End Sub
B4X:
Unexpected event (missing RaiseSynchronousEvents): xlv_panelrequested
Check the unfiltered logs for the full stack trace.
Also:
xLV.SetScrollbarColor(XUI.Color_Red) did not show the scroll bar
 

KZero

Active Member
Licensed User
Longtime User
xLV.JumpToItem(xLV.Size-1) '<--Did not jump to last item and error when I added this line, but no crash
JumpToItem works normally on all my devices and emulators, are you trying in Debug mode?
anyway try to add sleep(0) before calling jumpToItem or try to call it twice
B4X:
xLV.JumpToItem(xLV.Size-1)
sleep(0)
xLV.JumpToItem(xLV.Size-1)
xLV.SetScrollbarColor(XUI.Color_Red) did not show the scroll bar
SetScrollbarColor may not work on all devices as mentioned here
 

Mahares

Expert
Licensed User
Longtime User
try to add sleep(0) before calling jumpToItem
Thank you KMillion: This worked even in debug:
B4X:
Dim ms As Long = DateTime.Now
    For i = 1 To 10000
        xLV.NewItem(180dip,i)
    Next
    xLV.Reload ' you must call reload after adding new items 
    Sleep(0)  '<--- where it needs to be
    xLV.JumpToItem(xLV.Size-1)
    lblTime.Text = "Load Time : " & (DateTime.Now - ms) & " ms    -   Total Items : " & xLV.Size
 

Peter Simpson

Expert
Licensed User
Longtime User
It is true that xCLV (currently)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Currently???
Are you thinking about changing it up a bit @Erel???
 

peacemaker

Expert
Licensed User
Longtime User
Such solution (of any list view) gives a warranty that no app crash at user's side when (and if) he collected so long list of needed items. Sure, it's stupid to scroll whole 100K list, but for the first time to "show all" - it's safe solution.
There are many real databases of any kind of goods that initially are very long: Aliexpress goods, auto spare-parts, flowers... any kind of items aggregator.
So - many thanks for such views.
 

Xfood

Expert
Licensed User
congratulations @KZero , it would be nice to have this library in B4x lib, so that you can use it for all B4x platforms
 
Top