From ac24652b89cc2b9976abcec70e4406596c530f5e Mon Sep 17 00:00:00 2001 From: Ruben Sousa Date: Fri, 22 Mar 2024 23:57:34 +0100 Subject: [PATCH 1/2] Fix grid not aligning correctly to keyline --- .../test/tests/DpadRecyclerViewTest.kt | 22 ++--- .../test/tests/alignment/GridAlignmentTest.kt | 84 +++++++++++++++++++ .../alignment/LayoutAlignment.kt | 1 + 3 files changed, 96 insertions(+), 11 deletions(-) create mode 100644 dpadrecyclerview/src/androidTest/kotlin/com/rubensousa/dpadrecyclerview/test/tests/alignment/GridAlignmentTest.kt diff --git a/dpadrecyclerview/src/androidTest/kotlin/com/rubensousa/dpadrecyclerview/test/tests/DpadRecyclerViewTest.kt b/dpadrecyclerview/src/androidTest/kotlin/com/rubensousa/dpadrecyclerview/test/tests/DpadRecyclerViewTest.kt index 92a1f3a6..ca095f8c 100644 --- a/dpadrecyclerview/src/androidTest/kotlin/com/rubensousa/dpadrecyclerview/test/tests/DpadRecyclerViewTest.kt +++ b/dpadrecyclerview/src/androidTest/kotlin/com/rubensousa/dpadrecyclerview/test/tests/DpadRecyclerViewTest.kt @@ -196,19 +196,19 @@ abstract class DpadRecyclerViewTest { return bounds } - protected fun assertChildBounds( - childIndex: Int, - bounds: ViewBounds, - fromStart: Boolean = true - ) { + protected fun getChildrenBounds(position: Int): ViewBounds { + var bounds: ViewBounds? = null onRecyclerView("Assert child bounds") { recyclerView -> - val layoutManager = recyclerView.layoutManager!! - val view = getChildAt(layoutManager, childIndex, fromStart) - assertThat(view.left).isEqualTo(bounds.left) - assertThat(view.top).isEqualTo(bounds.top) - assertThat(view.right).isEqualTo(bounds.right) - assertThat(view.bottom).isEqualTo(bounds.bottom) + val viewHolder = recyclerView.findViewHolderForLayoutPosition(position)!! + val view = viewHolder.itemView + bounds = ViewBounds( + left = view.left, + top = view.top, + right = view.right, + bottom = view.bottom + ) } + return requireNotNull(bounds) { "View at $position not found" } } protected fun assertChildDecorations( diff --git a/dpadrecyclerview/src/androidTest/kotlin/com/rubensousa/dpadrecyclerview/test/tests/alignment/GridAlignmentTest.kt b/dpadrecyclerview/src/androidTest/kotlin/com/rubensousa/dpadrecyclerview/test/tests/alignment/GridAlignmentTest.kt new file mode 100644 index 00000000..48094e92 --- /dev/null +++ b/dpadrecyclerview/src/androidTest/kotlin/com/rubensousa/dpadrecyclerview/test/tests/alignment/GridAlignmentTest.kt @@ -0,0 +1,84 @@ +/* + * Copyright 2022 RĂºben Sousa + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.rubensousa.dpadrecyclerview.test.tests.alignment + +import androidx.recyclerview.widget.RecyclerView +import com.google.common.truth.Truth.assertThat +import com.rubensousa.dpadrecyclerview.ChildAlignment +import com.rubensousa.dpadrecyclerview.ParentAlignment +import com.rubensousa.dpadrecyclerview.ParentAlignment.Edge +import com.rubensousa.dpadrecyclerview.test.TestAdapterConfiguration +import com.rubensousa.dpadrecyclerview.test.TestLayoutConfiguration +import com.rubensousa.dpadrecyclerview.test.helpers.getItemViewBounds +import com.rubensousa.dpadrecyclerview.test.helpers.getRecyclerViewBounds +import com.rubensousa.dpadrecyclerview.test.helpers.waitForIdleScrollState +import com.rubensousa.dpadrecyclerview.test.tests.DpadRecyclerViewTest +import com.rubensousa.dpadrecyclerview.testing.KeyEvents +import com.rubensousa.dpadrecyclerview.testing.R +import com.rubensousa.dpadrecyclerview.testing.rules.DisableIdleTimeoutRule +import org.junit.Rule +import org.junit.Test + +class GridAlignmentTest : DpadRecyclerViewTest() { + + @get:Rule + val idleTimeoutRule = DisableIdleTimeoutRule() + + override fun getDefaultAdapterConfiguration(): TestAdapterConfiguration { + return super.getDefaultAdapterConfiguration().copy( + numberOfItems = 50, + itemLayoutId = R.layout.dpadrecyclerview_test_item_grid + ) + } + + override fun getDefaultLayoutConfiguration(): TestLayoutConfiguration { + return TestLayoutConfiguration( + spans = 5, + orientation = RecyclerView.VERTICAL, + parentAlignment = ParentAlignment( + edge = Edge.NONE, + offset = 0, + fraction = 0.5f + ), + childAlignment = ChildAlignment( + offset = 0, + fraction = 0.5f + ) + ) + } + + @Test + fun testItemInFirstColumnAndLastRowIsAlignedToTheCenter() { + launchFragment() + + // given + val recyclerViewBounds = getRecyclerViewBounds() + val itemViewBounds = getItemViewBounds(position = 0) + + // when + repeat(10) { + KeyEvents.pressDown() + waitForIdleScrollState() + } + + // then + val bounds = getChildrenBounds(position = 45) + assertThat(bounds.top) + .isEqualTo(recyclerViewBounds.height() / 2 - itemViewBounds.height() / 2) + } + +} diff --git a/dpadrecyclerview/src/main/java/com/rubensousa/dpadrecyclerview/layoutmanager/alignment/LayoutAlignment.kt b/dpadrecyclerview/src/main/java/com/rubensousa/dpadrecyclerview/layoutmanager/alignment/LayoutAlignment.kt index 1b8907bf..d4d2cd73 100644 --- a/dpadrecyclerview/src/main/java/com/rubensousa/dpadrecyclerview/layoutmanager/alignment/LayoutAlignment.kt +++ b/dpadrecyclerview/src/main/java/com/rubensousa/dpadrecyclerview/layoutmanager/alignment/LayoutAlignment.kt @@ -316,6 +316,7 @@ internal class LayoutAlignment( } private fun getAnchor(view: View): Int { + updateChildAlignments(view) return if (isVertical) { getVerticalAnchor(view) } else { From d36eabe7101ffb9067493448bc2e392f1903c287 Mon Sep 17 00:00:00 2001 From: Ruben Sousa Date: Sat, 23 Mar 2024 00:00:02 +0100 Subject: [PATCH 2/2] Bump to 1.3.0-alpha02 --- docs/changelog.md | 8 ++++++++ gradle.properties | 2 +- mkdocs.yml | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/docs/changelog.md b/docs/changelog.md index 0bf23fec..38cd6bbf 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -2,6 +2,14 @@ ## Version 1.3.0 +### 1.3.0-alpha02 + +2024-03-23 + +#### Bug fixes + +- Fixed grids not aligning to the keyline for the last row in some cases. ([#203](https://github.com/rubensousa/DpadRecyclerView/issues/203)) + ### 1.3.0-alpha01 2024-03-17 diff --git a/gradle.properties b/gradle.properties index 625f86d8..1cfa48b4 100644 --- a/gradle.properties +++ b/gradle.properties @@ -22,4 +22,4 @@ kotlin.code.style=official # thereby reducing the size of the R class for that library android.nonTransitiveRClass=true android.enableR8.fullMode=true -LIBRARY_VERSION=1.3.0-alpha01 \ No newline at end of file +LIBRARY_VERSION=1.3.0-alpha02 \ No newline at end of file diff --git a/mkdocs.yml b/mkdocs.yml index f5892e7c..ed49387f 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -24,7 +24,7 @@ theme: extra: dpadrecyclerview: - version: '1.3.0-alpha01' + version: '1.3.0-alpha02' social: - icon: 'fontawesome/brands/github' link: 'https://github.com/rubensousa/DpadRecyclerView'