1

I have been trying to make it so that the first item on a LazyRow is centered in the middle of the screen as soon as the app is opened. The snapping behavior works once it is scrolled, but when the app is initially opened, the first item is not centered on the page.

LazyRow with snapper and infinite scrolling

@OptIn(ExperimentalSnapperApi::class)
@Composable
fun CircularList(
    data: List<SingleBox>,
    modifier: Modifier = Modifier,
    isEndless: Boolean = true
) {
    val listState = rememberLazyListState(
        if (isEndless) Int.MAX_VALUE / 2 else 0
    )

val configuration = LocalConfiguration.current

val screenWidth = configuration.screenWidthDp.dp
val contentPadding = PaddingValues(horizontal = screenWidth / 2) //This moves starting point of item horizontally
BoxWithConstraints {
    LazyRow(
        state = listState,
        modifier = modifier,
        contentPadding = contentPadding,
        flingBehavior = rememberSnapperFlingBehavior(listState, SnapOffsets.Center, snapIndex = { _, startIndex, targetIndex ->
            targetIndex.coerceIn(startIndex - 7, startIndex + 7) //This snaps item to center of page when LazyRow stops moving
        })

    )
    {
        items(
            count = if (isEndless) Int.MAX_VALUE else data.size, //This makes it scroll infinitly
            itemContent = {
                val index = it % data.size
                CustomItem(data[index])    // item composable
            },

        )
    }

}

}

How it looks when opened enter image description here

How it should look when app is opened enter image description here

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • Have you tried Modifier.padding(start="someDpValue") for the LazyRow? Sometimes we would add padding to the first and last elements to get this to work. – S. Miller Jul 14 '22 at 23:07
  • I have not tried it. I did try adding padding to contentPadding and it does move the item left or right depending on the DpValue. The problem with this is that it is guesswork, and I would have to change it again. I will try Modifer.padding and see if it does anything – Erick Sorto Jul 14 '22 at 23:58
  • @S.Miller I just tried but it just made the LazyRow smaller and disappeared away from the edges – Erick Sorto Jul 15 '22 at 00:05
  • What happens if you scroll to the second item in the list in a `LaunchedEffect` – Rafsanjani Jul 15 '22 at 06:16
  • @Rafsanjani Can you clarify what you mean? I am not sure what you mean by LaunchedEffect. If I scroll to the second item it snaps to the center of the screen, which is what you see in image 2. The problem is that it's not centered once it is opened. It looks like image one when the app is opened. – Erick Sorto Jul 15 '22 at 15:04
  • Does this answer your question? [Snap to an index Lazyrow](https://stackoverflow.com/questions/71901039/snap-to-an-index-lazyrow) – nglauber Jul 15 '22 at 15:12
  • @nglauber not quite. I am already using the snapper library by Chris Banes and it does it's job except when the app opens. When the app opens the center is in between indexes but once I scroll it works properly. I want it so that the first index is in the center as soon as I open the app. – Erick Sorto Jul 15 '22 at 15:40
  • Have you tried set `Modifier.fillMaxWidth()` in your `BoxWithConstraints` or use `LazyRow` without the `Box`? – nglauber Jul 15 '22 at 15:48
  • @nglauber I just tried it and it did nothing :/. I found this [this](https://stackoverflow.com/questions/71838922/jetpack-compose-make-the-first-element-in-a-lazyrow-be-aligned-to-the-center-o) but I was not sure how to implement it with the code I have. That is why I have BoxWithConstraints in the first place. Haha now I just realized it was you who answered that one – Erick Sorto Jul 15 '22 at 16:42
  • I can take a look into your code if you share it somehow... – nglauber Jul 15 '22 at 17:32
  • @nglauber Okay here is the [repo](https://github.com/ErickSorto/Redesign_Proto) – Erick Sorto Jul 15 '22 at 17:40

1 Answers1

0

I think the best option here would be the Accompanist Pager Library.

Update: This library was deprecated, with official pager support in androidx.compose.foundation.pager from Compose 1.4.0+

Merkost
  • 1,251
  • 9
  • 16