修复了使用GridLayoutOptions场景下,长距离跳转可能回到底部的bug

Signed-off-by: tomkl123 <wangyansong11@huawei.com>
Change-Id: I34ddc07ceb847261a9bde2d9b1e12503f21f8827
This commit is contained in:
tomkl123 2024-07-24 15:21:43 +08:00
parent a135c6b9e5
commit 80b32d0ddc
2 changed files with 31 additions and 7 deletions

View File

@ -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<int32_t>(std::floor((targetContent - lastHeight) / regularHeight));
int32_t lines = static_cast<int32_t>(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);
}

View File

@ -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