Skip to content

Commit

Permalink
fix(listview): [Android] Correct scroll offsets if necessary when scr…
Browse files Browse the repository at this point in the history
…olling upwards in list
  • Loading branch information
davidjohnoliver committed Oct 18, 2021
1 parent 66d75a7 commit 5e8bce5
Showing 1 changed file with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,8 @@ private int ComputeScrollRange(RecyclerView.State state, Orientation orientation
remainingGroupExtent = remainingGroups * lastGroup.HeaderExtent;
}

CorrectForEstimationErrors();

var range = ContentOffset + remainingItemExtent + remainingGroupExtent + headerExtent + footerExtent +
//TODO: An inline group header might actually be the view at the bottom of the viewport, we should take this into account
GetChildEndWithMargin(base.GetChildAt(FirstItemView + ItemViewCount - 1));
Expand All @@ -732,6 +734,26 @@ private int ComputeScrollRange(RecyclerView.State state, Orientation orientation
return range;
}

/// <summary>
/// Correct the scroll offset, eg if items were added/removed or had their databound heights changed while they were scrolled out
/// of view.
/// </summary>
private void CorrectForEstimationErrors()
{
if (ContentOffset < 0)
{
// Scroll offset should always be non-negative
ContentOffset = 0;
}

var firstVisible = GetFirstVisibleIndexPath();
if (firstVisible.Row == 0 && firstVisible.Section == 0)
{
// If first item is in view, we can set ContentOffset exactly
ContentOffset = -GetContentStart();
}
}

/// <summary>
/// Update the internal state of the layout, as well as 'floating' views like group headers, when the scrolled offset changes.
/// </summary>
Expand All @@ -742,7 +764,9 @@ private int ComputeScrollRange(RecyclerView.State state, Orientation orientation
private void ApplyOffset(int delta)
{
ContentOffset -= delta;
Debug.Assert(ContentOffset >= 0, "ContentOffset must be non-negative.");

CorrectForEstimationErrors();

foreach (var group in _groups)
{
group.Start += delta;
Expand Down

0 comments on commit 5e8bce5

Please sign in to comment.