Share via

SmoothScrollToPosition Scrolls Wrong Amount From Partially Visible Items

Nathan Sokalski 4,111 Reputation points
2021-11-29T20:44:31.673+00:00

I have a SmoothScroller that inherits from LinearSmoothScroller, and I override CalculateDtToFit as follows:

public override int CalculateDtToFit(int viewStart, int viewEnd, int boxStart, int boxEnd, int snapPreference)
{
    int height = viewEnd - viewStart;
    int result;
    if (((MainActivity)Xamarin.Essentials.Platform.CurrentActivity).rvPlayerNamesInput.GetAdapter().ItemCount == 1)
    { result = base.CalculateDtToFit(viewStart, viewEnd, boxStart, boxEnd, LinearSmoothScroller.SnapToEnd); }
    else if (this.TargetPosition == 0)
    { result = base.CalculateDtToFit(viewStart, viewEnd + height, boxStart, boxEnd, LinearSmoothScroller.SnapToEnd); }
    else if (this.TargetPosition == ((MainActivity)Xamarin.Essentials.Platform.CurrentActivity).rvPlayerNamesInput.GetAdapter().ItemCount - 1)
    { result = base.CalculateDtToFit(viewStart - height, viewEnd, boxStart, boxEnd, LinearSmoothScroller.SnapToEnd); }
    else
    { result = base.CalculateDtToFit(viewStart - height, viewEnd + height, boxStart, boxEnd, LinearSmoothScroller.SnapToEnd); }
    return result;
}

When I call SmoothScrollToPosition(x) to scroll to a RecyclerView item that is completely visible, everything works exactly as expected. However, for items that are only partially visible (not completely offscreen, just not 100% within view), it does not. What it looks like it is doing for the partially visible items is using base.CalculateDtToFit with the original values & SnapToAny rather than what I have in the code above. However, I have confirmed that the code above is being called & returning the desired values (for both completely & partially visible items), yet it only scrolls the returned value for completely visible items (partially visible items do scroll, but not by the amount returned here). This override is always called & returns the desired value, so why does it not always use the returned value?

Developer technologies | .NET | Xamarin

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.