Android Question ListView Position Without ItemClick?

margret

Well-Known Member
Licensed User
Longtime User
I am working on a new app that I need to know the position of a listview. For example, I have 3000 entries and say I scroll down the list 20 pages. Is there any way to know the first item or any item that is currently displayed on the device or showing in the View. I know if I click the item, I can get the item position. I am wanting to know the position after each scroll or fling and not to require the user to click the item. There must be some way this can be done. Any ideas?? As always, thanks in advance.

Margret
 

walterf25

Expert
Licensed User
Longtime User
I am working on a new app that I need to know the position of a listview. For example, I have 3000 entries and say I scroll down the list 20 pages. Is there any way to know the first item or any item that is currently displayed on the device or showing in the View. I know if I click the item, I can get the item position. I am wanting to know the position after each scroll or fling and not to require the user to click the item. There must be some way this can be done. Any ideas?? As always, thanks in advance.

Margret
one way i can think of and it maybe a bit too complicated is retrieve the device's screen size, and based on the height of each line in the listview determine how many items get displayed at each time per page, since you would know how many items in total the listview will be populated with then determine how many pages in total.

Hope that makes sense, it makes sense in my head but again, there may be an easier way to solve this.

Good luck
Walter
 
Upvote 0

eps

Expert
Licensed User
Longtime User
I can see what the above poster is trying to suggest, but I don't think it will work.

The only way I can think of that might work is to overlay the listview with a panel and work out how much of a scroll or fling has been indicated and pass this to the listview. But it starts too sound a bit too complicated at that point.
 
Upvote 0

margret

Well-Known Member
Licensed User
Longtime User
I had hoped that this would work and it almost does. As I setup and populate the ListView the Scrolled event fires 3 times in a roll when no physical scroll has taken place. If you run the code with Debug, it gets stuck in the Scrolled Event and stays there. I was trying to use that Event to write data that would be used for Activity_Resume, but the data is not valid as this Event fires on internal events and not just from the user scrolling the view. Maybe if I understood more of why and what fires the event. At this point it seems that a DataChanged, Scroll, Load, Unload and Clear all fire the Event but I see no way to know which. I really like this view but I am still trying to find a way to use it for my current needs.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Hello Margret.
Well, I think you can use reflection combined with a timer for this. For example:
B4X:
Sub Globals
   Dim lv As ListView, rf As Reflector, tmr As Timer  
End Sub

Sub Activity_Create(FirstTime As Boolean)
   lv.Initialize("lv")
   For i=0 To 2999:lv.AddSingleLine(i):Next
   Activity.AddView(lv,0,0,100%x,100%y)
   tmr.Initialize ("tmr",100)
   tmr.Enabled=True
   rf.Target=lv
End Sub


Sub tmr_tick
   Dim firstVisible As Int
   firstVisible=rf.RunMethod("getFirstVisiblePosition")
   Log(firstVisible)
End Sub

This should get you the first visible item's index (top most row).
 
Upvote 0

margret

Well-Known Member
Licensed User
Longtime User
The ListView from Informatix is very nice, it's just that it didn't work the way I thought it would. I now have it working. I placed a Boolean flag inside the Scrolled event. The event fires all the time as before but the data is only changed if the Boolean is in the proper state, which I can control. Thank you mc73 for your help. If I didn't have this working I would certainly use your code.

Margret
:)
 
Upvote 0

margret

Well-Known Member
Licensed User
Longtime User
The best answer is that I thought that only a physical scroll would fire the event. I understand why other things trigger the event like a data change or a clear. If these events were all separate events it would be a more flexible view. Please, do not get me wrong, it is a GREAT view and very, very helpful, so thank you for this!
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
The best answer is that I thought that only a physical scroll would fire the event. I understand why other things trigger the event like a data change or a clear. If these events were all separate events it would be a more flexible view. Please, do not get me wrong, it is a GREAT view and very, very helpful, so thank you for this!
Unfortunately I don't think that these events can be separated. The scroller does only a difference for fling gestures, not between a normal gesture and an automatic move of items (e.g. when you add one).
 
Upvote 0
Top