From 998018032a72fb1490683393baf7e6e5210c5557 Mon Sep 17 00:00:00 2001 From: SpiritCroc Date: Fri, 29 Oct 2021 18:37:00 +0200 Subject: [PATCH] BetterLinearLayoutManager: Fix anchor view to preferred position Change-Id: Ib6b960603a25ffc4920067227a821debdb04973e --- .../recyclerview/widget/BetterLinearLayoutManager.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/vector/src/main/java/de/spiritcroc/recyclerview/widget/BetterLinearLayoutManager.java b/vector/src/main/java/de/spiritcroc/recyclerview/widget/BetterLinearLayoutManager.java index 8aa21bc698..dc8e385afd 100644 --- a/vector/src/main/java/de/spiritcroc/recyclerview/widget/BetterLinearLayoutManager.java +++ b/vector/src/main/java/de/spiritcroc/recyclerview/widget/BetterLinearLayoutManager.java @@ -906,9 +906,15 @@ public class BetterLinearLayoutManager extends LinearLayoutManager implements mStackFromEnd); if (referenceChild != null) { anchorInfo.assignFromView(referenceChild, getPosition(referenceChild)); + if (mCareAboutAnchorPlacement && mPreferredAnchorPosition >= 0) { + // Place anchor at the preferred anchor position + final int boundsStart = mOrientationHelper.getStartAfterPadding(); + final int boundsEnd = mOrientationHelper.getEndAfterPadding(); + anchorInfo.mCoordinate = (int) ((boundsEnd - boundsStart) * mPreferredAnchorPlacement); + // If all visible views are removed in 1 pass, reference child might be out of bounds. // If that is the case, offset it back to 0 so that we use these pre-layout children. - if (!state.isPreLayout() && supportsPredictiveItemAnimations()) { + } else if (!state.isPreLayout() && supportsPredictiveItemAnimations()) { // validate this child is at least partially visible. if not, offset it to start final int childStart = mOrientationHelper.getDecoratedStart(referenceChild); final int childEnd = mOrientationHelper.getDecoratedEnd(referenceChild);