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
New Developer
Laert
Posts: 13
Registered: ‎01-22-2013
My Device: Blackbery 10 Dev Alpha Simulator
Accepted Solution

What signal is emited in ListView , when one of its cells appears into or goes off from screen ?

what signal is emited in ListView , when one of its cells appears into or goes off from screen ?

I created listView with custom listItemComponents

Is there any signals when listItemComponent go off from screen ?

Please use plain text.
Developer
Zmey
Posts: 1,512
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: What signal is emited in ListView , when one of its cells appears into or goes off from screen ?

[ Edited ]

Currently it's not possible to detect when the cells go off from screen. Even if you attach LayoutUpdateHandler to them they will just stop sending updates when going off screen. I was able to hack a solution using a timer, but I guess more cleaner approach would be using a ScrollView with CustomControls instead:

1. Get the ScrollView's dimensions by attaching LayoutUpdateHandler to ScrollView.

2. Attach LayoutUpdateHandlers to CustomControls.

UPD: There's also viewableArea property & viewableAreaChanged event in scrollView.

3. If controls which were inside of visible area of ScrollView have gone outside, fire the event.

 

Of course cell reuse won't work this way.

 

Catching the cell appearing on screen is easy though:

If you're using ListItemProvider, updateItem() function will be called when item appears on screen:

https://developer.blackberry.com/cascades/reference/bb__cascades__listitemprovider.html

It will be also called if item data is updated in model.

 

When the item is created for first time, createItem() is called.

 


Andrey Fidrya, @zmeyc on twitter
Please use plain text.
New Developer
Laert
Posts: 13
Registered: ‎01-22-2013
My Device: Blackbery 10 Dev Alpha Simulator

Re: What signal is emited in ListView , when one of its cells appears into or goes off from screen ?

[ Edited ]

3. If controls which were inside of visible area of ListView have gone outside, fire the event.
If it goes outside visible area it stops sending signals, so how can we catch this signal ? I saw that last signal emitted is +-60 from parent borders

Please use plain text.
Developer
Zmey
Posts: 1,512
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: What signal is emited in ListView , when one of its cells appears into or goes off from screen ?

[ Edited ]

In ListView, not ScrollView. :smileyhappy:

In ScrollView they will still send layout updates I guess.
If they don't, another approach to try is precalculating item positions & sizes and checking which ones are visible in viewableAreaChanged handler. This will require some experimenting. I haven't tried ScrollView approach yet.

I'm currently using a ListView hack which I don't like: when items aren't updated for some amount of time I consider them to be off-screen. When scrolling starts I'm enabling this logic with a delay so all items on screen have a chance for an initial update.

 

UPD. Added some code to show the idea.

 

I keep track of all ListItems in a map. Don't forget to remove items from map when they are destroyed: destroyed() signal can be used for this.

 

In .h file:

        struct DistanceInfo
        {
                DistanceInfo(int distance = 0)
                        : lastUpdateTime_(QDateTime::currentDateTimeUtc())
                        , distance_(distance)
                {

                }

                QDateTime lastUpdateTime_;
                int distance_;
        };

        void removeOldDistances();
        int findNearestIndex();

        QMap<SpinnerListItem *, DistanceInfo> distances_;


In .cpp:

SpinnerControl::SpinnerControl()
{
...
       nearestIndexUpdateTimer_ = new QTimer(this);
        nearestIndexUpdateTimer_->setInterval(itemFrameUpdateIntervalMsec);
        nearestIndexUpdateTimer_->setSingleShot(true);
        QObject::connect(nearestIndexUpdateTimer_, SIGNAL(timeout()),
                                         this, SLOT(updateNearestIndex()));
}

void SpinnerControl::item_layoutFrameChanged(SpinnerListItem *item, const QRectF &layoutFrame)
{
        // Removes items which weren't updated for last itemFrameUpdateIntervalMsec msecs from map
removeOldDistances();
bool shouldPostponeUpdate = distances_.empty() || nearestIndexUpdateTimer_->isActive(); int dist = qAbs(layoutFrame.top() - listViewFrame_.top()); distances_[item] = DistanceInfo(dist); if (shouldPostponeUpdate) { if (!nearestIndexUpdateTimer_->isActive()) nearestIndexUpdateTimer_->start(); } else { updateNearestIndex(); } } void SpinnerControl::updateNearestIndex() { int nearestIndex = findNearestIndex(); if (nearestIndex != currentIndex_) { currentIndex_ = nearestIndex; emit currentIndexChanged(); } }

 


Andrey Fidrya, @zmeyc on twitter
Please use plain text.