From 80b32d0ddc4aa3b1d663d44631649b6adb72c3a9 Mon Sep 17 00:00:00 2001 From: tomkl123 Date: Wed, 24 Jul 2024 15:21:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=86=E4=BD=BF=E7=94=A8Gr?= =?UTF-8?q?idLayoutOptions=E5=9C=BA=E6=99=AF=E4=B8=8B=EF=BC=8C=E9=95=BF?= =?UTF-8?q?=E8=B7=9D=E7=A6=BB=E8=B7=B3=E8=BD=AC=E5=8F=AF=E8=83=BD=E5=9B=9E?= =?UTF-8?q?=E5=88=B0=E5=BA=95=E9=83=A8=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: tomkl123 Change-Id: I34ddc07ceb847261a9bde2d9b1e12503f21f8827 --- .../pattern/grid/grid_layout_info.cpp | 14 +++++------ .../grid/grid_option_layout_test_ng.cpp | 24 +++++++++++++++++++ 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/frameworks/core/components_ng/pattern/grid/grid_layout_info.cpp b/frameworks/core/components_ng/pattern/grid/grid_layout_info.cpp index 8d22206e53a..a7db06c4325 100644 --- a/frameworks/core/components_ng/pattern/grid/grid_layout_info.cpp +++ b/frameworks/core/components_ng/pattern/grid/grid_layout_info.cpp @@ -401,7 +401,7 @@ float GridLayoutInfo::GetIrregularHeight(float mainGap) const void GridLayoutInfo::SkipStartIndexByOffset(const GridLayoutOptions& options, float mainGap) { - auto targetContent = currentHeight_ - (currentOffset_ - prevOffset_); + float targetContent = currentHeight_ - (currentOffset_ - prevOffset_); if (LessOrEqual(targetContent, 0.0)) { currentOffset_ = 0.0f; startIndex_ = 0; @@ -422,12 +422,12 @@ void GridLayoutInfo::SkipStartIndexByOffset(const GridLayoutOptions& options, fl lastRegularMainSize_ = regularHeight; } - auto firstIrregularIndex = *(options.irregularIndexes.begin()); + int32_t firstIrregularIndex = *(options.irregularIndexes.begin()); float totalHeight = AddLinesInBetween(-1, firstIrregularIndex, crossCount_, regularHeight); - auto lastIndex = firstIrregularIndex; - auto lastHeight = 0.0f; + int32_t lastIndex = GreatNotEqual(totalHeight, targetContent) ? 0 : firstIrregularIndex; + float lastHeight = 0.0f; - for (auto idx : options.irregularIndexes) { + for (int32_t idx : options.irregularIndexes) { if (GreatOrEqual(totalHeight, targetContent)) { break; } @@ -436,9 +436,9 @@ void GridLayoutInfo::SkipStartIndexByOffset(const GridLayoutOptions& options, fl totalHeight += AddLinesInBetween(lastIndex, idx, crossCount_, regularHeight); lastIndex = idx; } - auto lines = static_cast(std::floor((targetContent - lastHeight) / regularHeight)); + int32_t lines = static_cast(std::floor((targetContent - lastHeight) / regularHeight)); currentOffset_ = lastHeight + lines * regularHeight - targetContent; - auto startIdx = lines * crossCount_ + lastIndex; + int32_t startIdx = lines * crossCount_ + lastIndex; startIndex_ = std::min(startIdx, childrenCount_ - 1); } diff --git a/test/unittest/core/pattern/grid/grid_option_layout_test_ng.cpp b/test/unittest/core/pattern/grid/grid_option_layout_test_ng.cpp index 07285367fc2..a94f6afbc16 100644 --- a/test/unittest/core/pattern/grid/grid_option_layout_test_ng.cpp +++ b/test/unittest/core/pattern/grid/grid_option_layout_test_ng.cpp @@ -796,4 +796,28 @@ HWTEST_F(GridOptionLayoutTestNg, OutOfBounds001, TestSize.Level1) EXPECT_GT(GetChildRect(frameNode_, 29).Bottom(), GRID_HEIGHT); EXPECT_FALSE(pattern_->IsOutOfBoundary(true)); } + +/** + * @tc.name: ScrollTo001 + * @tc.desc: Test ScrollTo Function. + * @tc.type: FUNC + */ +HWTEST_F(GridOptionLayoutTestNg, ScrollTo001, TestSize.Level1) +{ + GridLayoutOptions option; + option.irregularIndexes = { 45 }; + GridModelNG model = CreateGrid(); + model.SetLayoutOptions(option); + model.SetColumnsTemplate("1fr"); + CreateFixedItems(50); + CreateDone(frameNode_); + + pattern_->ScrollTo(ITEM_HEIGHT * 40); + FlushLayoutTask(frameNode_); + EXPECT_EQ(pattern_->GetGridLayoutInfo().startIndex_, 40); + + pattern_->ScrollTo(ITEM_HEIGHT * 20); + FlushLayoutTask(frameNode_); + EXPECT_EQ(pattern_->GetGridLayoutInfo().startIndex_, 20); +} } // namespace OHOS::Ace::NG