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