Skip to content
This repository has been archived by the owner on Nov 10, 2022. It is now read-only.

Add snapIndex lambda parameter #15

Merged
merged 10 commits into from
Feb 11, 2022
Merged

Add snapIndex lambda parameter #15

merged 10 commits into from
Feb 11, 2022

Conversation

chrisbanes
Copy link
Owner

@chrisbanes chrisbanes commented Feb 5, 2022

This block which returns the index which the apps wishes to snap to. The block is provided with the SnapperLayoutInfo and the index which Snapper has determined is the correct target index. Callers can override this value as they see fit.

A common use case could be rounding up/down to achieve groupings of items, which was the intention of #12.

A very simple version could look this this:

val GroupSize = 3

flingBehavior = rememberSnapperFlingBehavior(
    lazyListState = lazyListState,
    snapIndex = { layout, targetIndex ->
        val mod = targetIndex % GroupSize
        if (mod > (GroupSize / 2)) {
            // Round up towards infinity
            GroupSize + targetIndex - mod
        } else {
            // Round down towards zero
            targetIndex - mod
        }
    }
),

You could also look at the layout.currentItem to determine the fling direction to influence which direction to round.

@chrisbanes chrisbanes marked this pull request as ready for review February 5, 2022 17:34
@ghost
Copy link

ghost commented Feb 7, 2022

Can you, please, explain more about flingDirection?
For me it looks impossible to calculate fling direction without additional input.
For case with 3 items: I can be on 6th item and scroll to the start of the 5th, then lift the finger. Or I can be on 3rd item, scroll to the end of the 4th (start of the 5th), then lift the finger. Current item will return same object for those cases, as far as I tested. (with offset near 0). However I may want to use different calculations to continue gesture (because it's in the middle)

@chrisbanes
Copy link
Owner Author

Can you, please, explain more about flingDirection? For me it looks impossible to calculate fling direction without additional input.

You can determine that by: val flingForward = targetIndex > layout.currentItem.index

@ghost
Copy link

ghost commented Feb 7, 2022

Thanks, I reached behavior I wanted to 👍

This block which returns the index which the apps wishes to snap to.
The block is provided with the SnapperLayoutInfo and the index which Snapper
has determined is the correct target index. Callers can override this value as
they see fit.

A common use case could be rounding up/down to achieve groupings of items, which was
the intention of #12
Had to remove the default value to avoid overload ambiguity.
This maintains binary compatibility.
@chrisbanes chrisbanes enabled auto-merge (squash) February 11, 2022 12:15
Also added some docs for the new parameter
@chrisbanes chrisbanes enabled auto-merge (squash) February 11, 2022 12:59
@chrisbanes chrisbanes merged commit 4d27add into main Feb 11, 2022
@chrisbanes chrisbanes deleted the cb/snapindex-lambda branch February 11, 2022 13:34
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants