Is it possible in a LazyVerticalGrid to have a ran...
# compose
n
Is it possible in a LazyVerticalGrid to have a range of items rather than a count? For instance here:
Copy code
Column(Modifier.fillMaxSize()) {
        LazyVerticalGrid(
            cells = GridCells.Fixed(2),
            contentPadding = PaddingValues(start = 20.dp, end = 20.dp, top = 20.dp)
        ) {
            items(4) {
                CardItem(feature = features[it])
            }
        }
    }
Instead of
4
I'd like
0..3
Eventually I want my items
4..7
to be in an
AnimatedVisibility
so they slide in when my button is pressed.
j
This isn’t necessarily built into the lazy grid. But maybe you could use a variation of
windowed
to get the first and last window of items then show them conditionally using
AnimatedVisibility
instead of the
if
statement in the example. You could remember the calculation of the windows or something so you don’t have to recalculate them each recompose. Something like this:
Copy code
val itemList = listOf(1..7).windowed(size = 4, step = 1)
val lastFour = itemList.last()
val firstFour = itemList.first()
var showSecondWindow by remember { mutableStateOf(false) }

Column(Modifier.fillMaxSize()) {
    LazyVerticalGrid(
        cells = GridCells.Fixed(2),
        contentPadding = PaddingValues(start = 20.dp, end = 20.dp, top = 20.dp)
    ) {
        if (showSecondWindow)
            items(lastFour) {
                CardItem(feature = features[it])
            }
        else
            items(firstFour) {
                CardItem(feature = features[it])
            }
    }
}
I’m not sure how that’d affect the Performance of the Lazy part of the grid though or if swapping the items out is really what you want. Maybe it will give you another idea though 🙂
n
Thanks a lot, will try! 🙏
Turns out it can be solved using
Copy code
itemsIndexed
😊
👍 3
a
or you can try
(0..3).toList()
🙏 1