diff --git a/frameworks/core/components_ng/syntax/lazy_for_each_builder.h b/frameworks/core/components_ng/syntax/lazy_for_each_builder.h index 4a50886211e4616cd35cc98f756e4226aed162fd..72a81c006dd758b1d25e6e1a591d3038c27ca0c1 100644 --- a/frameworks/core/components_ng/syntax/lazy_for_each_builder.h +++ b/frameworks/core/components_ng/syntax/lazy_for_each_builder.h @@ -162,6 +162,22 @@ public: bool OnDataMoved(size_t from, size_t to) { + if (from == to) { + return false; + } + auto fromIter = cachedItems_.find(from); + auto toIter = cachedItems_.find(to); + if (fromIter != cachedItems_.end() && toIter != cachedItems_.end()) { + std::swap(fromIter->second, toIter->second); + } else if (fromIter != cachedItems_.end()) { + expiringItem_.try_emplace( + fromIter->second.first, LazyForEachCacheChild(to, std::move(fromIter->second.second))); + cachedItems_.erase(fromIter); + } else if (toIter != cachedItems_.end()) { + expiringItem_.try_emplace( + toIter->second.first, LazyForEachCacheChild(from, std::move(toIter->second.second))); + cachedItems_.erase(toIter); + } return true; } @@ -261,13 +277,15 @@ public: std::unordered_set idleIndexes; if (startIndex_ != -1 && endIndex_ != -1) { for (int32_t i = 1; i <= cacheCount_; i++) { - if (startIndex_ >= i) { - idleIndexes.emplace(startIndex_ - i); - } if (endIndex_ + i < count) { idleIndexes.emplace(endIndex_ + i); } } + for (int32_t i = 1; i <= cacheCount_; i++) { + if (startIndex_ >= i) { + idleIndexes.emplace(startIndex_ - i); + } + } } for (auto& [key, node] : expiringItem_) { @@ -345,4 +363,4 @@ private: }; } // namespace OHOS::Ace::NG -#endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_SYNTAX_FOREACH_LAZY_FOR_EACH_BUILDER_H \ No newline at end of file +#endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_SYNTAX_FOREACH_LAZY_FOR_EACH_BUILDER_H diff --git a/frameworks/core/components_ng/syntax/lazy_for_each_node.cpp b/frameworks/core/components_ng/syntax/lazy_for_each_node.cpp index 2cb4f51276a9a4b251eb7c885cd762fb6310c84e..3ef4073168fd196acd33bbd2b450205e6ce155f7 100644 --- a/frameworks/core/components_ng/syntax/lazy_for_each_node.cpp +++ b/frameworks/core/components_ng/syntax/lazy_for_each_node.cpp @@ -153,7 +153,12 @@ void LazyForEachNode::OnDataChanged(size_t index) void LazyForEachNode::OnDataMoved(size_t from, size_t to) { - // TODO: data move + if (builder_) { + builder_->OnDataMoved(from, to); + } + children_.clear(); + MarkNeedSyncRenderTree(true); + MarkNeedFrameFlushDirty(PROPERTY_UPDATE_MEASURE_SELF_AND_PARENT); } void LazyForEachNode::NotifyDataCountChanged(int32_t index)