Skip to content

Commit

Permalink
Merge pull request #204 from rubensousa/fix-grid-alignment
Browse files Browse the repository at this point in the history
Fix grid not aligning correctly to keyline
  • Loading branch information
rubensousa authored Mar 22, 2024
2 parents c462f4f + d36eabe commit 64e8148
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 13 deletions.
8 changes: 8 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ internal class LayoutAlignment(
}

private fun getAnchor(view: View): Int {
updateChildAlignments(view)
return if (isVertical) {
getVerticalAnchor(view)
} else {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
LIBRARY_VERSION=1.3.0-alpha02
2 changes: 1 addition & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 64e8148

Please sign in to comment.