From 782da0c23823d5bb2dfc7f68d22c45e674c0bc96 Mon Sep 17 00:00:00 2001 From: Rayen Sbai Date: Sun, 22 Nov 2026 13:05:02 +0100 Subject: [PATCH] fix(android): switch columns in one frame without blank or fading rows Changing numColumns showed two blank frames, then faded the rows in over four more. Every row binds again through notifyDataSetChanged, which RecyclerView animated as new rows fading in from transparent, and the new layout manager laid out from a posted pass one frame after the props. A rebind of every row now lays out without item animations, and a new layout manager lays out at the end of the prop update that made it. --- .../java/me/sectiontn/flume/FlumeAdapter.kt | 3 ++ .../me/sectiontn/flume/FlumeItemAnimator.kt | 35 +++++++++++++++++++ .../main/java/me/sectiontn/flume/FlumeView.kt | 17 +++++++-- .../me/sectiontn/flume/FlumeViewManager.kt | 5 +++ 4 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 android/src/main/java/me/sectiontn/flume/FlumeItemAnimator.kt diff --git a/android/src/main/java/me/sectiontn/flume/FlumeAdapter.kt b/android/src/main/java/me/sectiontn/flume/FlumeAdapter.kt index 1e21b75..80a7234 100644 --- a/android/src/main/java/me/sectiontn/flume/FlumeAdapter.kt +++ b/android/src/main/java/me/sectiontn/flume/FlumeAdapter.kt @@ -24,6 +24,8 @@ class FlumeAdapter( var onPress: ((position: Int, pressId: String?, long: Boolean) -> Unit)? = null var fullSpans = FullSpans(IntArray(0)) var onFirstData: (() -> Unit)? = null + // Every row binds again at the same data, which the list lays out without item animations. + var onRebindAll: (() -> Unit)? = null var onDataChanged: ((reset: Boolean) -> Unit)? = null var onStickyIndicesChanged: (() -> Unit)? = null // Data indices of the applied data, from the store with each data change. @@ -50,6 +52,7 @@ class FlumeAdapter( } private fun rebindAll() { + onRebindAll?.invoke() measurer.invalidate() if (storeId != 0) FlumeJni.invalidateMeasures(storeId) // React grid cells take their width from JS, which learns it from the slot snapshot. diff --git a/android/src/main/java/me/sectiontn/flume/FlumeItemAnimator.kt b/android/src/main/java/me/sectiontn/flume/FlumeItemAnimator.kt new file mode 100644 index 0000000..47fa9b4 --- /dev/null +++ b/android/src/main/java/me/sectiontn/flume/FlumeItemAnimator.kt @@ -0,0 +1,35 @@ +package me.sectiontn.flume + +import androidx.recyclerview.widget.DefaultItemAnimator +import androidx.recyclerview.widget.RecyclerView + +// RecyclerView's animator, held still for the layout that rebinds every row at a new shape or size. +class FlumeItemAnimator : DefaultItemAnimator() { + // Set before such a rebind and cleared once its layout completes, so its rows land in place instead of fading in. + var still = false + + override fun animateAppearance(holder: RecyclerView.ViewHolder, pre: ItemHolderInfo?, post: ItemHolderInfo): Boolean = + if (still) settle(holder) else super.animateAppearance(holder, pre, post) + + override fun animateDisappearance(holder: RecyclerView.ViewHolder, pre: ItemHolderInfo, post: ItemHolderInfo?): Boolean = + if (still) settle(holder) else super.animateDisappearance(holder, pre, post) + + override fun animatePersistence(holder: RecyclerView.ViewHolder, pre: ItemHolderInfo, post: ItemHolderInfo): Boolean = + if (still) settle(holder) else super.animatePersistence(holder, pre, post) + + override fun animateChange( + oldHolder: RecyclerView.ViewHolder, + newHolder: RecyclerView.ViewHolder, + pre: ItemHolderInfo, + post: ItemHolderInfo, + ): Boolean { + if (!still) return super.animateChange(oldHolder, newHolder, pre, post) + if (newHolder !== oldHolder) dispatchAnimationFinished(newHolder) + return settle(oldHolder) + } + + private fun settle(holder: RecyclerView.ViewHolder): Boolean { + dispatchAnimationFinished(holder) + return false + } +} diff --git a/android/src/main/java/me/sectiontn/flume/FlumeView.kt b/android/src/main/java/me/sectiontn/flume/FlumeView.kt index 3bd9413..d6faf43 100644 --- a/android/src/main/java/me/sectiontn/flume/FlumeView.kt +++ b/android/src/main/java/me/sectiontn/flume/FlumeView.kt @@ -8,7 +8,6 @@ import android.view.ViewConfiguration import android.content.res.Configuration import android.os.SystemClock import android.widget.FrameLayout -import androidx.recyclerview.widget.DefaultItemAnimator import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView @@ -94,7 +93,7 @@ class FlumeView(context: Context) : FrameLayout(context) { } // Change animations stay off so a rebind never flickers; false removes the animator entirely. - private val animator = DefaultItemAnimator().apply { supportsChangeAnimations = false } + private val animator = FlumeItemAnimator().apply { supportsChangeAnimations = false } var itemAnimations = true set(value) { if (value == field) return @@ -186,6 +185,7 @@ class FlumeView(context: Context) : FrameLayout(context) { init { slots.onRendered = { slot -> jumps.rendered(slot)?.let { runJump(it) } } + adapter.onRebindAll = { animator.still = true } recycler.layoutManager = layoutManager recycler.adapter = adapter recycler.itemAnimator = animator @@ -301,6 +301,7 @@ class FlumeView(context: Context) : FrameLayout(context) { } private fun onLaidOut() { + animator.still = false if (reclaimOnLayout) { reclaimOnLayout = false slots.reclaim() @@ -660,6 +661,18 @@ class FlumeView(context: Context) : FrameLayout(context) { kept?.let { layoutManager.scrollToPositionWithOffset(it.firstVisiblePosition, it.firstVisibleOffsetPx) } adapter.configure(horizontal, numColumns, inverted, paddingPx) sticky.refresh(force = true) + layoutWithProps = true + } + + // A new layout manager has no rows until it lays out, so it does in the frame of the props that made it. + private var layoutWithProps = false + + fun afterProps() { + if (!layoutWithProps) return + layoutWithProps = false + if (width == 0 || height == 0) return + removeCallbacks(measureAndLayout) + measureAndLayout.run() } private fun applyDrawDistance() { diff --git a/android/src/main/java/me/sectiontn/flume/FlumeViewManager.kt b/android/src/main/java/me/sectiontn/flume/FlumeViewManager.kt index ddaf121..48d01bd 100644 --- a/android/src/main/java/me/sectiontn/flume/FlumeViewManager.kt +++ b/android/src/main/java/me/sectiontn/flume/FlumeViewManager.kt @@ -24,6 +24,11 @@ class FlumeViewManager : ViewGroupManager(), FlumeViewManagerInterfac override fun createViewInstance(context: ThemedReactContext): FlumeView = FlumeView(context) + override fun onAfterUpdateTransaction(view: FlumeView) { + super.onAfterUpdateTransaction(view) + view.afterProps() + } + // Slot views live in holders and the bay; Fabric's child order is mirrored so its removeViewAt finds them. override fun needsCustomLayoutForChildren(): Boolean = true