snapper icon indicating copy to clipboard operation
snapper copied to clipboard

LazyList doesn't show the item defined by `initialFirstVisibleItemIndex` in LazyListState

Open iamutkarshtiwari opened this issue 3 years ago • 4 comments

Describe the bug

LazyList with snapper doesn't scroll to the item defined by initialFirstVisibleItemIndex.

To Reproduce

Steps to reproduce the behavior:

val lazyListState = rememberLazyListState(initialFirstVisibleItemIndex = 3)

val flingBehavior = rememberSnapperFlingBehavior(
    lazyListState = lazyListState,
    endContentPadding = contentPadding.calculateEndPadding(LayoutDirection.Ltr),
)

LazyRow(
    state = lazyListState,
    flingBehavior = flingBehavior
) {
    items(5) {
        // show item here
    }
}

Expected behavior

On the initial display (or first launch), the lazylist should auto-scroll to 3rd item. The lazylist shouldn't scroll after display but rather scroll to the correct initial item index before displaying.

Actual behavior

On initial display, the lazylist shows 1st item as the 1st visible item.

To work this issue around, I am using a side affect to scroll to the initial page when the screen is shown

val lazyListState = rememberLazyListState(initialFirstVisibleItemIndex = 3)

// workaround
LaunchedEffect(Unit) {
    lazyListState.animateScrollToItem(initialPage)
}

val flingBehavior = rememberSnapperFlingBehavior(
    lazyListState = lazyListState,
    endContentPadding = contentPadding.calculateEndPadding(LayoutDirection.Ltr),
)

LazyRow(
    state = lazyListState,
    flingBehavior = flingBehavior
) {
    items(5) {
        // show item here
    }
}

Screenshots?

If applicable, add screenshots to help explain your problem.

Environment:

  • Android OS version: 13
  • Device: Google Pixel 4a
  • Library version: 0.2.2

Additional context

Add any other context about the problem here.

iamutkarshtiwari avatar Aug 23 '22 03:08 iamutkarshtiwari

Not a lot we can do about this unfortunately. FlingBehaviors aren't notified about the initial scroll value, only deltas.

FYI @andkulikov

chrisbanes avatar Aug 23 '22 10:08 chrisbanes

Hm, not sure I understood what exactly doesn't work for you. So you set initialFirstVisibleItemIndex to 3, but in fact it displays item 0? It is not even related to flingBehavior, it should not ignore the initial index you provided. And are you sure that if you not set flingBehavior on such list it makes any difference? Would be great if you can provide some screenshots. Thanks

andkulikov avatar Aug 23 '22 11:08 andkulikov

I just tried and with initialFirstVisibleItemIndex = 3 and it does display item index 3, but not snapped

Screenshot_1661256079

chrisbanes avatar Aug 23 '22 11:08 chrisbanes

Yes, that is expected. The same is true when you try to programmatically scroll via scrollTo/animateScrollTo. FlingBehavior is only run when the user is performing gestures via touch. @iamutkarshtiwari is it what you meant?

andkulikov avatar Aug 23 '22 12:08 andkulikov

I just tried and with initialFirstVisibleItemIndex = 3 and it does display item index 3

Interesting. In my case, it doesn't. @chrisbanes did you do exactly as I did? I mean did you just initialize the lazyliststate as

val lazyListState = rememberLazyListState(initialFirstVisibleItemIndex = 3)

and you carousel was showing the 3 item on initial display?

iamutkarshtiwari avatar Sep 02 '22 07:09 iamutkarshtiwari

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] avatar Oct 03 '22 04:10 github-actions[bot]