-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #259 from rubensousa/alignment_lookup
Add AlignmentLookup for customizing individual alignment config per item
- Loading branch information
Showing
15 changed files
with
558 additions
and
221 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
189 changes: 189 additions & 0 deletions
189
...idTest/kotlin/com/rubensousa/dpadrecyclerview/test/tests/alignment/AlignmentLookupTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
/* | ||
* 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.AlignmentLookup | ||
import com.rubensousa.dpadrecyclerview.ChildAlignment | ||
import com.rubensousa.dpadrecyclerview.ParentAlignment | ||
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.onRecyclerView | ||
import com.rubensousa.dpadrecyclerview.test.helpers.waitForIdleScrollState | ||
import com.rubensousa.dpadrecyclerview.test.helpers.waitForLayout | ||
import com.rubensousa.dpadrecyclerview.test.tests.DpadRecyclerViewTest | ||
import com.rubensousa.dpadrecyclerview.testing.KeyEvents | ||
import com.rubensousa.dpadrecyclerview.testing.rules.DisableIdleTimeoutRule | ||
import org.junit.Rule | ||
import org.junit.Test | ||
|
||
class AlignmentLookupTest : DpadRecyclerViewTest() { | ||
|
||
@get:Rule | ||
val idleTimeoutRule = DisableIdleTimeoutRule() | ||
|
||
override fun getDefaultLayoutConfiguration(): TestLayoutConfiguration { | ||
return TestLayoutConfiguration( | ||
spans = 1, | ||
orientation = RecyclerView.VERTICAL, | ||
parentAlignment = ParentAlignment( | ||
edge = ParentAlignment.Edge.MIN_MAX, | ||
offset = 0, | ||
fraction = 0f | ||
), | ||
childAlignment = ChildAlignment( | ||
offset = 0, | ||
fraction = 0f | ||
) | ||
) | ||
} | ||
|
||
@Test | ||
fun testItemRespectsParentAlignmentLookup() { | ||
// given | ||
launchFragment() | ||
val recyclerViewBounds = getRecyclerViewBounds() | ||
|
||
// when | ||
onRecyclerView("Set alignment") { recyclerView -> | ||
recyclerView.setAlignmentLookup(object : AlignmentLookup { | ||
override fun getParentAlignment( | ||
viewHolder: RecyclerView.ViewHolder, | ||
): ParentAlignment? { | ||
if (viewHolder.layoutPosition == 0) { | ||
return ParentAlignment(offset = 0, fraction = 0.5f) | ||
} | ||
return null | ||
} | ||
}) | ||
} | ||
|
||
// then | ||
waitForLayout() | ||
val viewBounds = getItemViewBounds(position = 0) | ||
assertThat(viewBounds.top).isEqualTo(recyclerViewBounds.height() / 2) | ||
} | ||
|
||
@Test | ||
fun testItemRespectsChildAlignmentLookup() { | ||
// given | ||
launchFragment() | ||
|
||
// when | ||
onRecyclerView("Set alignment") { recyclerView -> | ||
recyclerView.setAlignmentLookup(object : AlignmentLookup { | ||
override fun getChildAlignment(viewHolder: RecyclerView.ViewHolder): ChildAlignment? { | ||
if (viewHolder.layoutPosition == 0) { | ||
return ChildAlignment(offset = 0, fraction = 0.5f) | ||
} | ||
return null | ||
} | ||
}) | ||
} | ||
|
||
// then | ||
waitForLayout() | ||
val viewBounds = getItemViewBounds(position = 0) | ||
assertThat(viewBounds.top).isEqualTo(-viewBounds.height() / 2) | ||
} | ||
|
||
@Test | ||
fun testScrollingAlignsToLookup() { | ||
// given | ||
launchFragment() | ||
val centerParentAlignment = ParentAlignment(fraction = 0.5f) | ||
val centerChildAlignment = ChildAlignment(fraction = 0.5f) | ||
val recyclerViewBounds = getRecyclerViewBounds() | ||
|
||
// when | ||
onRecyclerView("Set alignment") { recyclerView -> | ||
recyclerView.setAlignmentLookup(object : AlignmentLookup { | ||
override fun getParentAlignment( | ||
viewHolder: RecyclerView.ViewHolder, | ||
): ParentAlignment? { | ||
if (viewHolder.layoutPosition % 2 == 0) { | ||
return centerParentAlignment | ||
} | ||
return null | ||
} | ||
|
||
override fun getChildAlignment(viewHolder: RecyclerView.ViewHolder): ChildAlignment? { | ||
if (viewHolder.layoutPosition % 2 == 0) { | ||
return centerChildAlignment | ||
} | ||
return null | ||
} | ||
}) | ||
} | ||
waitForLayout() | ||
|
||
repeat(10) { index -> | ||
val viewBounds = getItemViewBounds(position = index) | ||
// then | ||
if (index % 2 == 0) { | ||
assertThat(viewBounds.centerY()).isEqualTo(recyclerViewBounds.height() / 2) | ||
} else { | ||
assertThat(viewBounds.top).isEqualTo(0) | ||
} | ||
KeyEvents.pressDown() | ||
waitForIdleScrollState() | ||
} | ||
} | ||
|
||
@Test | ||
fun testScrollIsStillAppliedAfterFastScrolling() { | ||
// given | ||
launchFragment() | ||
val bottomParentAlignment = ParentAlignment(fraction = 1f) | ||
val bottomChildAlignment = ChildAlignment(fraction = 1f) | ||
val recyclerViewBounds = getRecyclerViewBounds() | ||
|
||
onRecyclerView("Set alignment") { recyclerView -> | ||
recyclerView.setAlignmentLookup(object : AlignmentLookup { | ||
override fun getParentAlignment( | ||
viewHolder: RecyclerView.ViewHolder, | ||
): ParentAlignment? { | ||
if (viewHolder.layoutPosition == 0) { | ||
return bottomParentAlignment | ||
} | ||
return null | ||
} | ||
|
||
override fun getChildAlignment(viewHolder: RecyclerView.ViewHolder): ChildAlignment? { | ||
if (viewHolder.layoutPosition == 0) { | ||
return bottomChildAlignment | ||
} | ||
return null | ||
} | ||
}) | ||
} | ||
waitForLayout() | ||
|
||
// when | ||
KeyEvents.pressDown(times = 10) | ||
waitForIdleScrollState() | ||
KeyEvents.pressUp(times = 10) | ||
waitForIdleScrollState() | ||
|
||
// then | ||
val viewBounds = getItemViewBounds(position = 0) | ||
assertThat(viewBounds.bottom).isEqualTo(recyclerViewBounds.bottom) | ||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
dpadrecyclerview/src/main/java/com/rubensousa/dpadrecyclerview/AlignmentLookup.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright 2024 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 | ||
|
||
import androidx.recyclerview.widget.RecyclerView.ViewHolder | ||
|
||
/** | ||
* Allows [DpadRecyclerView] to align differently for each ViewHolder. | ||
* When this is used, the [ParentAlignment.Edge] preference has no effect | ||
* and you're fully responsible to pick an anchor for all ViewHolders | ||
*/ | ||
interface AlignmentLookup { | ||
|
||
/** | ||
* @return the [ParentAlignment] configuration to be used for [viewHolder] | ||
* or null to fallback to the default one set via [DpadRecyclerView.setParentAlignment] | ||
*/ | ||
fun getParentAlignment(viewHolder: ViewHolder): ParentAlignment? = null | ||
|
||
/** | ||
* @return the [ChildAlignment] configuration to be used for [viewHolder] | ||
* or null to fallback to the default one set via [DpadRecyclerView.setChildAlignment] | ||
*/ | ||
fun getChildAlignment(viewHolder: ViewHolder): ChildAlignment? = null | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.