如何正确动画从级联中的数据模型中删除项目



当我尝试对从数据模型中删除项目进行动画处理时,该项目被删除,但显然因为我使用的动画将项目的维度设置为 0,添加到数据模型的下一个项目是不可见的。要查看它,我必须重新加载整个数据模型,或者关闭并重新打开我的应用程序。如果我不做动画,项目就会被删除并正确添加,只是没有达到我想要达到的效果。我的示例代码如下:

ListView {
dataModel: GroupDataModel {
    id: noteDataModel
}
listItemComponents: [
            ListItemComponent {
                type: "item"
                StandardListItem {
                    id: noteItem
                    title: ListItemData.noteTitle
                    description: ListItemData.noteText 
                    animations: [
                        ScaleTransition {
                            id: deleteAnimation
                            toY: 0
                            toX: 0
                            duration: 500
                            onEnded: {          
                              noteItem.ListItem.view.dataModel.remove(noteItem.ListItem.view.dataModel.data(noteItem.ListItem.indexPath));
                            }
                        }
                    ]
                contextActions: [
                        ActionSet {
                            DeleteActionItem {
                                onTriggered: {
                                    deleteAnimation.play();
                                }
                            }
                        }
                    ]
}

出现此问题的原因是 Cascades 框架有时会重用对象以获得更好的性能。从数据模型中删除项并将其维度设置为零时,该对象将在内存中保留一段时间,维度属性设置为 0。您添加的下一个项将重用已删除的项对象,但尺寸为 0,因此它不可见。

若要解决此问题,请更改动画的 onTEnd 事件,并在删除后将项目重新缩放回 1.0 的尺寸:

onEnded: {
     noteItem.ListItem.view.dataModel.remove(noteItem.ListItem.view.dataModel.data(noteItem.ListItem.indexPath));
     noteItem.scaleX = 1.0;
     noteItem.scaleY = 1.0;
}

最新更新