I use the following OnScrollListener to synchronize the scrolling of several RecyclerView(s):
public class RoundScrollEvent : RecyclerView.OnScrollListener
{
public static int ScrollY;
public override void OnScrolled(RecyclerView rv, int dx, int dy)
{
if (dy == 0) { return; }
base.OnScrolled(rv, dx, dy);
RoundScrollEvent.ScrollY += dy;
RecyclerView rvScores;
RecyclerView rvRoundNumbers;
if (rv.Id == Resource.Id.rvRoundNumbers)
{
rvRoundNumbers = rv;
rvScores = ((GridLayout)rv.Parent).FindViewById<RecyclerView>(Resource.Id.rvScores);
}
else
{
rvScores = rv.Parent as RecyclerView;
rvRoundNumbers = ((GridLayout)rvScores.Parent).FindViewById<RecyclerView>(Resource.Id.rvRoundNumbers);
}
rvRoundNumbers.ClearOnScrollListeners();
for (int playerindex = 0; playerindex < rvScores.ChildCount; playerindex++)
{ (rvScores.GetChildViewHolder(rvScores.GetChildAt(playerindex)) as ScoresViewHolder).rvPoints.ClearOnScrollListeners(); }
((LinearLayoutManager)rvRoundNumbers.GetLayoutManager()).ScrollToPositionWithOffset(0, -RoundScrollEvent.ScrollY);
for (int playerindex = 0; playerindex < rvScores.ChildCount; playerindex++)
{ ((LinearLayoutManager)(rvScores.GetChildViewHolder(rvScores.GetChildAt(playerindex)) as ScoresViewHolder).rvPoints.GetLayoutManager()).ScrollToPositionWithOffset(0, -RoundScrollEvent.ScrollY); }
rvRoundNumbers.AddOnScrollListener(new RoundScrollEvent());
for (int playerindex = 0; playerindex < rvScores.ChildCount; playerindex++)
{ (rvScores.GetChildViewHolder(rvScores.GetChildAt(playerindex)) as ScoresViewHolder).rvPoints.AddOnScrollListener(new RoundScrollEvent()); }
}
}
I also have the following LinearSmoothScroller:
public class RoundSmoothScroller : LinearSmoothScroller
{
public RoundSmoothScroller(Context context) : base(context) { }
public override int CalculateDtToFit(int viewStart, int viewEnd, int boxStart, int boxEnd, int snapPreference)
{
if (this.TargetPosition == 0) { return base.CalculateDtToFit(viewStart, viewEnd, boxStart, boxEnd, LinearSmoothScroller.SnapToStart); }
else if (this.TargetPosition == 1) { return base.CalculateDtToFit((int)(viewStart - 27 * Application.Context.Resources.DisplayMetrics.Density), viewEnd, boxStart, boxEnd, LinearSmoothScroller.SnapToStart); }
else if (this.TargetPosition == MainActivity.Players[MainActivity.CurrentPlayerIndex].Scores.Count) { return base.CalculateDtToFit((int)(viewStart - (((MainActivity)Xamarin.Essentials.Platform.CurrentActivity).Cumulative ? 27 : 54) * Application.Context.Resources.DisplayMetrics.Density), (int)(viewStart + 81 * Application.Context.Resources.DisplayMetrics.Density), boxStart, boxEnd, LinearSmoothScroller.SnapToAny); }
else { return base.CalculateDtToFit((int)(viewStart - (((MainActivity)Xamarin.Essentials.Platform.CurrentActivity).Cumulative ? 27 : 54) * Application.Context.Resources.DisplayMetrics.Density), (int)(viewStart + (((MainActivity)Xamarin.Essentials.Platform.CurrentActivity).Cumulative ? 81 : 108) * Application.Context.Resources.DisplayMetrics.Density), boxStart, boxEnd, LinearSmoothScroller.SnapToAny); }
}
}
However, depending on previous scrolling & what is currently visible, the LinearSmoothScroller is sometimes called while the user is doing normal scrolling and prevents the user from scrolling. What I need to do is prevent the LinearSmoothScroller from being called when the user does normal scrolling so that it is only called when I scrolled programmatically using StartSmoothScroll. How can I do this? Thanks.