我目前正在开发一个应用程序的一部分,该应用程序在图像旁边显示一个新闻片段(如果存在图像)。该新闻列表是 SegmentedControl“标签”页面的一部分,可以根据用户的需要隐藏和重新显示。由于图像是通过网络加载的,它们由 ImageTracker 处理,并且只有在完全加载后才设置为属性。
我遇到的问题是,虽然这些图像第一次加载良好,但如果我导航到不同的 SegmentControl“选项卡”然后导航回“新闻”选项卡,这些图像就会消失。通过将非活动容器的可见性设置为 false,并将活动容器的可见性设置为 true 来完成隐藏操作。
这就是我定义数据绑定的方式: ListView { id: lstShowNews objectName: "lstShowNews"
listItemComponents: [
ListItemComponent {
type: "item"
Detail_Tabs_NewsCell {
newsHeadline: ListItemData.headline
newsBlurbPreview: ListItemData.content
newsTimeAdded: ListItemData.time
newsImageVisible: ListItemData.hasImage
newsImage: ListItemData.image
}
}
]
}
自定义列表项的别名/属性:
property alias newsImageVisible: newsImage.visible
property alias newsHeadline: newsTitle.text
property alias newsImage: newsImage.image
property string newsBlurbPreview: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam condimentum tellus vel magna dapibus aliquam. Nunc iaculis metus ut arcu tempus semper."
property string newsTimeAdded: "News Source - 3 hours ago"
处理列表加载的 C++ 代码部分:
/*!
* Handles loading for each tab page. Enables data to only be loaded on demand.
* @param index - The new selected index on the SegmentedControl
*/
void MyClass::onSegmentChanged(int index) {
if(index == 2) {
if(!mNewsLoaded) {
// Load the news
NewsRequester* news = new NewsRequester(mId);
connect(news, SIGNAL(requestDone(QList<NewsItem*>)), this, SLOT(onNewsReceieved(QList<NewsItem*>)));
indNews->start(); // Activity indicator
news->requestNews();
}
else {
// Caching functions
}
}
else {
// Stuff for other tabs
}
}
void MyClass::onNewsReceieved(QList<NewsItem*> newsItems) {
qDebug() << "Received news reply";
indNews->stop();
indNews->setVisible(false);
mNewsItems = newsItems;
setNewsModel();
}
void MyClass::setNewsModel() {
GroupDataModel* newsModel = new GroupDataModel(QStringList() << "news");
newsModel->setGrouping(ItemGrouping::None);
// Move the list items to the data model
for(int i = 0; i < mNewsItems.size(); i++) {
mNewsItems.at(i)->loadImage();
newsModel->insert(mNewsItems.at(i));
}
ListView* lstNews = root->findChild<ListView*>("lstShowNews");
lstNews->setDataModel(newsModel);
}
用于跟踪图像并确保其在传递到列表之前正确加载的功能:
/*!
* Tracks the status of the current local image and displays when ready
*/
void NewsItem::trackImage() {
// Load cascades image
mTracker = new ImageTracker(mLocalImage);
connect(mTracker, SIGNAL(stateChanged (bb::cascades::ResourceTracker::State)),
this, SLOT(onStateChanged (bb::cascades::ResourceTracker::State)));
}
/*!
* Passes the downloaded image's path to the list
*/
void NewsItem::onImageRetrieved(QString path) {
//qDebug() << "Received image " << path;
mLocalImage = QUrl(path);
trackImage();
}
/*!
* Display the image if it's been fully loaded in memory
*/
void NewsItem::onStateChanged(bb::cascades::ResourceTracker::State state) {
if(state == bb::cascades::ResourceTracker::Loaded) {
qDebug() << "Image loaded";
mLocalCascadesImage = mTracker->image();
emit localImageChanged();
}
}
mLocalCascadesImage 的属性:
Q_PROPERTY(bb::cascades::Image image READ localImage NOTIFY localImageChanged)
有什么理由说明为什么 ListView 会以这种方式行为不端?谢谢。