diff --git a/vector/src/main/java/de/spiritcroc/recyclerview/widget/BLLRecyclerView.kt b/vector/src/main/java/de/spiritcroc/recyclerview/widget/BLLRecyclerView.kt new file mode 100644 index 0000000000..fd6ebe2443 --- /dev/null +++ b/vector/src/main/java/de/spiritcroc/recyclerview/widget/BLLRecyclerView.kt @@ -0,0 +1,25 @@ +package de.spiritcroc.recyclerview.widget + +import android.content.Context +import android.util.AttributeSet +import androidx.recyclerview.widget.RecyclerView + +/** + * A recyclerview to use with BetterLinearLayoutManager. + * Exposes some things that androidx' LinearLayoutManager would use but is package-private in androidx' RecyclerView. + */ +class BLLRecyclerView : RecyclerView { + constructor(context: Context): super(context) + constructor(context: Context, attrs: AttributeSet?): super(context, attrs) + constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int): super(context, attrs, defStyleAttr) + + override fun onMeasure(widthSpec: Int, heightSpec: Int) { + super.onMeasure(widthSpec, heightSpec) + (layoutManager as? BetterLinearLayoutManager)?.setPreviousMeasure(measuredWidth, measuredHeight) + } + + override fun setLayoutManager(layout: LayoutManager?) { + super.setLayoutManager(layout) + (layoutManager as? BetterLinearLayoutManager)?.setIsBLLRecyclerView() + } +} 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 df5bfb0b2d..a6d13f0353 100644 --- a/vector/src/main/java/de/spiritcroc/recyclerview/widget/BetterLinearLayoutManager.java +++ b/vector/src/main/java/de/spiritcroc/recyclerview/widget/BetterLinearLayoutManager.java @@ -258,6 +258,18 @@ public class BetterLinearLayoutManager extends LinearLayoutManager implements return true; } + // Moved from androidx' State in RecyclerView.java, so we can access it + private int mPreviousMeasuredWidth = 0; + private int mPreviousMeasuredHeight = 0; + private boolean isBLLRecyclerView = false; + public void setPreviousMeasure(int width, int height) { + mPreviousMeasuredWidth = width; + mPreviousMeasuredHeight = height; + } + public void setIsBLLRecyclerView() { + isBLLRecyclerView = true; + } + /** * {@inheritDoc} */ @@ -646,15 +658,22 @@ public class BetterLinearLayoutManager extends LinearLayoutManager implements // The 2 booleans below are necessary because if we are laying out from the end, and the // previous measured dimension is different from the new measured value, then any // previously calculated anchor will be incorrect. - boolean reCalcAnchorDueToVertical = layoutFromEnd - && getOrientation() == RecyclerView.VERTICAL - && state.getPreviousMeasuredHeight() != getHeight(); - boolean reCalcAnchorDueToHorizontal = layoutFromEnd - && getOrientation() == RecyclerView.HORIZONTAL - && state.getPreviousMeasuredWidth() != getWidth(); + boolean reCalcAnchor; + if (isBLLRecyclerView) { + boolean reCalcAnchorDueToVertical = layoutFromEnd + && getOrientation() == RecyclerView.VERTICAL + && /*state.getPreviousMeasuredHeight()*/ mPreviousMeasuredHeight != getHeight(); + boolean reCalcAnchorDueToHorizontal = layoutFromEnd + && getOrientation() == RecyclerView.HORIZONTAL + && /*state.getPreviousMeasuredWidth()*/ mPreviousMeasuredWidth != getWidth(); - boolean reCalcAnchor = reCalcAnchorDueToVertical || reCalcAnchorDueToHorizontal - || mPendingScrollPosition != RecyclerView.NO_POSITION || mPendingSavedState != null; + reCalcAnchor = reCalcAnchorDueToVertical || reCalcAnchorDueToHorizontal + || mPendingScrollPosition != RecyclerView.NO_POSITION || mPendingSavedState != null; + } else { + Timber.w("Using legacy layouting method without BLLRecyclerView"); + // Legacy fallback for 0815 RecyclerViews without exposed previous measured width/height + reCalcAnchor = mPendingScrollPosition != RecyclerView.NO_POSITION || mPendingSavedState != null; + } final View focused = getFocusedChild(); if (!mAnchorInfo.mValid || reCalcAnchor) { diff --git a/vector/src/main/res/layout/fragment_timeline.xml b/vector/src/main/res/layout/fragment_timeline.xml index 4ce2c7d840..ce17590aae 100644 --- a/vector/src/main/res/layout/fragment_timeline.xml +++ b/vector/src/main/res/layout/fragment_timeline.xml @@ -79,7 +79,7 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/liveLocationStatusIndicator" /> -