Refresh ListViewGadget without jumping to top

Just starting out? Need help? Post your questions and find answers here.
firace
Addict
Addict
Posts: 899
Joined: Wed Nov 09, 2011 8:58 am

Refresh ListViewGadget without jumping to top

Post by firace »

My listviewgadget refreshes every 2 seconds with updated data.
Is it possible to prevent the scroll position from jumping back to the top when it does?

[EDIT] Removed irrlevant code for clarity.

Code: Select all

Global NewList PROBA()

Procedure GetTList()
  for i = 1 to 300
    AddElement   (PROBA ())
    PROBA () = random(10)
  next
EndProcedure

Enumeration 
  #MainForm :   #MainList :   #MainList2
EndEnumeration

Procedure FillLists()
  ClearList( PROBA() )
  GetTList()
  ResetList( PROBA() )
  
  ClearGadgetItems(#MainList)   
   
  ForEach PROBA()
    AddGadgetItem(#MainList,-1,str(PROBA()),-1,#PB_Unicode)  
  next
   
EndProcedure

X=10:Y=0:WF=500:HF=400

Window_1 = OpenWindow(#PB_Any, 66, 33, 640, 520, "TT", #PB_Window_SystemMenu)

ListViewGadget(#MainList,X     ,45,WF-50,HF-(45+10))

AddWindowTimer(Window_1, 77, 2000)

Repeat 
  
  e = WaitWindowEvent()
  if e = #PB_Event_Timer 
    FillLists()
  endif
  
Until e = 13116
Last edited by firace on Sun Apr 23, 2017 5:37 pm, edited 2 times in total.
User avatar
spikey
Enthusiast
Enthusiast
Posts: 581
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: Refresh ListViewGadget without jumping to top

Post by spikey »

firace wrote:Is it possible to prevent the scroll position from jumping back to the top when it does?
You can't entirely eliminate the flicker because you are clearing the gadget, but it should be a lot better.

Code: Select all

Global NewList PROBA ()

Procedure fLSISetRedraw(aListView.I, aState.B)
  ; Set Redraw state for list view aListView to aState.
  
  If aState <> #True And aState <> #False
    aState = #True
  EndIf
  
  If IsGadget(aListView)
    SendMessage_(GadgetID(aListView), #WM_SETREDRAW, aState, 0)
  EndIf
  
EndProcedure

Procedure fLSIShowSelected(aListView.I)
  ; Force list view to scroll to selected item.
  
  Protected.I hItem
  
  If IsGadget(aListView)
    hItem = SendMessage_(GadgetID(aListView), #LVM_GETNEXTITEM, -1, #LVNI_SELECTED)
    If hItem
      SendMessage_(GadgetID(aListView), #LVM_ENSUREVISIBLE, hItem, #False)
    EndIf
  EndIf
  
EndProcedure

Procedure GetTList()
  For i = 1 To 300
    AddElement   (PROBA ())
    PROBA () = Random(10)
  Next
  
EndProcedure


Enumeration 
  #MainForm :   #MainList :   #MainList2
EndEnumeration

Procedure FillLists()
  
  ClearList( PROBA() )
  GetTList()
  ResetList( PROBA() )
  
  Selected = GetGadgetState(#MainList)
  fLSISetRedraw(#MainList, #False)
  
  ClearGadgetItems(#MainList)  : k=0 : j=0 
  
  
  ForEach PROBA()
    AddGadgetItem(#MainList,-1,Str(PROBA()))  
  Next
  
  fLSISetRedraw(#MainList, #True)
  
  If Selected >= 0
    SetGadgetState(#MainList, Selected)
    fLSIShowSelected(#MainList)
  EndIf
  
  
  SetGadgetText(333,RSet(Str(ListSize(PROBA())),9))
  
  
EndProcedure


Window_1 = OpenWindow(#PB_Any, 66, 33, 640, 520, "TT", #PB_Window_SystemMenu)

X=10:Y=0:WF=500:HF=400


ListViewGadget(#MainList,X     ,45,WF-50,HF-(45+10))
TextGadget(333,X+15,429,330,70,"-")


AddWindowTimer(Window_1, 77, 2000)

Repeat 
  
  e = WaitWindowEvent()
  If e = #PB_Event_Timer 
    FillLists()
  EndIf
  
Until e = 13116
Last edited by spikey on Sun Apr 23, 2017 5:52 pm, edited 1 time in total.
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Refresh ListViewGadget without jumping to top

Post by IdeasVacuum »

If the total number of rows remains the same or similar after the refresh:
ListIcon Scroll Position Saving
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Post Reply