Android Question listview anchor

drgottjr

Expert
Licensed User
Longtime User
i have a lengthy listview

after i click on an item and the app performs the assigned function, when i return to the listview, i notice that everything is where i left it (eg, if item 7 was at the top of the screen, it's still at the top the next time i make the view visible.)

on a resume, however (following an orientation change), the listview display starts with item 0 at the top of the screen.

forcing the view to start its display at item 0 is easy enough; how do i force it to start at a specific index - an anchor of sorts? in other words, do a virtual scroll to a certain index and then make the view visible with that index at the top.

thank you.
-go
 

DonManfred

Expert
Licensed User
Longtime User
B4X:
listview1.SetSelection(6)
?
 
Upvote 0

James Chamblin

Active Member
Licensed User
Longtime User
You can use JavaObject to get the current top of the listview, then set it in Activity_Resume.
B4X:
#Region  Project Attributes
   #ApplicationLabel: B4A Example
   #VersionCode: 1
   #VersionName:
   'SupportedOrientations possible values: unspecified, landscape or portrait.
   #SupportedOrientations: unspecified
   #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
   #FullScreen: False
   #IncludeTitle: True
#End Region

Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim Top As Int = 0

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.
   Dim lvItems As ListView


End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   'Activity.LoadLayout("Layout1")

     lvItems.Initialize("lvItems")
     Dim i As Int
     For i = 1 To 500
       lvItems.AddSingleLine("Item "&i)
     Next
   Activity.AddView(lvItems,0,0,100%x,100%y)

End Sub

Sub Activity_Resume
   lvItems.SetSelection(Top)

End Sub

Sub Activity_Pause (UserClosed As Boolean)
   Dim jvObject As JavaObject = lvItems
   Top = jvObject.RunMethod("getFirstVisiblePosition",Null)

End Sub
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
B4X:
listview1.SetSelection(6)
?

yeah, thanks. that gets me closer. James Chamblin's reply (next), i think, does the trick. instead of my having to figure out what might have been at the top of the displayed list (or coming close), accessing the javaobject tells me what was there. then i can set selection as you suggest. thanks, both.
-go
 
Upvote 0
Top