Use lazyColum inside the column has an error in the Jetpack Compose
Asked Answered
S

2

9

I have a lazyColumn which I want to use inside the column but I get the below error and app crashes:

Nesting scrollable in the same direction layouts like LazyColumn and Column(Modifier.verticalScroll()) is not allowed. If you want to add a header before the list of items please take a look on LazyColumn component which has a DSL api which allows to first add a header via item() function and then the list of items via items()

lazyColumn codes, I have a list in this code:


@Composable
fun UpScreenSection(
    modifier: Modifier,
    state: ProfileState,
    viewModel: ProfileViewModel
) {
    Spacer(modifier = Modifier.size(24.dp))

    Column(
        modifier = modifier
            .fillMaxSize()
            .padding(24.dp)
    ) {
        if (!state.items.isNullOrEmpty()) {
            Box(
                modifier = modifier
                    .fillMaxSize()
            ) {
                LazyColumn(modifier = modifier.fillMaxSize()) {
                    items(state.items) { item ->
                        ProfileListItems(item = item, onItemClick = {
                            //TODO Navigate to specific screen
                            when (it.id) {
                                1 -> {
                                }
                                2 -> {
                                }
                                3 -> {
                                }
                                4 -> viewModel.navigate(ReferAFriendDestination.route())
                            }
                        })
                    }
                }
            }
        }
    }
    Spacer(modifier = Modifier.size(24.dp))
}

The above codes used inside the below composable codes:

@Composable
fun ProfileContentSection(
    modifier: Modifier = Modifier,
    viewModel: ProfileViewModel
) {
    val context = LocalContext.current
    val scrollState = rememberScrollState()
    Box(
        modifier = modifier
            .fillMaxSize()
    ) {
        val state = viewModel.state.value

        Column(
            modifier = modifier
                .fillMaxSize()
                .verticalScroll(state = scrollState)
        ) {
            AccountNameSection(modifier = modifier, viewModel = viewModel)
            UpScreenSection(modifier, state, viewModel)   // used above block codes
            DownScreenSection(modifier, context)
        }
        if (state.error.isNotBlank())
            SimpleSnackbar(
                text = state.error,
                modifier = modifier.align(Alignment.BottomCenter)
            )

        if (state.isLoading)
            Loading(modifier = modifier.align(Alignment.BottomCenter))

    }
}

how can I fix this error?

Note: I want to have a scrollable screen that has a list in it for small devices

Smukler answered 3/11, 2021 at 9:22 Comment(0)
S
11

I found the solution. I have to move every view that is out of the LayzyColumn, inside it.

Example:

@Composable
fun SampleScreen(){
    LazyColumn{
        item {
            // other views
        }

         items(state.items){listItem ->
          //Load list data 
         }

          item {
           //other views
          }
     }
}

With this code, I will have a screen that has a scrollable view.

Smukler answered 3/11, 2021 at 11:45 Comment(3)
Just in case someone wants to build a similar layout: You can just drop Box & Column in this example and it will work fineGraycegrayheaded
Yeah, the Box and Column is unnecessaryHeeled
To avoid the header item to scroll with the list items, replace item by stickyHeader:Cess
T
0

you can use only one LazyColumn. Just use this@LazyColumn:

LazyColumn {
    items (...){
        // or items
        [email protected] {
            
        }
    }
}
Tonl answered 9/5 at 14:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.