ListIcon - load more data on scroll

Just starting out? Need help? Post your questions and find answers here.
User avatar
captain_skank
Enthusiast
Enthusiast
Posts: 639
Joined: Fri Oct 06, 2006 3:57 pm
Location: England

ListIcon - load more data on scroll

Post by captain_skank »

Hi,

Has anyone got or can point to an example of dynamicaly loading data into a listicongadget when scrolling up or down ?

I can't use pagination in this particular usage case but want to speed up load times from the database.

Any help appreciated

Cheers
User avatar
spikey
Enthusiast
Enthusiast
Posts: 747
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: ListIcon - load more data on scroll

Post by spikey »

What do you mean exactly by "I can't use pagination"?
User avatar
captain_skank
Enthusiast
Enthusiast
Posts: 639
Joined: Fri Oct 06, 2006 3:57 pm
Location: England

Re: ListIcon - load more data on scroll

Post by captain_skank »

Hi spikey,

So normaly i retrieve a limited record set using the LIMIT and OFFSET paramters in the underlying SQL query, I then have First, Previous Next and Last page navigation buttons to move through all the data in the recordest.

I only use this for large recorsets though.

However in my particular use case I cannot easliy do this as the number of records can change dynaicaly depending on user filtering etc. so i'd like to load an initial 500 records then as the scroll bar nears the 500th record load the next 500 etc.

Perhaps i'm thinking about this wrong or not explaining it right ?

Cheers
Axolotl
Enthusiast
Enthusiast
Posts: 798
Joined: Wed Dec 31, 2008 3:36 pm

Re: ListIcon - load more data on scroll

Post by Axolotl »

I haven't done anything like that yet, but the topic of virtual listboxes comes to mind.
Maybe this can help:
(New) Virtual ListIcon with Check Boxes & Images [Windows]
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
User avatar
spikey
Enthusiast
Enthusiast
Posts: 747
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: ListIcon - load more data on scroll

Post by spikey »

Something like this?

The gadget generates a #WM_NOTIFY message when a scroll starts with a NMHDR\Code value of #LVN_BEGINSCROLL or #LVN_ENDSCROLL when one ends. An #WM_NOTIFY/#LVN_ENDSCROLL message occurs when an arrow is clicked or a non-grip (page scroll) click occurs. I've then used GetScrollInfo_ to obtain the current state of the scroll. See https://learn.microsoft.com/en-us/windo ... scrollinfo and https://learn.microsoft.com/en-us/windo ... crollinfo .

If the position (SCROLLINFO\nPos) is over 75% of the current maximum (SCROLLINFO\nMax) I generate a custom event to call for more data.

In practice you will need to be a little more refined than this solution:
1) You'll need to adjust your threshold value. It will need to be lower when there are a small number of entries in the list, otherwise you might not trigger an event. But you'll want to make it tighter when there are larger numbers of entries, otherwise you will generate a request too early.
2) You'll need to 'debounce' the event, otherwise dragging the slider to the end of the bar will generate multiple events in a row.
3) You'll need to differentiate on hWnd too, you'll be getting #WM_Notify messages from other sources as well.

Code: Select all

#ProportionOfMax = 0.75
#Custom_Event_MoreData = #PB_Event_FirstCustomValue

Global Window_0, ListIcon_0, Counter

Declare ResizeGadgetsWindow_0()

Procedure WinCallback(hWnd, uMsg, WParam, LParam) 
  ; Windows fills the parameter automatically, which we will use in the callback...
  Define.SCROLLINFO ListScrollInfo
  Define.NMHDR *nm
  
  If uMsg = #WM_NOTIFY
    *nm = LParam
    If *nm\code = #LVN_ENDSCROLL
      ListScrollInfo\fMask = #SIF_ALL
      GetScrollInfo_(GadgetID(ListIcon_0), #SB_VERT, @ListScrollInfo)
      If ListScrollInfo\nPos >= (ListScrollInfo\nMax * #ProportionOfMax)
        PostEvent(#Custom_Event_MoreData, Window_0, ListIcon_0)
      EndIf
    EndIf
  EndIf
  
  ProcedureReturn #PB_ProcessPureBasicEvents 
EndProcedure 

Procedure OpenWindow_0(x = 0, y = 0, width = 600, height = 400)
  Window_0 = OpenWindow(#PB_Any, x, y, width, height, "", #PB_Window_SystemMenu)
  ListIcon_0 = ListIconGadget(#PB_Any, 10, 10, 580, 380, "Column 1", 200)
EndProcedure

Procedure ResizeGadgetsWindow_0()
  Protected FormWindowWidth, FormWindowHeight
  FormWindowWidth = WindowWidth(Window_0)
  FormWindowHeight = WindowHeight(Window_0)
  ResizeGadget(ListIcon_0, 10, 10, FormWindowWidth - 20, FormWindowHeight - 20)
EndProcedure

Procedure AddMore()
  Debug "More stuff needed!"
  For x = 1 To 100
    AddGadgetItem(ListIcon_0, -1, "This is item #" + StrU(Counter) + ".")
    Counter + 1
  Next x
EndProcedure

OpenWindow_0(50, 50)
AddMore()

SetWindowCallback(@WinCallback())

Repeat
  Event = WaitWindowEvent()
  
  If Event = #Custom_Event_MoreData
    AddMore()
  EndIf
    
Until Event = #PB_Event_CloseWindow
User avatar
captain_skank
Enthusiast
Enthusiast
Posts: 639
Joined: Fri Oct 06, 2006 3:57 pm
Location: England

[SOLVED] Re: ListIcon - load more data on scroll

Post by captain_skank »

Thanks Spikey,

Gave me an excellent start and works like a charm

cheers
Post Reply