简体   繁体   中英

Use lazyColum inside the column has an error in the Jetpack Compose

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

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

Example:

@Composable
fun SampleScreen(){
Box{

    Column{
            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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM