protected override (int First, int Center, int Last) GetVisibleItemsIndex(RecyclerView recyclerView) { var firstVisibleItemIndex = -1; var lastVisibleItemIndex = -1; var centerItemIndex = -1; if (recyclerView.GetLayoutManager() is LinearLayoutManager linearLayoutManager) { var firstView = recyclerView.FindViewHolderForAdapterPosition(linearLayoutManager.FindFirstVisibleItemPosition()); var lastView = recyclerView.FindViewHolderForAdapterPosition(linearLayoutManager.FindLastVisibleItemPosition()); var centerView = recyclerView.GetCenteredView(); firstVisibleItemIndex = GetIndexFromTemplatedCell(firstView?.ItemView); lastVisibleItemIndex = GetIndexFromTemplatedCell(lastView?.ItemView); centerItemIndex = GetIndexFromTemplatedCell(centerView); } return(firstVisibleItemIndex, centerItemIndex, lastVisibleItemIndex); }
public int GetGoToIndex(RecyclerView recyclerView, int carouselPosition, int newPosition) { if (!(recyclerView.GetLayoutManager() is LinearLayoutManager linearLayoutManager)) { return(-1); } var currentCarouselPosition = carouselPosition; var itemSourceCount = _itemsSource.Count; var diffToStart = currentCarouselPosition + (itemSourceCount - newPosition); var diffToEnd = itemSourceCount - currentCarouselPosition + newPosition; var centerView = recyclerView.GetCenteredView(); if (centerView == null) { return(-1); } var centerPosition = linearLayoutManager.GetPosition(centerView); var increment = currentCarouselPosition - newPosition; var incrementAbs = System.Math.Abs(increment); int goToPosition; if (diffToStart < incrementAbs) { goToPosition = centerPosition - diffToStart; } else if (diffToEnd < incrementAbs) { goToPosition = centerPosition + diffToEnd; } else { goToPosition = centerPosition - increment; } return(goToPosition); }