Is there a way to check if user has scrolled before

210 Views Asked by At

Is there a way in Kotlin and jetpack composes Lazylists to check if the user has scrolled the list before checking if it stopped? I know that there is a state of isScrollInproggress but if you ask if !isScrollinproggress then this will be true from the start before the user has eaven scrolled once. So my question is is there like a scrollended state to get a hold of or am i screwed?

1

There are 1 best solutions below

0
Abhimanyu On

If you need a boolean flag to determine if the user has scrolled before, you can save that data in a state manually.

Sample code logic

var isScrolled by remember {
    mutableStateOf(false)
}
val scrollState = rememberLazyListState()
LaunchedEffect(
    key1 = scrollState.isScrollInProgress,
) {
    if (!isScrolled) {
        isScrolled = scrollState.isScrollInProgress
    }
}

Example code

@Composable
fun IsScrolled() {
    var isScrolled by remember {
        mutableStateOf(false)
    }
    val scrollState = rememberLazyListState()
    val list = Array(50) {
        "Item ${it + 1}"
    }

    LaunchedEffect(
        key1 = scrollState.isScrollInProgress,
    ) {
        if (!isScrolled) {
            isScrolled = scrollState.isScrollInProgress
        }
    }

    Column(
        modifier = Modifier
            .fillMaxSize(),
    ) {
        Text(text = "$isScrolled")
        LazyColumn(
            state = scrollState,
            modifier = Modifier
                .background(LightGray)
                .fillMaxSize(),
        ) {
            items(list) {
                Text(
                    text = it,
                    modifier = Modifier
                        .padding(16.dp)
                        .background(White),
                )
            }
        }
    }
}