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 db27195dd828662c090f2295e3ef4c36bd58942f..7427ce97eadcafff742748b62d10c8408e6d376a 100644 --- a/frameworks/core/components_ng/pattern/list/list_layout_algorithm.cpp +++ b/frameworks/core/components_ng/pattern/list/list_layout_algorithm.cpp @@ -728,7 +728,7 @@ std::pair ListLayoutAlgorithm::FindIndexAndDeltaInPosMap(float d // The delta and index in the return value satisfies the following constraints: // 1) delta >= -spaceWidth_; // 2) delta < posMap[index].mainSize. - if (!posMap_) { + if (!posMap_ || itemPosition_.empty()) { return { -1, 0.f }; } // itemPosition_ has been checked nonNull before the func is called. diff --git a/test/unittest/core/pattern/list/list_algorithm_test_ng.cpp b/test/unittest/core/pattern/list/list_algorithm_test_ng.cpp index 7374d3e1b64fc79e582854b21feecb52f4e0503d..8bf6c0afc165caa9d8d1c5b5fbb7976bf5da05fc 100644 --- a/test/unittest/core/pattern/list/list_algorithm_test_ng.cpp +++ b/test/unittest/core/pattern/list/list_algorithm_test_ng.cpp @@ -1647,6 +1647,22 @@ HWTEST_F(ListAlgorithmTestNg, PredictBuildItem002, TestSize.Level1) EXPECT_TRUE(result); } +/** + * @tc.name: FindIndexAndDeltaInPosMap001 + * @tc.desc: Test ListItemLayoutAlgorithm FindIndexAndDeltaInPosMap + * @tc.type: FUNC + */ +HWTEST_F(ListAlgorithmTestNg, FindIndexAndDeltaInPosMap001, TestSize.Level1) +{ + RefPtr listPattern = AceType::MakeRefPtr(); + ASSERT_NE(listPattern, nullptr); + auto layoutAlgorithm = listPattern->CreateLayoutAlgorithm(); + ASSERT_NE(layoutAlgorithm, nullptr); + auto [index, delta] = layoutAlgorithm->FindIndexAndDeltaInPosMap(0.0f); + ASSERT_EQ(index, -1); + ASSERT_EQ(delta, 0.0f); +} + /** * @tc.name: CheckAndUpdateCurOffset001 * @tc.desc: Test ListItemLayoutAlgorithm CheckAndUpdateCurOffset