2

Here, I used a LazyRow with the snappingLayout behavior & the items number of that LazyRow is the size of a list which contains listOf some dates

GetScrollIndex(dateName = listOf("Monday", "Tuesday", "Wednesday", "Thursday"))

I want to change the text value

text = dateName[0] hardcoded value to dynamic value that will change its value according to the position of the snappingLayouts index number

Here's What I Tried

enter image description here

@Composable
fun MainUi() {
    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(MaterialTheme.colorScheme.background)
    ) {
        GetScrollIndex(dateName = listOf("Monday", "Tuesday", "Wednesday", "Thursday"))
    }
}
@Suppress("OPT_IN_IS_NOT_ENABLED")
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun GetScrollIndex(dayName: List<String>) {
    val state = rememberLazyListState()
    val snappingLayout = remember(state) { SnapLayoutInfoProvider(state) }
    val flingBehavior = rememberSnapFlingBehavior(snappingLayout)
    Column {
        Text(
            modifier = Modifier
                .fillMaxWidth(),
            text = dateName[0],
            style = GoogleSansTypography.titleMedium,
            color = MaterialTheme.colorScheme.secondary,
            textAlign = TextAlign.Center
        )
        LazyRow(
            state = state,
            flingBehavior = flingBehavior,
            modifier = Modifier
                .fillMaxSize(),
            contentPadding = PaddingValues(
                horizontal = 30.dp,
                vertical = 40.dp
            ),
            horizontalArrangement = Arrangement.spacedBy(10.dp)
        ) {
            items(dayName.size) {
                Card(
                    modifier = Modifier
                        .fillParentMaxSize(),
                    colors = CardDefaults.cardColors(MaterialTheme.colorScheme.primary)
                ) {

                }
            }
        }
    }
}
im_gh0st
  • 21
  • 3

1 Answers1

2

You can use itemsIndexed(items) overload such as

itemsIndexed(items){ index: Int, item: String ->

}
Thracian
  • 43,021
  • 16
  • 133
  • 222
  • I've Tried this approach but it has a scope inside the item itself, I want to use this index number outside this scope to another composable, also i'm trying to use this approach differently to affect the other composable outside this scope. Maybe i'll need to use SideEffects not sure tho, i'm noob to android. – im_gh0st Nov 29 '22 at 16:10