From 8c6b79e0d36235cebcd600e9967dd1af030c3919 Mon Sep 17 00:00:00 2001 From: yeyinglong_admin Date: Fri, 15 Mar 2024 10:23:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96List/Gird=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E5=AD=90=E8=8A=82=E7=82=B9=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yeyinglong_admin --- .../core/components_ng/base/frame_node.cpp | 25 ++++----- .../core/components_ng/base/frame_node.h | 7 +-- .../core/components_ng/base/ui_node.cpp | 4 +- frameworks/core/components_ng/base/ui_node.h | 2 +- .../components_ng/layout/layout_wrapper.h | 5 +- .../layout/layout_wrapper_node.cpp | 2 +- .../layout/layout_wrapper_node.h | 5 +- .../pattern/custom/custom_node.cpp | 4 +- .../pattern/custom/custom_node.h | 2 +- .../grid_scroll_layout_algorithm.cpp | 12 ++--- .../list/list_lanes_layout_algorithm.cpp | 8 +-- .../pattern/list/list_layout_algorithm.cpp | 6 +-- .../syntax/lazy_for_each_builder.cpp | 18 +++++-- .../syntax/lazy_for_each_builder.h | 12 ++++- .../syntax/lazy_for_each_node.cpp | 54 ++++++++++--------- .../components_ng/syntax/lazy_for_each_node.h | 2 +- 16 files changed, 95 insertions(+), 73 deletions(-) diff --git a/frameworks/core/components_ng/base/frame_node.cpp b/frameworks/core/components_ng/base/frame_node.cpp index df040172592..fe419f93244 100644 --- a/frameworks/core/components_ng/base/frame_node.cpp +++ b/frameworks/core/components_ng/base/frame_node.cpp @@ -158,7 +158,7 @@ public: return allFrameNodeChildren_; } - RefPtr FindFrameNodeByIndex(uint32_t index, bool needBuild) + RefPtr FindFrameNodeByIndex(uint32_t index, bool needBuild, bool isCache) { while (cursor_ != children_.end()) { if (cursor_->startIndex > index) { @@ -168,7 +168,7 @@ public: if (cursor_->startIndex + cursor_->count > index) { auto frameNode = AceType::DynamicCast( - cursor_->node->GetFrameChildByIndex(index - cursor_->startIndex, needBuild)); + cursor_->node->GetFrameChildByIndex(index - cursor_->startIndex, needBuild, isCache)); return frameNode; } cursor_++; @@ -180,17 +180,16 @@ public: return nullptr; } - RefPtr GetFrameNodeByIndex(uint32_t index, bool needBuild) + RefPtr GetFrameNodeByIndex(uint32_t index, bool needBuild, bool isCache) { auto itor = partFrameNodeChildren_.find(index); if (itor == partFrameNodeChildren_.end()) { Build(); - auto child = FindFrameNodeByIndex(index, needBuild); - if (child) { + auto child = FindFrameNodeByIndex(index, needBuild, isCache); + if (child && !isCache) { partFrameNodeChildren_[index] = child; - return child; } - return nullptr; + return child; } return itor->second; } @@ -259,10 +258,8 @@ public: int32_t index = itor->first; if ((start <= end && index >= start && index <= end) || (start > end && (index <= end || start <= index))) { - itor->second->SetActive(true); itor++; } else { - itor->second->SetActive(false); partFrameNodeChildren_.erase(itor++); } } @@ -3092,9 +3089,9 @@ void FrameNode::SyncGeometryNode(bool needSkipSync) layoutAlgorithm_.Reset(); } -RefPtr FrameNode::GetOrCreateChildByIndex(uint32_t index, bool addToRenderTree) +RefPtr FrameNode::GetOrCreateChildByIndex(uint32_t index, bool addToRenderTree, bool isCache) { - auto child = frameProxy_->GetFrameNodeByIndex(index, true); + auto child = frameProxy_->GetFrameNodeByIndex(index, true, isCache); if (child) { child->SetSkipSyncGeometryNode(SkipSyncGeometryNode()); if (addToRenderTree) { @@ -3104,9 +3101,9 @@ RefPtr FrameNode::GetOrCreateChildByIndex(uint32_t index, bool ad return child; } -RefPtr FrameNode::GetChildByIndex(uint32_t index) +RefPtr FrameNode::GetChildByIndex(uint32_t index, bool isCache) { - return frameProxy_->GetFrameNodeByIndex(index, false); + return frameProxy_->GetFrameNodeByIndex(index, false, isCache); } int32_t FrameNode::GetChildTrueIndex(const RefPtr& child) const @@ -3213,7 +3210,7 @@ void FrameNode::MarkNeedSyncRenderTree(bool needRebuild) needSyncRenderTree_ = true; } -RefPtr FrameNode::GetFrameChildByIndex(uint32_t index, bool needBuild) +RefPtr FrameNode::GetFrameChildByIndex(uint32_t index, bool needBuild, bool isCache) { if (index != 0) { return nullptr; diff --git a/frameworks/core/components_ng/base/frame_node.h b/frameworks/core/components_ng/base/frame_node.h index 5df2a7a4439..d95758cefaa 100644 --- a/frameworks/core/components_ng/base/frame_node.h +++ b/frameworks/core/components_ng/base/frame_node.h @@ -592,8 +592,9 @@ public: return layoutProperty_; } - RefPtr GetOrCreateChildByIndex(uint32_t index, bool addToRenderTree = true) override; - RefPtr GetChildByIndex(uint32_t index) override; + RefPtr GetOrCreateChildByIndex( + uint32_t index, bool addToRenderTree = true, bool isCache = false) override; + RefPtr GetChildByIndex(uint32_t index, bool isCache = false) override; /** * @brief Get the index of Child among all FrameNode children of [this]. * Handles intermediate SyntaxNodes like LazyForEach. @@ -649,7 +650,7 @@ public: int32_t cacheCount = 0, const std::optional& itemConstraint = std::nullopt) override; void SyncGeometryNode(bool needSkipSync = false); - RefPtr GetFrameChildByIndex(uint32_t index, bool needBuild) override; + RefPtr GetFrameChildByIndex(uint32_t index, bool needBuild, bool isCache = false) override; bool CheckNeedForceMeasureAndLayout() override; bool SetParentLayoutConstraint(const SizeF& size) const override; diff --git a/frameworks/core/components_ng/base/ui_node.cpp b/frameworks/core/components_ng/base/ui_node.cpp index 6c539c6de62..da7719603eb 100644 --- a/frameworks/core/components_ng/base/ui_node.cpp +++ b/frameworks/core/components_ng/base/ui_node.cpp @@ -969,12 +969,12 @@ RefPtr UINode::GetDisappearingChildById(const std::string& id) const return nullptr; } -RefPtr UINode::GetFrameChildByIndex(uint32_t index, bool needBuild) +RefPtr UINode::GetFrameChildByIndex(uint32_t index, bool needBuild, bool isCache) { for (const auto& child : GetChildren()) { uint32_t count = static_cast(child->FrameCount()); if (count > index) { - return child->GetFrameChildByIndex(index, needBuild); + return child->GetFrameChildByIndex(index, needBuild, isCache); } index -= count; } diff --git a/frameworks/core/components_ng/base/ui_node.h b/frameworks/core/components_ng/base/ui_node.h index 357f8a0882d..b31a7403135 100644 --- a/frameworks/core/components_ng/base/ui_node.h +++ b/frameworks/core/components_ng/base/ui_node.h @@ -386,7 +386,7 @@ public: newChild->MountToParent(AceType::Claim(this), slot, false); } virtual void FastPreviewUpdateChildDone() {} - virtual RefPtr GetFrameChildByIndex(uint32_t index, bool needBuild); + virtual RefPtr GetFrameChildByIndex(uint32_t index, bool needBuild, bool isCache = false); void SetDebugLine(const std::string& line) { diff --git a/frameworks/core/components_ng/layout/layout_wrapper.h b/frameworks/core/components_ng/layout/layout_wrapper.h index 50296ad323c..322a4479c13 100644 --- a/frameworks/core/components_ng/layout/layout_wrapper.h +++ b/frameworks/core/components_ng/layout/layout_wrapper.h @@ -58,8 +58,9 @@ public: virtual const RefPtr& GetGeometryNode() const = 0; virtual const RefPtr& GetLayoutProperty() const = 0; - virtual RefPtr GetOrCreateChildByIndex(uint32_t index, bool addToRenderTree = true) = 0; - virtual RefPtr GetChildByIndex(uint32_t index) = 0; + virtual RefPtr GetOrCreateChildByIndex( + uint32_t index, bool addToRenderTree = true, bool isCache = false) = 0; + virtual RefPtr GetChildByIndex(uint32_t index, bool isCache = false) = 0; virtual const std::list>& GetAllChildrenWithBuild(bool addToRenderTree = true) = 0; virtual void RemoveChildInRenderTree(uint32_t index) = 0; virtual void RemoveAllChildInRenderTree() = 0; diff --git a/frameworks/core/components_ng/layout/layout_wrapper_node.cpp b/frameworks/core/components_ng/layout/layout_wrapper_node.cpp index 49dc54d4f8d..a0682eb74d2 100644 --- a/frameworks/core/components_ng/layout/layout_wrapper_node.cpp +++ b/frameworks/core/components_ng/layout/layout_wrapper_node.cpp @@ -57,7 +57,7 @@ void LayoutWrapperNode::AppendChild(const RefPtr& child, bool overlayChild_ = child; } } -RefPtr LayoutWrapperNode::GetOrCreateChildByIndex(uint32_t index, bool addToRenderTree) +RefPtr LayoutWrapperNode::GetOrCreateChildByIndex(uint32_t index, bool addToRenderTree, bool isCache) { if ((index >= static_cast(currentChildCount_)) || (index < 0)) { return nullptr; diff --git a/frameworks/core/components_ng/layout/layout_wrapper_node.h b/frameworks/core/components_ng/layout/layout_wrapper_node.h index 68ee780467e..e7bbc7df6ac 100644 --- a/frameworks/core/components_ng/layout/layout_wrapper_node.h +++ b/frameworks/core/components_ng/layout/layout_wrapper_node.h @@ -102,9 +102,10 @@ public: // Calling these two method will mark the node as in use by default, nodes marked as use state will be added to the // render area, and nodes in the render area will be mounted on the render tree after the layout is complete. You // can call the RemoveChildInRenderTree method to explicitly remove the node from the area to be rendered. - RefPtr GetOrCreateChildByIndex(uint32_t index, bool addToRenderTree = true) override; + RefPtr GetOrCreateChildByIndex( + uint32_t index, bool addToRenderTree = true, bool isCache = false) override; const std::list>& GetAllChildrenWithBuild(bool addToRenderTree = true) override; - RefPtr GetChildByIndex(uint32_t index) override + RefPtr GetChildByIndex(uint32_t index, bool isCache = false) override { return nullptr; } diff --git a/frameworks/core/components_ng/pattern/custom/custom_node.cpp b/frameworks/core/components_ng/pattern/custom/custom_node.cpp index a7c6ee34861..924ceb22565 100644 --- a/frameworks/core/components_ng/pattern/custom/custom_node.cpp +++ b/frameworks/core/components_ng/pattern/custom/custom_node.cpp @@ -162,9 +162,9 @@ void CustomNode::MarkNeedSyncRenderTree(bool needRebuild) } } -RefPtr CustomNode::GetFrameChildByIndex(uint32_t index, bool needBuild) +RefPtr CustomNode::GetFrameChildByIndex(uint32_t index, bool needBuild, bool isCache) { Render(); - return UINode::GetFrameChildByIndex(index, needBuild); + return UINode::GetFrameChildByIndex(index, needBuild, isCache); } } // namespace OHOS::Ace::NG diff --git a/frameworks/core/components_ng/pattern/custom/custom_node.h b/frameworks/core/components_ng/pattern/custom/custom_node.h index ce6d371b745..0ea3ca6d302 100644 --- a/frameworks/core/components_ng/pattern/custom/custom_node.h +++ b/frameworks/core/components_ng/pattern/custom/custom_node.h @@ -77,7 +77,7 @@ public: } void MarkNeedSyncRenderTree(bool needRebuild = false) override; - RefPtr GetFrameChildByIndex(uint32_t index, bool needBuild) override; + RefPtr GetFrameChildByIndex(uint32_t index, bool needBuild, bool isCache = false) override; bool RenderCustomChild(int64_t deadline) override; void SetJSViewActive(bool active) override; diff --git a/frameworks/core/components_ng/pattern/grid/grid_scroll/grid_scroll_layout_algorithm.cpp b/frameworks/core/components_ng/pattern/grid/grid_scroll/grid_scroll_layout_algorithm.cpp index 75b0054adb9..324e80d23bb 100644 --- a/frameworks/core/components_ng/pattern/grid/grid_scroll/grid_scroll_layout_algorithm.cpp +++ b/frameworks/core/components_ng/pattern/grid/grid_scroll/grid_scroll_layout_algorithm.cpp @@ -328,7 +328,7 @@ void GridScrollLayoutAlgorithm::LayoutBackwardCachedLine(LayoutWrapper* layoutWr } else { offset.SetY(crossOffset); } - auto wrapper = layoutWrapper->GetChildByIndex(itemIdex); + auto wrapper = layoutWrapper->GetChildByIndex(itemIdex, true); if (!wrapper || wrapper->CheckNeedForceMeasureAndLayout()) { continue; } @@ -392,7 +392,7 @@ void GridScrollLayoutAlgorithm::LayoutForwardCachedLine(LayoutWrapper* layoutWra } else { offset.SetY(crossOffset); } - auto wrapper = layoutWrapper->GetChildByIndex(itemIdex); + auto wrapper = layoutWrapper->GetChildByIndex(itemIdex, true); if (!wrapper || wrapper->CheckNeedForceMeasureAndLayout()) { continue; } @@ -1894,7 +1894,7 @@ float GridScrollLayoutAlgorithm::FillNewCacheLineBackward( auto currentIndex = gridLayoutInfo_.endIndex_ + 1; for (uint32_t i = (line->second.empty() ? 0 : line->second.rbegin()->first); i < crossCount_ - 1; i++) { // Step1. Get wrapper of [GridItem] - auto itemWrapper = layoutWrapper->GetChildByIndex(currentIndex); + auto itemWrapper = layoutWrapper->GetChildByIndex(currentIndex, true); if (!itemWrapper || itemWrapper->CheckNeedForceMeasureAndLayout()) { for (uint32_t y = i; y < crossCount_ - 1; y++) { predictBuildList_.emplace_back(currentIndex++); @@ -1937,7 +1937,7 @@ float GridScrollLayoutAlgorithm::FillNewCacheLineBackward( break; } // Step1. Get wrapper of [GridItem] - auto itemWrapper = layoutWrapper->GetChildByIndex(currentIndex); + auto itemWrapper = layoutWrapper->GetChildByIndex(currentIndex, true); if (!itemWrapper || itemWrapper->CheckNeedForceMeasureAndLayout()) { for (uint32_t x = i; x < crossCount_; x++) { predictBuildList_.emplace_back(currentIndex++); @@ -2017,7 +2017,7 @@ void GridScrollLayoutAlgorithm::CompeleteItemCrossPosition( { for (auto item : items) { auto currentIndex = item.second; - auto itemWrapper = layoutWrapper->GetChildByIndex(currentIndex); + auto itemWrapper = layoutWrapper->GetChildByIndex(currentIndex, true); if (!itemWrapper || itemWrapper->CheckNeedForceMeasureAndLayout()) { continue; } @@ -2056,7 +2056,7 @@ void GridScrollLayoutAlgorithm::PostIdleTask(RefPtr frameNode, const if (GetSysTimestamp() > deadline) { break; } - auto wrapper = frameNode->GetOrCreateChildByIndex(*it, false); + auto wrapper = frameNode->GetOrCreateChildByIndex(*it, false, true); needMarkDirty = PredictBuildItem(wrapper, param.layoutConstraint) || needMarkDirty; param.items.erase(it++); } diff --git a/frameworks/core/components_ng/pattern/list/list_lanes_layout_algorithm.cpp b/frameworks/core/components_ng/pattern/list/list_lanes_layout_algorithm.cpp index b843e75f2a1..1d9773a3ad0 100644 --- a/frameworks/core/components_ng/pattern/list/list_lanes_layout_algorithm.cpp +++ b/frameworks/core/components_ng/pattern/list/list_lanes_layout_algorithm.cpp @@ -391,7 +391,7 @@ std::list ListLanesLayoutAlgorithm::LayoutCachedALineForward(LayoutWrap int32_t cnt = 0; int32_t lanes = lanes_ > 1 ? lanes_ : 1; for (int32_t i = 0; i < lanes && index + i <= GetMaxListItemIndex(); i++) { - auto wrapper = layoutWrapper->GetChildByIndex(index + i); + auto wrapper = layoutWrapper->GetChildByIndex(index + i, true); if (!wrapper || CheckNeedMeasure(wrapper)) { predictBuildList.emplace_back(index + i); continue; @@ -415,7 +415,7 @@ std::list ListLanesLayoutAlgorithm::LayoutCachedALineForward(LayoutWrap startPos = endPos + GetSpaceWidth(); auto startIndex = index; for (auto& pos: posMap) { - auto wrapper = layoutWrapper->GetChildByIndex(pos.first); + auto wrapper = layoutWrapper->GetChildByIndex(pos.first, true); if (!wrapper) { break; } @@ -440,7 +440,7 @@ std::list ListLanesLayoutAlgorithm::LayoutCachedALineBackward(LayoutWra int32_t lanes = lanes_ > 1 ? lanes_ : 1; for (int32_t i = 0; i < lanes && index >= 0; i++) { auto idx = index - i; - auto wrapper = layoutWrapper->GetChildByIndex(idx); + auto wrapper = layoutWrapper->GetChildByIndex(idx, true); if (!wrapper || CheckNeedMeasure(wrapper)) { predictBuildList.emplace_back(idx); continue; @@ -465,7 +465,7 @@ std::list ListLanesLayoutAlgorithm::LayoutCachedALineBackward(LayoutWra endPos = startPos - GetSpaceWidth(); auto startIndex = index - cnt + 1; for (auto& pos: posMap) { - auto wrapper = layoutWrapper->GetChildByIndex(pos.first); + auto wrapper = layoutWrapper->GetChildByIndex(pos.first, true); if (!wrapper) { break; } diff --git a/frameworks/core/components_ng/pattern/list/list_layout_algorithm.cpp b/frameworks/core/components_ng/pattern/list/list_layout_algorithm.cpp index 7e1554daa52..348c9ad3dec 100644 --- a/frameworks/core/components_ng/pattern/list/list_layout_algorithm.cpp +++ b/frameworks/core/components_ng/pattern/list/list_layout_algorithm.cpp @@ -1469,7 +1469,7 @@ std::list ListLayoutAlgorithm::LayoutCachedItem(LayoutWrapper* layoutWr auto currPos = itemPosition_.rbegin()->second.endPos + spaceWidth_; for (int32_t i = 0; i < cacheCount && currIndex + i < totalItemCount_; i++) { int32_t index = currIndex + i; - auto wrapper = layoutWrapper->GetChildByIndex(index); + auto wrapper = layoutWrapper->GetChildByIndex(index, true); if (!wrapper || CheckNeedMeasure(wrapper)) { predictBuildList.emplace_back(index); continue; @@ -1490,7 +1490,7 @@ std::list ListLayoutAlgorithm::LayoutCachedItem(LayoutWrapper* layoutWr currPos = itemPosition_.begin()->second.startPos - spaceWidth_; for (int32_t i = 0; i < cacheCount && currIndex - i >= 0; i++) { int32_t index = currIndex - i; - auto wrapper = layoutWrapper->GetChildByIndex(index); + auto wrapper = layoutWrapper->GetChildByIndex(index, true); if (!wrapper || CheckNeedMeasure(wrapper)) { predictBuildList.emplace_back(index); continue; @@ -1551,7 +1551,7 @@ void ListLayoutAlgorithm::PostIdleTask(RefPtr frameNode, const ListPr if (GetSysTimestamp() > deadline) { break; } - auto wrapper = frameNode->GetOrCreateChildByIndex(*it, false); + auto wrapper = frameNode->GetOrCreateChildByIndex(*it, false, true); if (wrapper && wrapper->GetHostNode() && !wrapper->GetHostNode()->RenderCustomChild(deadline)) { break; } diff --git a/frameworks/core/components_ng/syntax/lazy_for_each_builder.cpp b/frameworks/core/components_ng/syntax/lazy_for_each_builder.cpp index 8a63346185e..0a531312fc5 100644 --- a/frameworks/core/components_ng/syntax/lazy_for_each_builder.cpp +++ b/frameworks/core/components_ng/syntax/lazy_for_each_builder.cpp @@ -16,7 +16,8 @@ #include "core/components_ng/syntax/lazy_for_each_builder.h" namespace OHOS::Ace::NG { - std::pair> LazyForEachBuilder::GetChildByIndex(int32_t index, bool needBuild) + std::pair> LazyForEachBuilder::GetChildByIndex( + int32_t index, bool needBuild, bool isCache) { auto iter = cachedItems_.find(index); if (iter != cachedItems_.end()) { @@ -25,9 +26,13 @@ namespace OHOS::Ace::NG { } auto keyIter = expiringItem_.find(iter->second.first); if (keyIter != expiringItem_.end() && keyIter->second.second) { - iter->second.second = keyIter->second.second; - expiringItem_.erase(keyIter); - return iter->second; + if (!isCache) { + iter->second.second = keyIter->second.second; + expiringItem_.erase(keyIter); + return iter->second; + } else { + return { keyIter->first, keyIter->second.second }; + } } } @@ -35,7 +40,10 @@ namespace OHOS::Ace::NG { ACE_SCOPED_TRACE("Builder:BuildLazyItem [%d]", index); auto itemInfo = OnGetChildByIndex(index, expiringItem_); CHECK_NULL_RETURN(itemInfo.second, itemInfo); - { + if (isCache) { + expiringItem_.emplace(itemInfo.first, LazyForEachCacheChild(index, itemInfo.second)); + cachedItems_[index] = LazyForEachChild(itemInfo.first, nullptr); + } else { cachedItems_[index] = itemInfo; } return itemInfo; 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 3d066aedc51..63750f5e619 100644 --- a/frameworks/core/components_ng/syntax/lazy_for_each_builder.h +++ b/frameworks/core/components_ng/syntax/lazy_for_each_builder.h @@ -52,7 +52,7 @@ public: return OnGetTotalCount(); } - std::pair> GetChildByIndex(int32_t index, bool needBuild); + std::pair> GetChildByIndex(int32_t index, bool needBuild, bool isCache = false); void ExpandChildrenOnInitial() { @@ -165,12 +165,17 @@ public: } } - void SetActiveChildRange(int32_t start, int32_t end) + bool SetActiveChildRange(int32_t start, int32_t end) { + bool needBuild = false; for (auto& [index, node] : cachedItems_) { if ((start <= end && start <= index && end >= index) || (start > end && (index <= end || index >= start))) { if (node.second) { + auto frameNode = AceType::DynamicCast(node.second->GetFrameChildByIndex(0, true)); + if (frameNode) { + frameNode->SetActive(true); + } continue; } auto keyIter = expiringItem_.find(node.first); @@ -182,6 +187,7 @@ public: frameNode->SetActive(true); } } + needBuild = true; continue; } if (!node.second) { @@ -192,7 +198,9 @@ public: frameNode->SetActive(false); } expiringItem_.try_emplace(node.first, LazyForEachCacheChild(index, std::move(node.second))); + needBuild = true; } + return needBuild; } void SetFlagForGeneratedItem(PropertyChangeFlag propertyChangeFlag) 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 67e5038eb51..bfe7e6d2ec1 100644 --- a/frameworks/core/components_ng/syntax/lazy_for_each_node.cpp +++ b/frameworks/core/components_ng/syntax/lazy_for_each_node.cpp @@ -218,28 +218,33 @@ void LazyForEachNode::MarkNeedSyncRenderTree(bool needRebuild) } } -RefPtr LazyForEachNode::GetFrameChildByIndex(uint32_t index, bool needBuild) +RefPtr LazyForEachNode::GetFrameChildByIndex(uint32_t index, bool needBuild, bool isCache) { - if (index < static_cast(FrameCount())) { - auto child = builder_->GetChildByIndex(index, needBuild); - if (child.second) { - if (isActive_) { - child.second->SetJSViewActive(true); - } - if (child.second->GetDepth() != GetDepth() + 1) { - child.second->SetDepth(GetDepth() + 1); - } - MarkNeedSyncRenderTree(); - children_.clear(); - child.second->SetParent(WeakClaim(this)); - if (IsOnMainTree()) { - child.second->AttachToMainTree(); - } - PostIdleTask(); - return child.second->GetFrameChildByIndex(0, needBuild); - } + if (index >= static_cast(FrameCount())) { + return nullptr; + } + auto child = builder_->GetChildByIndex(index, needBuild, isCache); + if (!child.second) { + return nullptr; + } + if (isCache) { + child.second->SetParent(WeakClaim(this)); + return child.second->GetFrameChildByIndex(0, needBuild); + } + if (isActive_) { + child.second->SetJSViewActive(true); + } + if (child.second->GetDepth() != GetDepth() + 1) { + child.second->SetDepth(GetDepth() + 1); + } + MarkNeedSyncRenderTree(); + children_.clear(); + child.second->SetParent(WeakClaim(this)); + if (IsOnMainTree()) { + child.second->AttachToMainTree(); } - return nullptr; + PostIdleTask(); + return child.second->GetFrameChildByIndex(0, needBuild); } int32_t LazyForEachNode::GetIndexByUINode(const RefPtr& uiNode) const @@ -288,10 +293,11 @@ void LazyForEachNode::DoSetActiveChildRange(int32_t start, int32_t end) if (!builder_) { return; } - children_.clear(); - builder_->SetActiveChildRange(start, end); - MarkNeedSyncRenderTree(); - PostIdleTask(); + if (builder_->SetActiveChildRange(start, end)) { + children_.clear(); + MarkNeedSyncRenderTree(); + PostIdleTask(); + } } const std::list>& LazyForEachNode::GetChildren() const diff --git a/frameworks/core/components_ng/syntax/lazy_for_each_node.h b/frameworks/core/components_ng/syntax/lazy_for_each_node.h index 901f3a578f9..92047b46fda 100644 --- a/frameworks/core/components_ng/syntax/lazy_for_each_node.h +++ b/frameworks/core/components_ng/syntax/lazy_for_each_node.h @@ -104,7 +104,7 @@ public: void MarkNeedSyncRenderTree(bool needRebuild = false) override; void BuildAllChildren(); - RefPtr GetFrameChildByIndex(uint32_t index, bool needBuild) override; + RefPtr GetFrameChildByIndex(uint32_t index, bool needBuild, bool isCache = false) override; void DoRemoveChildInRenderTree(uint32_t index, bool isAll) override; void DoSetActiveChildRange(int32_t start, int32_t end) override; -- Gitee