From 2a3534a7e6baaaf5f8154682361ba07ef13225d0 Mon Sep 17 00:00:00 2001 From: zcdqs Date: Mon, 27 Mar 2023 22:43:03 +0800 Subject: [PATCH] get start item for Grid without columnStart Signed-off-by: zcdqs Change-Id: I7dc331191bf03dc2db980131b046ed8c73965a3a --- .../pattern/grid/grid_layout_info.h | 3 ++ .../grid_scroll_layout_algorithm.cpp | 47 ++++++++++++++----- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/frameworks/core/components_ng/pattern/grid/grid_layout_info.h b/frameworks/core/components_ng/pattern/grid/grid_layout_info.h index 78e790a40a5..6f04c6cced9 100644 --- a/frameworks/core/components_ng/pattern/grid/grid_layout_info.h +++ b/frameworks/core/components_ng/pattern/grid/grid_layout_info.h @@ -105,6 +105,9 @@ struct GridLayoutInfo { // rect of grid item dragged in RectF currentRect_; + // Grid has GridItem whose columnEnd - columnStart > 0 + bool hasBigItem_; + private: int32_t GetItemIndexByPosition(int32_t position); int32_t GetPositionByItemIndex(int32_t itemIndex); 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 f568011a013..269627a348f 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 @@ -958,6 +958,9 @@ int32_t GridScrollLayoutAlgorithm::MeasureChild(const SizeF& frameSize, int32_t int32_t itemColStart = childLayoutProperty->GetColumnStart().value_or(-1); int32_t itemRowSpan = std::max(childLayoutProperty->GetRowEnd().value_or(-1) - itemRowStart + 1, 1); int32_t itemColSpan = std::max(childLayoutProperty->GetColumnEnd().value_or(-1) - itemColStart + 1, 1); + if (itemRowSpan > 1 || itemColSpan > 1) { + gridLayoutInfo_.hasBigItem_ = true; + } auto mainStart = axis_ == Axis::VERTICAL ? itemRowStart : itemColStart; auto crossStart = axis_ == Axis::VERTICAL ? itemColStart : itemRowStart; auto mainSpan = axis_ == Axis::VERTICAL ? itemRowSpan : itemColSpan; @@ -1005,6 +1008,9 @@ int32_t GridScrollLayoutAlgorithm::MeasureChildPlaced(const SizeF& frameSize, in int32_t itemColStart = childLayoutProperty->GetColumnStart().value_or(-1); int32_t itemRowSpan = std::max(childLayoutProperty->GetRowEnd().value_or(-1) - itemRowStart + 1, 1); int32_t itemColSpan = std::max(childLayoutProperty->GetColumnEnd().value_or(-1) - itemColStart + 1, 1); + if (itemRowSpan > 1 || itemColSpan > 1) { + gridLayoutInfo_.hasBigItem_ = true; + } auto crossSpan = axis_ == Axis::VERTICAL ? itemColSpan : itemRowSpan; if (static_cast(crossStart + crossSpan) > crossCount_) { LOGE("cross not enough"); @@ -1081,21 +1087,36 @@ int32_t GridScrollLayoutAlgorithm::GetStartingItem(LayoutWrapper* layoutWrapper, currentIndex = currentIndex < layoutWrapper->GetTotalChildCount() ? currentIndex : layoutWrapper->GetTotalChildCount() - 1; auto index = currentIndex; - while (index > 0) { - // Step1. Get wrapper of [GridItem] - auto childLayoutWrapper = layoutWrapper->GetOrCreateChildByIndex(index); - if (!childLayoutWrapper) { - LOGE("GridItem wrapper of index %{public}u null", index); - break; + if (gridLayoutInfo_.hasBigItem_) { + while (index > 0) { + auto childLayoutWrapper = layoutWrapper->GetOrCreateChildByIndex(index); + if (!childLayoutWrapper) { + LOGE("GridItem wrapper of index %{public}u null", index); + break; + } + auto childLayoutProperty = DynamicCast(childLayoutWrapper->GetLayoutProperty()); + CHECK_NULL_RETURN(childLayoutProperty, 0); + auto crossIndex = childLayoutProperty->GetCustomCrossIndex(axis_); + if (crossIndex == 0) { + firstIndex = index; + break; + } + --index; } - auto childLayoutProperty = DynamicCast(childLayoutWrapper->GetLayoutProperty()); - CHECK_NULL_RETURN(childLayoutProperty, 0); - auto crossIndex = childLayoutProperty->GetCustomCrossIndex(axis_); - if (crossIndex == 0) { - firstIndex = index; - break; + } else { + while (index > 0) { + // need to obtain the item node in order and by step one + auto childLayoutWrapper = layoutWrapper->GetOrCreateChildByIndex(index); + if (!childLayoutWrapper) { + LOGE("GridItem wrapper of index %{public}u null", index); + break; + } + if (index % gridLayoutInfo_.crossCount_ == 0) { + firstIndex = index; + break; + } + --index; } - --index; } return firstIndex; } -- Gitee