Skip to content

Commit

Permalink
Do not align to start if anchor is closer to the keyline
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensousa committed Jan 21, 2025
1 parent 576b0d5 commit 7f3cbc5
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,4 @@ fun swipeVerticallyBy(dy: Int) {
)
}


Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,33 @@ class VerticalAlignmentTest : DpadRecyclerViewTest() {
.isEqualTo(getRecyclerViewBounds().centerY())
}

@Test
fun testShortListAlignmentWithMinEdgeStillAlignsToKeyline() {
// given
val parentAlignment = ParentAlignment(
edge = Edge.MIN,
offset = 0,
fraction = 0.5f,
isFractionEnabled = true,
preferKeylineOverEdge = false
)

launchFragment(
getDefaultLayoutConfiguration().copy(parentAlignment = parentAlignment),
getDefaultAdapterConfiguration().copy(numberOfItems = 6)
)

// when
repeat(4) {
KeyEvents.pressDown(times = 1)
waitForIdleScrollState()
}

// then
assertThat(getItemViewBounds(position = 4).centerY())
.isEqualTo(getRecyclerViewBounds().centerY())
}

@Test
fun testLayoutAlignsToKeylineWhenThereAreNotManyItemsAndEdgeMaxIsUsed() {
val parentAlignment = ParentAlignment(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,9 @@ internal class ParentAlignmentCalculator {
if (isLayoutComplete()) {
return viewAnchor + getLayoutAbsoluteStart() <= startEdge + keyline
}
return isLayoutStartKnown() && !preferKeylineOverEdge(alignment)
return isLayoutStartKnown()
&& !preferKeylineOverEdge(alignment)
&& viewAnchor < keyline
}

private fun shouldAlignViewToEnd(
Expand All @@ -239,7 +241,9 @@ internal class ParentAlignmentCalculator {
if (isLayoutComplete()) {
return viewAnchor + getLayoutAbsoluteEnd() >= endEdge + keyline
}
return isLayoutStartKnown() && !preferKeylineOverEdge(alignment)
return isLayoutStartKnown()
&& !preferKeylineOverEdge(alignment)
&& viewAnchor > keyline
}

private fun calculateScrollOffsetToKeyline(anchor: Int, keyline: Int): Int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ class ShortListFragment : Fragment(R.layout.screen_recyclerview) {
binding.recyclerView.apply {
setParentAlignment(
ParentAlignment(
edge = ParentAlignment.Edge.NONE,
fraction = 0.5f
edge = ParentAlignment.Edge.MIN,
offset = 0,
fraction = 0.5f,
isFractionEnabled = true,
preferKeylineOverEdge = false
)
)
setFocusableDirection(FocusableDirection.CIRCULAR)
setItemSpacing(dpToPx(16.dp))
adapter = Adapter(
items = List(4) { i ->
items = List(8) { i ->
"Item $i"
}
)
Expand Down

0 comments on commit 7f3cbc5

Please sign in to comment.