Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
contryboy
Posts: 68
Registered: ‎08-16-2012
My Device: BB10 Dev Alpha

Is there a way to get the scroll position of a ListView?

Hi There,

 

I have searched it for a long time, but am not able to find a way to get it? 

What I want is to implement a release to refresh feature. There is already a discussion in this forum, but no solution yet. I think I can implment this feature when when I know the current scroll position. Any idea?

 

Thanks,

Dong

Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10

Re: Is there a way to get the scroll position of a ListView?

There's definitely no obvious way. I've tried a variety of approaches, none working yet. Whatever we come up with, if it's even possible, will probably be a crude hack.

Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10

Re: Is there a way to get the scroll position of a ListView?

As often occurs when I write such things, I immediately thought of some possibilities. One of them involves attaching a LayoutUpdateHandler to the visual nodes for the list items. When you do that, you can at least get exact positioning info (and width/height) for the currently visible individual list items, relative to the ListView's own position.

Possibly with that plus some monitoring (with ListItemListener?) of when individual items become visible or go offscreen, you might be able to extract the required data to figure this out.

Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10

Re: Is there a way to get the scroll position of a ListView?

By the way, doing this with C++ shouldn't be too difficult, as you could implement ListItemListener::reset() and/or ListItemProvider::updateItem() to infer which ones are visible.

Looks like it can be done, even in QML, but "crude hack" doesn't begin to describe it. ;-)

Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Developer
kylefowler
Posts: 526
Registered: ‎05-17-2009
My Device: 9900

Re: Is there a way to get the scroll position of a ListView?

This kind of works, but since the ListView preloads X number of items so that the scrolling is fast, you will get update item even if it is not visible on the screen.
Like all of my posts
Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10

Re: Is there a way to get the scroll position of a ListView?

That's true, Kyle, but I'm almost certain there is enough information there to figure it out.

I probably won't get around to it for a week since I've got other fish to fry, but the basic idea would be that you have to track the vertical positions of the VisualNodes using the LayoutUpdateHandler, and be able to map from those to the items in the model. You should be able to do that with ListItem.indexInSection and/or similar properties. The layoutFrame stuff also gives you the dimensions of each item, so you can probably make this work even if you have different heights for various items, as long as you track them.

You'd probably also need some extra code and data to track items as they move and basically drop them out of the set as they go too negative or positive... the Y positions relative to the ListView's own layout frame should tell you that.

There are also ways to use this to solve various specialized use cases without all the hard work. For example, if you wanted to know whether a special "Load More" item at the end of the list was visible, you could just assign it a unique "type" property and have a dedicated ListItemComponent with its own LayoutUpdateHandler. When the position changes, if you're in the field of view, you know you're visible and can fire a signal to load more data.

The same type of thing works for having a dynamically growing "log" window, where new stuff arrives at the bottom and you want to auto-scroll to show it. If the last item is visible, then you can auto-scroll. If the last one's not visible, you know the user has scrolled back so you start a timer, restarting it whenever the Y position adjusts (i.e. user scrolls again). If the timer fires, or the last item becomes visible again, you reposition to the end, etc.

Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Developer
helex
Posts: 293
Registered: ‎02-14-2012
My Device: Nokia N950 / BB Playbook

Re: Is there a way to get the scroll position of a ListView?

I found a issue at the issue tracker for this problem: ListView should support a way to know its current scroll position

 

Please Vote!

Developer
helex
Posts: 293
Registered: ‎02-14-2012
My Device: Nokia N950 / BB Playbook

Re: Is there a way to get the scroll position of a ListView?

For the people which loves to see code I created a small testing example in the other thread: Link

 

It's created for a horizontal scrolling listview and works only if there is only a single element as currently visible possible. But with a little bit tweaking it should also work fine if you have smaller elements in the list.