diff --git a/dpadrecyclerview/src/androidTest/kotlin/com/rubensousa/dpadrecyclerview/test/tests/recycling/RecyclingTest.kt b/dpadrecyclerview/src/androidTest/kotlin/com/rubensousa/dpadrecyclerview/test/tests/recycling/RecyclingTest.kt index 31542b1c..1ccfd337 100644 --- a/dpadrecyclerview/src/androidTest/kotlin/com/rubensousa/dpadrecyclerview/test/tests/recycling/RecyclingTest.kt +++ b/dpadrecyclerview/src/androidTest/kotlin/com/rubensousa/dpadrecyclerview/test/tests/recycling/RecyclingTest.kt @@ -25,6 +25,7 @@ import com.rubensousa.dpadrecyclerview.UnboundViewPool import com.rubensousa.dpadrecyclerview.test.TestAdapterConfiguration import com.rubensousa.dpadrecyclerview.test.TestGridFragment import com.rubensousa.dpadrecyclerview.test.TestLayoutConfiguration +import com.rubensousa.dpadrecyclerview.test.helpers.assertSelectedPosition import com.rubensousa.dpadrecyclerview.test.helpers.onRecyclerView import com.rubensousa.dpadrecyclerview.test.helpers.waitForCondition import com.rubensousa.dpadrecyclerview.test.tests.DpadRecyclerViewTest @@ -112,5 +113,34 @@ class RecyclingTest : DpadRecyclerViewTest() { assertThat(viewPool.getRecycledViewCount(0)).isEqualTo(0) } + @Test + fun testViewsAreRecreatedOnAttachedFromWindowAgain() { + launchFragment( + getDefaultLayoutConfiguration().copy( + recycleChildrenOnDetach = true + ) + ) + + waitForCondition("Waiting for RecyclerView layout") { recyclerView -> + recyclerView.layoutManager!!.childCount > 0 && !recyclerView.isLayoutRequested + } + + val childBounds = getChildrenBounds() + + onRecyclerView("Detach RecyclerView from window") { view -> + view.detachFromWindow() + } + + onRecyclerView("Attach RecyclerView to window") { view -> + view.attachToWindow() + } + + waitForCondition("Waiting for RecyclerView layout") { recyclerView -> + recyclerView.childCount > 0 && !recyclerView.isLayoutRequested + } + + assertSelectedPosition(0) + assertChildrenPositions(childBounds) + } } diff --git a/dpadrecyclerview/src/main/java/com/rubensousa/dpadrecyclerview/DpadRecyclerView.kt b/dpadrecyclerview/src/main/java/com/rubensousa/dpadrecyclerview/DpadRecyclerView.kt index 5e1e6342..8c2aa358 100644 --- a/dpadrecyclerview/src/main/java/com/rubensousa/dpadrecyclerview/DpadRecyclerView.kt +++ b/dpadrecyclerview/src/main/java/com/rubensousa/dpadrecyclerview/DpadRecyclerView.kt @@ -28,6 +28,7 @@ import android.view.View import android.view.ViewGroup import android.view.animation.Interpolator import androidx.annotation.Px +import androidx.annotation.VisibleForTesting import androidx.core.view.ViewCompat import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView @@ -1218,6 +1219,16 @@ open class DpadRecyclerView @JvmOverloads constructor( */ fun getOnMotionInterceptListener(): OnMotionInterceptListener? = motionInterceptListener + @VisibleForTesting + internal fun detachFromWindow() { + onDetachedFromWindow() + } + + @VisibleForTesting + internal fun attachToWindow() { + onAttachedToWindow() + } + private fun removeSelectionForRecycledViewHolders() { addRecyclerListener { holder -> val position = holder.absoluteAdapterPosition diff --git a/dpadrecyclerview/src/main/java/com/rubensousa/dpadrecyclerview/layoutmanager/PivotLayoutManager.kt b/dpadrecyclerview/src/main/java/com/rubensousa/dpadrecyclerview/layoutmanager/PivotLayoutManager.kt index cceeb955..7b68c5e4 100644 --- a/dpadrecyclerview/src/main/java/com/rubensousa/dpadrecyclerview/layoutmanager/PivotLayoutManager.kt +++ b/dpadrecyclerview/src/main/java/com/rubensousa/dpadrecyclerview/layoutmanager/PivotLayoutManager.kt @@ -228,7 +228,7 @@ class PivotLayoutManager(properties: Properties) : RecyclerView.LayoutManager() if (childCount == 0) { return 0 } - return DpadScrollbarHelper.computeScrollRange( + return DpadScrollbarHelper.computeScrollRange( state = state, orientationHelper = layoutInfo.orientationHelper, startChild = layoutInfo.findFirstVisibleChild(), @@ -334,6 +334,9 @@ class PivotLayoutManager(properties: Properties) : RecyclerView.LayoutManager() override fun onAttachedToWindow(view: RecyclerView) { super.onAttachedToWindow(view) focusDispatcher.updateParentRecyclerView(view) + if (configuration.recycleChildrenOnDetach) { + requestLayout() + } } override fun onDetachedFromWindow(view: RecyclerView, recycler: RecyclerView.Recycler) {